pub unsafe trait CastFrom<T> {
    fn cast(t: &T) -> &Self;
    fn cast_mut(t: &mut T) -> &mut Self;
}
Expand description

Helper trait for the MetaTable. This trait is required to be implemented for a trait to be compatible with the meta table.

Safety

Not casting self but e.g. a field to the trait object will result in Undefined Bahavior.

Examples

use shred::CastFrom;

trait Foo {
    fn foo1(&self);
    fn foo2(&mut self, x: i32) -> i32;
}

unsafe impl<T> CastFrom<T> for dyn Foo
where
    T: Foo + 'static,
{
    fn cast(t: &T) -> &(dyn Foo + 'static) {
        t
    }

    fn cast_mut(t: &mut T) -> &mut (dyn Foo + 'static) {
        t
    }
}

Required Methods

Casts an immutable T reference to a trait object.

Casts a mutable T reference to a trait object.

Implementors