pub struct Hato<Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>>>(/* private fields */);Expand description
Arenas of heterogeneous trait objects, stored by type in separate vectors.
As with bump allocators, Drop implementations will not be invoked on deallocation
or calls to remove. If you need to run the logic contained in destructors,
you can acquire a mutable reference with get_mut, and then call core::ptr::drop_in_place.
This type is subject to the ABA problem. Using handles of previously removed elements will not trigger errors but will return stale or newly inserted elements. This can lead to unexpected behavior, as shown below:
// Initialize the collection
let mut arena = hato::Hato::<dyn core::fmt::Debug>::default();
// Insert an element...
let x = arena.push(5_u8);
// ... then remove it
arena.remove(x);
// ! We can still use the handle to access it
assert_eq!(format!("{:?}", arena.get(x)), "5");
// Insert a new element into the arena
let _y = arena.push(9_u8);
// ! The old handle accesses the repurposed capacity
assert_eq!(format!("{:?}", arena.get(x)), "9");Implementations§
source§impl<Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>>> Hato<Trait>
impl<Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>>> Hato<Trait>
sourcepub fn push<T: Unsize<Trait> + Unscrupulous>(&mut self, x: T) -> Handle
pub fn push<T: Unsize<Trait> + Unscrupulous>(&mut self, x: T) -> Handle
Insert x into the arena for its specific type.
§Panics
This function will panic if the number of arenas overflows the index type.
sourcepub unsafe fn get(&self, handle: Handle) -> &Trait
pub unsafe fn get(&self, handle: Handle) -> &Trait
Retrieve the element identified by handle as a trait object.
§Safety
The handle must originate from the same instance of Hato.
sourcepub fn get_mut(&mut self, handle: Handle) -> &mut Trait
pub fn get_mut(&mut self, handle: Handle) -> &mut Trait
Retrieve the element identified by handle as a mutable trait object.
§Safety
The handle must originate from the same instance of Hato.