pub trait Downcast<R: Transience> {
// Required methods
fn is<T: Transient>(&self) -> bool;
fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>
where T::Transience: CanRecoverFrom<R>;
fn downcast_ref<T: Transient>(&self) -> Option<&T>
where T::Transience: CanRecoverFrom<R>;
fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>
where T::Transience: CanRecoverFrom<R>;
unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>
where T::Transience: CanRecoverFrom<R>;
unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &T
where T::Transience: CanRecoverFrom<R>;
unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut T
where T::Transience: CanRecoverFrom<R>;
}Expand description
Extension trait defining methods for downcasting the dyn Any<_> trait
object back into a concrete type.
This trait has an implementation provided for the dyn Any trait object,
as in not intended to be implemented by downstream types.
Required Methods§
sourcefn is<T: Transient>(&self) -> bool
fn is<T: Transient>(&self) -> bool
Returns true if the concrete type of the erased object is T, which can
be used to predict the outcome of calling the downcast
and similar methods.
Slight caveat: this method is not actually comparing the erased type
(call it E) to the given type T; in reality, it is comparing
E::Static to T::Static as defined in their Transient impls. This
is effectively equivalent for most purposes, but see the TypeId::of
documentation for a discussion of the subtle differences (especially
when using this check in the implementation of unsafe code).
sourcefn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>where
T::Transience: CanRecoverFrom<R>,
Attempt to downcast the box to a concrete type with its lifetime
parameters restored, returning the original in the Err variant
if the type was incorrect.
sourcefn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_ref<T: Transient>(&self) -> Option<&T>where
T::Transience: CanRecoverFrom<R>,
Returns a reference to the inner value with its lifetime parameters
restored if it is of type T, or None if it isn’t.
sourcefn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>where
T::Transience: CanRecoverFrom<R>,
Returns a mutable reference to the inner value with its lifetime
parameters restored if it is of type T, or None if it isn’t.
sourceunsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>where
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>where
T::Transience: CanRecoverFrom<R>,
Downcasts the box to a concrete type without compile-time checks.
For a safe alternative see downcast.
§Safety
The contained value must be of type T::Static; calling this method with
the incorrect type is undefined behavior. However, the the caller is not
expected to uphold any lifetime guarantees, since the trait bounds handle
this statically.
sourceunsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &Twhere
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &Twhere
T::Transience: CanRecoverFrom<R>,
Downcasts the shared reference to a concrete type without runtime checks.
For a safe alternative see downcast_ref.
§Safety
The contained value must be of type T::Static; calling this method with
the incorrect type is undefined behavior. However, the the caller is not
expected to uphold any lifetime guarantees, since the trait bounds handle
this statically.
sourceunsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut Twhere
T::Transience: CanRecoverFrom<R>,
unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut Twhere
T::Transience: CanRecoverFrom<R>,
Downcasts the mutable reference to a concrete type without runtime checks.
For a safe alternative see downcast_mut.
§Safety
The contained value must be of type T::Static; calling this method with
the incorrect type is undefined behavior. However, the the caller is not
expected to uphold any lifetime guarantees, since the trait bounds handle
this statically.