Trait transient::Downcast

source ·
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§

source

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).

source

fn downcast<T: Transient>(self: Box<Self>) -> Result<Box<T>, Box<Self>>

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.

source

fn downcast_ref<T: Transient>(&self) -> Option<&T>

Returns a reference to the inner value with its lifetime parameters restored if it is of type T, or None if it isn’t.

source

fn downcast_mut<T: Transient>(&mut self) -> Option<&mut T>

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.

source

unsafe fn downcast_unchecked<T: Transient>(self: Box<Self>) -> Box<T>

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.

source

unsafe fn downcast_ref_unchecked<T: Transient>(&self) -> &T

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.

source

unsafe fn downcast_mut_unchecked<T: Transient>(&mut self) -> &mut T

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.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<R: Transience> Downcast<R> for dyn Any<R> + '_

source§

impl<R: Transience> Downcast<R> for dyn Any<R> + Send + '_

source§

impl<R: Transience> Downcast<R> for dyn Any<R> + Send + Sync + '_