Skip to content

Instantly share code, notes, and snippets.

@robertohuertasm
Created August 9, 2020 13:42
Show Gist options
  • Save robertohuertasm/dbca7161880b9298cb32c8b7de81ecf8 to your computer and use it in GitHub Desktop.
Save robertohuertasm/dbca7161880b9298cb32c8b7de81ecf8 to your computer and use it in GitHub Desktop.
Downcasting rust -- error downcasting
use std::any::Any;
/// Method that can be use to safely downcast impl Responder to a specific type T
/// It returns a reference of type T
pub fn downcast<T>(responder: &dyn Any) -> Option<&T>
where
T: Responder + 'static,
{
responder.downcast_ref::<T>()
}
/// Method that can be use to safely downcast impl Responder to a specific type T
/// It returns a mutable reference of type T
pub fn downcast_mut<T>(responder: &mut dyn Any) -> Option<&mut T>
where
T: Responder + 'static,
{
responder.downcast_mut::<T>()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment