Trait better_any::AnyExt
source · [−]pub trait AnyExt: Any {
fn downcast_ref<T: Any>(this: &Self) -> Option<&T> { ... }
fn downcast_mut<T: Any>(this: &mut Self) -> Option<&mut T> { ... }
fn downcast_rc<T: Any>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>> { ... }
fn downcast_arc<T: Any>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>> { ... }
fn downcast_box<T: Any>(this: Box<Self>) -> Result<Box<T>, Box<Self>> { ... }
fn downcast_move<T: Any>(this: Self) -> Option<T>
where
Self: Sized,
{ ... }
}
Expand description
Methods here are implemented as an associated functions because otherwise
for one they will conflict with methods defined on dyn Any
in stdlib,
for two they will be available on almost every type in the program causing confusing bugs and error messages
For example if you have &Box<dyn Any>
and call downcast_ref
, instead of failing or working on coerced &dyn Any
it would work with type id of Box<dyn Any>
itself instead of the type behind dyn Any
.
Provided Methods
sourcefn downcast_ref<T: Any>(this: &Self) -> Option<&T>
fn downcast_ref<T: Any>(this: &Self) -> Option<&T>
Attempts to downcast this to T
behind reference
sourcefn downcast_mut<T: Any>(this: &mut Self) -> Option<&mut T>
fn downcast_mut<T: Any>(this: &mut Self) -> Option<&mut T>
Attempts to downcast this to T
behind mutable reference
sourcefn downcast_rc<T: Any>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
fn downcast_rc<T: Any>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
Attempts to downcast this to T
behind Rc
pointer
sourcefn downcast_arc<T: Any>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
fn downcast_arc<T: Any>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
Attempts to downcast this to T
behind Arc
pointer
sourcefn downcast_box<T: Any>(this: Box<Self>) -> Result<Box<T>, Box<Self>>
fn downcast_box<T: Any>(this: Box<Self>) -> Result<Box<T>, Box<Self>>
Attempts to downcast this to T
behind Box
pointer
sourcefn downcast_move<T: Any>(this: Self) -> Option<T>where
Self: Sized,
fn downcast_move<T: Any>(this: Self) -> Option<T>where
Self: Sized,
Attempts to downcast owned Self
to T
,
useful only in generic context as a workaround for specialization