Struct Cage

Source
pub struct Cage<'life, Z: Zonbi<'life>> { /* private fields */ }
Expand description

Holds a Zonbi with the 'life lifetime and can be casted into a dyn AnyZonbi.

This is done because it forbids to have a lower-lifetimed mutable reference to the zonbi, preventing a possible use-after-free reference. See this comment for more.

Implementations§

Source§

impl<'life, Z: Zonbi<'life>> Cage<'life, Z>

Source

pub fn new(val: Z) -> Self

Creates a new Cage for a zonbi.

Examples found in repository?
examples/type_map.rs (line 25)
19fn with_zonbi<'a>(a: &'a NonCopyI32) {
20    let my_struct = MyStruct { val: a };
21
22    let mut type_map: HashMap<ZonbiId, Box<dyn AnyZonbi<'a>>> = HashMap::new();
23    let id = ZonbiId::of::<MyStruct>();
24
25    type_map.insert(id, Box::new(Cage::new(my_struct)));
26
27    let r: &MyStruct<'a> = type_map[&id].downcast_ref::<MyStruct<'a>>().unwrap();
28    assert_eq!(r.val, &NonCopyI32(42));
29    println!("{:?}", r.val);
30}
Source

pub fn into_inner(self) -> Z::Casted

Returns the inner zonbi of this cage with a lifetime of 'life

Trait Implementations§

Source§

impl<'life, Z: Zonbi<'life>> AnyZonbi<'life> for Cage<'life, Z>

Source§

fn zonbi_id(&self) -> ZonbiId

Returns the ZonbiId of this erased type. Read more
Source§

unsafe fn get_raw(&self) -> *const ()

Source§

unsafe fn get_raw_mut(&mut self) -> *mut ()

Auto Trait Implementations§

§

impl<'life, Z> Freeze for Cage<'life, Z>
where Z: Freeze,

§

impl<'life, Z> RefUnwindSafe for Cage<'life, Z>
where Z: RefUnwindSafe,

§

impl<'life, Z> Send for Cage<'life, Z>
where Z: Send,

§

impl<'life, Z> Sync for Cage<'life, Z>
where Z: Sync,

§

impl<'life, Z> Unpin for Cage<'life, Z>
where Z: Unpin,

§

impl<'life, Z> UnwindSafe for Cage<'life, Z>
where Z: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.