Struct hato::Hato

source ·
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>

source

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.

source

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.

source

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.

source

pub fn remove(&mut self, handle: Handle)

Remove the element identified by handle from the collection.

Trait Implementations§

source§

impl<Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>>> Clone for Hato<Trait>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Trait: Debug + ?Sized + Pointee<Metadata = DynMetadata<Trait>>> Debug for Hato<Trait>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>>> Default for Hato<Trait>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Trait> Freeze for Hato<Trait>
where Trait: ?Sized,

§

impl<Trait> !RefUnwindSafe for Hato<Trait>

§

impl<Trait> Send for Hato<Trait>
where Trait: ?Sized,

§

impl<Trait> Sync for Hato<Trait>
where Trait: ?Sized,

§

impl<Trait> Unpin for Hato<Trait>
where Trait: ?Sized,

§

impl<Trait> !UnwindSafe for Hato<Trait>

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.