pub trait AnyZonbi<'life>: Sealed + 'life {
// Required methods
fn zonbi_id(&self) -> ZonbiId;
unsafe fn get_raw(&self) -> *const ();
unsafe fn get_raw_mut(&mut self) -> *mut ();
}
Required Methods§
Sourcefn zonbi_id(&self) -> ZonbiId
fn zonbi_id(&self) -> ZonbiId
Returns the ZonbiId
of this erased type.
This needs a shared reference to self in order to be object-safe.
If you have a concrete type without an instance of it, use Zonbi::zonbi_id
instead.
unsafe fn get_raw(&self) -> *const ()
unsafe fn get_raw_mut(&mut self) -> *mut ()
Implementations§
Source§impl<'life> dyn AnyZonbi<'life>
impl<'life> dyn AnyZonbi<'life>
Sourcepub fn represents<Z: Zonbi<'life>>(&self) -> bool
pub fn represents<Z: Zonbi<'life>>(&self) -> bool
Returns true
if the inner type represents the same as T
,
excluding its lifetimes.
Sourcepub fn downcast_ref<Z: Zonbi<'life>>(&self) -> Option<&Z::Casted>
pub fn downcast_ref<Z: Zonbi<'life>>(&self) -> Option<&Z::Casted>
Returns a shared reference to the inner value with a lifetime of 'life
if it represents the zonbi Z
, or None
if it doesn’t.
Examples found in repository?
examples/type_map.rs (line 27)
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}
pub unsafe fn downcast_ref_unchecked<Z: Zonbi<'life>>(&self) -> &Z::Casted
Sourcepub fn downcast_mut<Z: Zonbi<'life>>(&mut self) -> Option<&mut Z::Casted>
pub fn downcast_mut<Z: Zonbi<'life>>(&mut self) -> Option<&mut Z::Casted>
Returns an exclusive reference to the inner value with a lifetime of 'life
if it represents the zonbi Z
, or None
if it doesn’t.