pub trait TidExt<'a>: Tid<'a> {
// Provided methods
fn is<T: Tid<'a>>(&self) -> bool { ... }
fn downcast_ref<'b, T: Tid<'a>>(&'b self) -> Option<&'b T> { ... }
fn downcast_mut<'b, T: Tid<'a>>(&'b mut self) -> Option<&'b mut T> { ... }
fn downcast_rc<T: Tid<'a>>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>> { ... }
fn downcast_arc<T: Tid<'a>>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>> { ... }
fn downcast_box<T: Tid<'a>>(self: Box<Self>) -> Result<Box<T>, Box<Self>> { ... }
fn downcast_move<T: Tid<'a>>(self) -> Option<T>
where Self: Sized { ... }
}
Expand description
Extension trait that contains actual downcasting methods.
Use methods from this trait only if dyn Tid
was created directly from T
for this particular T
If Self
is Sized
then any of those calls is optimized to no-op because both T and Self are known statically.
Useful if you have generic code that you want to behave differently depending on which
concrete type replaces type parameter. Usually there are better ways to do this like specialization,
but sometimes it can be the only way.
Provided Methods§
Sourcefn downcast_ref<'b, T: Tid<'a>>(&'b self) -> Option<&'b T>
fn downcast_ref<'b, T: Tid<'a>>(&'b self) -> Option<&'b T>
Attempts to downcast self to T
behind reference
Sourcefn downcast_mut<'b, T: Tid<'a>>(&'b mut self) -> Option<&'b mut T>
fn downcast_mut<'b, T: Tid<'a>>(&'b mut self) -> Option<&'b mut T>
Attempts to downcast self to T
behind mutable reference
Sourcefn downcast_rc<T: Tid<'a>>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
fn downcast_rc<T: Tid<'a>>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
Attempts to downcast self to T
behind Rc
pointer
Sourcefn downcast_arc<T: Tid<'a>>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
fn downcast_arc<T: Tid<'a>>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
Attempts to downcast self to T
behind Arc
pointer
Sourcefn downcast_box<T: Tid<'a>>(self: Box<Self>) -> Result<Box<T>, Box<Self>>
fn downcast_box<T: Tid<'a>>(self: Box<Self>) -> Result<Box<T>, Box<Self>>
Attempts to downcast self to T
behind Box
pointer
Sourcefn downcast_move<T: Tid<'a>>(self) -> Option<T>where
Self: Sized,
fn downcast_move<T: Tid<'a>>(self) -> Option<T>where
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.