Skip to main content

AnyExt

Trait AnyExt 

Source
pub trait AnyExt: Any {
    // Provided methods
    fn downcast_ref<T>(this: &Self) -> Option<&T>
       where T: Any { ... }
    fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>
       where T: Any { ... }
    fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
       where T: Any { ... }
    fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
       where T: Any { ... }
    fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>
       where T: Any { ... }
    fn downcast_move<T>(this: Self) -> Option<T>
       where T: Any,
             Self: Sized { ... }
}
Expand description

Re-export public API. 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§

Source

fn downcast_ref<T>(this: &Self) -> Option<&T>
where T: Any,

Attempts to downcast this to T behind reference

Source

fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>
where T: Any,

Attempts to downcast this to T behind mutable reference

Source

fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
where T: Any,

Attempts to downcast this to T behind Rc pointer

Source

fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
where T: Any,

Attempts to downcast this to T behind Arc pointer

Source

fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>
where T: Any,

Attempts to downcast this to T behind Box pointer

Source

fn downcast_move<T>(this: Self) -> Option<T>
where T: Any, Self: Sized,

Attempts to downcast owned Self to T, useful only in generic context as a workaround for specialization

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> AnyExt for T
where T: Any + ?Sized,