Trait Tid

Source
pub unsafe trait Tid<'a>: 'a {
    // Required methods
    fn self_id(&self) -> TypeId;
    fn id() -> TypeId
       where Self: Sized;
}
Expand description

This trait indicates that this type can be converted to trait object with typeid while preserving lifetime information. Extends Any functionality for types with single lifetime

Use it only as a dyn Tid<'a> or as super trait when you need to create trait object. In all other places use TidAble<'a>.

Lifetime here is necessary to make dyn Tid<'a> + 'a invariant over 'a.

Required Methods§

Source

fn self_id(&self) -> TypeId

Returns type id of the type of self

Note that returned type id is guaranteed to be different from provided by Any. It is necessary for the creation of dyn Tid from dyn Any to be sound.

Source

fn id() -> TypeId
where Self: Sized,

Returns type id of this type

Implementations§

Source§

impl<'a> dyn Tid<'a> + 'a

Source

pub fn downcast_any_ref<T: Any>(&self) -> Option<&T>

Tries to downcast dyn Tid to T

Use it only if dyn Tid was created from concrete T:Any via From implementations. See examples how it does relate to other downcast methods

struct S;
tid!(S);

let a = &S;
let from_any: &dyn Tid = a.into();
assert!(from_any.downcast_any_ref::<S>().is_some());
assert!(from_any.downcast_ref::<S>().is_none());

let direct = &S as &dyn Tid;
assert!(direct.downcast_any_ref::<S>().is_none());
assert!(direct.downcast_ref::<S>().is_some());
Source

pub fn downcast_any_mut<T: Any>(&mut self) -> Option<&mut T>

See downcast_any_ref

Source

pub fn downcast_any_box<T: Any>(self: Box<Self>) -> Result<Box<T>, Box<Self>>

See downcast_any_ref

Trait Implementations§

Source§

impl<'a: 'b, 'b, T: Any> From<&'b T> for &'b (dyn Tid<'a> + 'a)

Source§

fn from(f: &'b T) -> Self

Converts to this type from the input type.
Source§

impl<'a: 'b, 'b, T: Any> From<&'b mut T> for &'b mut (dyn Tid<'a> + 'a)

Source§

fn from(f: &'b mut T) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: Any> From<Box<T>> for Box<dyn Tid<'a> + 'a>

Source§

fn from(f: Box<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a> TidAble<'a> for dyn Tid<'a> + 'a

Implementors§

Source§

impl<'a, T: ?Sized + TidAble<'a>> Tid<'a> for T