Skip to main content

Arena

Struct Arena 

Source
pub struct Arena<K, T, const N: usize>
where K: Kind,
{ /* private fields */ }
Expand description

N slots of T, addressed by generational Handle<K>.

Implementations§

Source§

impl<K, T, const N: usize> Arena<K, T, N>
where K: Kind, T: Default,

Source

pub fn new() -> Arena<K, T, N>

An empty arena.

Not const: a free slot holds T::default() and Default is not a const trait. Nothing built one in a const context — only size_of names the type there, which needs no value.

Source

pub const fn len(&self) -> usize

How many objects are live.

Source

pub const fn is_empty(&self) -> bool

Whether no object is live.

Source

pub const fn capacity(&self) -> usize

The capacity, N.

Source

pub fn contains(&self, handle: Handle<K>) -> bool

Whether handle names a live object.

Source

pub fn insert(&mut self, value: T) -> Result<Handle<K>, Error>

Place value in a free slot and return its handle.

§Errors

Error::NoMemory when every slot is occupied; the value is returned to the caller inside the error’s companion so nothing is lost — see Arena::try_insert.

Source

pub fn try_insert(&mut self, value: T) -> Result<Handle<K>, T>

Like Arena::insert, but hands the value back when there is no room.

§Errors

The value itself, when every slot is occupied.

Source

pub fn get(&self, handle: Handle<K>) -> Option<&T>

The object handle names.

Source

pub fn get_mut(&mut self, handle: Handle<K>) -> Option<&mut T>

The object handle names, mutably.

Source

pub fn resolve(&self, handle: Handle<K>) -> Result<&T, Error>

Like Arena::get, but says why: Error::InvalidHandle for the null handle or an index beyond the arena, Error::Gone for a generation that has been reused or freed.

§Errors

As above.

Source

pub fn resolve_mut(&mut self, handle: Handle<K>) -> Result<&mut T, Error>

Like Arena::resolve, mutably.

§Errors

As Arena::resolve.

Source

pub fn discard(&mut self, handle: Handle<K>) -> bool

Remove the object handle names, invalidating the handle and every copy of it. Free the slot handle names without handing the value back.

Same state change as Arena::remove – FREE set, len decremented, the old handle dead – but it does not MOVE the value out, and that is the whole point. remove answers Option<T>, which for a Tcb is over a hundred bytes returned THROUGH MEMORY: the caller gets a stack buffer, mem::take copies the value into it, and T::default() is written over the slot with a memset CALL. Every arena removal in this kernel is spelled let _ = ...remove(x) – not one reads the value – so all of that filled a buffer that is dropped on the next line.

Leaving the old value in the slot is sound because nothing can reach it: the slot’s generation now carries [FREE], which no handle can equal, and try_insert overwrites the value before handing out a new handle. It is the same argument Arena::remove’s own doc makes about the generation bump, applied to the value as well.

Drop is the one thing that cannot be left to chance, so it is not: needs_drop is a const fn, so for a T that owns something this still writes the default and runs the old value’s destructor exactly where remove ran it, and for plain data the whole branch folds away.

Returns whether a live object was there.

Source

pub fn remove(&mut self, handle: Handle<K>) -> Option<T>

Take the value out and free its slot, if handle still names it.

None for a handle that is stale, forged or already removed – and the slot’s generation moves on, so no copy of this handle resolves again.

Source

pub fn iter(&self) -> impl Iterator<Item = (Handle<K>, &T)>

Every live (handle, object) in slot order.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (Handle<K>, &mut T)>

Every live (handle, object) in slot order, mutably.

Source

pub fn handle_at(&self, index: u16) -> Option<Handle<K>>

The handle of the object at slot index, if live.

Trait Implementations§

Source§

impl<K, T, const N: usize> Debug for Arena<K, T, N>
where K: Kind, T: Debug + Default,

Source§

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

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

impl<K, T, const N: usize> Default for Arena<K, T, N>
where K: Kind, T: Default,

Source§

fn default() -> Arena<K, T, N>

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

Auto Trait Implementations§

§

impl<K, T, const N: usize> Freeze for Arena<K, T, N>
where [Slot<T>; N]: Freeze, PhantomData<K>: Freeze,

§

impl<K, T, const N: usize> RefUnwindSafe for Arena<K, T, N>
where [Slot<T>; N]: RefUnwindSafe, PhantomData<K>: RefUnwindSafe,

§

impl<K, T, const N: usize> Send for Arena<K, T, N>
where [Slot<T>; N]: Send, PhantomData<K>: Send,

§

impl<K, T, const N: usize> Sync for Arena<K, T, N>
where [Slot<T>; N]: Sync, PhantomData<K>: Sync,

§

impl<K, T, const N: usize> Unpin for Arena<K, T, N>
where [Slot<T>; N]: Unpin, PhantomData<K>: Unpin,

§

impl<K, T, const N: usize> UnsafeUnpin for Arena<K, T, N>
where [Slot<T>; N]: UnsafeUnpin, PhantomData<K>: UnsafeUnpin,

§

impl<K, T, const N: usize> UnwindSafe for Arena<K, T, N>
where [Slot<T>; N]: UnwindSafe, PhantomData<K>: 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.