Skip to main content

Arena

Struct Arena 

Source
pub struct Arena<T> { /* private fields */ }
Expand description

A generational arena of T.

Live values are stored packed, in two runs. The tail holds every slot from some index on, each one occupied, so its values are found by arithmetic; an arena that has only been appended to is all tail. The values before it are scattered: slots whose neighbours were dropped, found through a map from slot to position. An arena holds what is live and nothing for the slots it no longer uses, so cloning one costs what it holds, however many slots it has handed out.

Implementations§

Source§

impl<T> Arena<T>

Source

pub const fn new() -> Self

An empty arena.

Source

pub fn with_capacity(capacity: usize) -> Self

An empty arena with room for capacity entries.

Source

pub const fn len(&self) -> usize

Number of live entries.

Source

pub const fn is_empty(&self) -> bool

Whether there are no live entries.

Source

pub const fn scope(&self) -> u32

Which arena this is, for stamping keys that were rebuilt elsewhere.

UNSCOPED until the first insert.

Source

pub const fn issued(&self, key: Key<T>) -> bool

Whether a key was issued by this arena.

Distinct from Arena::contains, which also asks whether the slot is still live. This asks only whether the key belongs here at all, which is the question a caller wants when reporting why a lookup failed.

Source

pub fn next_index(&self) -> Option<u32>

The slot the next Arena::insert takes, at generation zero, when it takes a fresh one; None while a freed slot waits to be reused.

The precondition for extending the arena by offset, where a caller predicts the keys of entries it is about to append: the k-th insert from here lands at this index plus k.

Source

pub fn key_at(&self, index: u32) -> Option<Key<T>>

The live key at slot index, if the slot holds a value.

Source

pub fn insert(&mut self, value: T) -> Key<T>

Insert a value, returning its key.

The first insert is what fixes the arena’s identity, since Arena::new is const and cannot read a counter. That is safe because an arena with nothing in it has issued no keys to disagree with.

§Panics

If the arena exceeds u32::MAX slots. A single model reaching four billion topological entities is a bug elsewhere, not a case to handle.

Source

pub fn get(&self, key: Key<T>) -> Option<&T>

Borrow the value behind key, or None if the key is stale.

Source

pub fn get_mut(&mut self, key: Key<T>) -> Option<&mut T>

Mutably borrow the value behind key, or None if the key is stale.

Source

pub fn contains(&self, key: Key<T>) -> bool

Whether key resolves to a live entry.

Source

pub fn remove(&mut self, key: Key<T>) -> Option<T>

Remove and return the value behind key, if it is live.

The slot’s generation is bumped, invalidating every outstanding copy of key, and a later insert reuses the slot.

Source

pub fn retain(&mut self, keep: impl FnMut(Key<T>, &T) -> bool)

Keep the entries keep answers true for and drop the rest.

Every kept key still resolves to its value. A dropped key fails to resolve, and its slot is never handed out again, so no later entry answers to it. What is dropped stops costing memory: the arena holds the kept values, packed.

Source

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

Iterate over live (key, &value) pairs, in slot order.

Source

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

Iterate over live (key, &mut value) pairs, in slot order.

Source

pub fn values(&self) -> impl Iterator<Item = &T>

Iterate over live values.

Source

pub fn into_values(self) -> impl Iterator<Item = T>

Consume the arena, yielding its live values in index order.

For appending one arena’s contents onto another: the receiving arena hands out its own keys, so the values travel bare.

Source

pub fn is_dense(&self) -> bool

Whether the arena has only ever been appended to: every slot occupied, every generation zero.

When this holds, Arena::len is also the next index Arena::insert will hand out.

Source

pub fn clear(&mut self)

Remove every entry. Existing keys go stale, and their slots are not handed out again.

Trait Implementations§

Source§

impl<T: Clone> Clone for Arena<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Arena<T>

Source§

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

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

impl<T> Default for Arena<T>

Source§

fn default() -> Self

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

impl<T> Index<Key<T>> for Arena<T>

Source§

fn index(&self, key: Key<T>) -> &T

§Panics

If the key is stale. Use Arena::get where that is a possibility.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<Key<T>> for Arena<T>

Source§

fn index_mut(&mut self, key: Key<T>) -> &mut T

§Panics

If the key is stale. Use Arena::get_mut where that is a possibility.

Auto Trait Implementations§

§

impl<T> Freeze for Arena<T>

§

impl<T> RefUnwindSafe for Arena<T>
where T: RefUnwindSafe,

§

impl<T> Send for Arena<T>
where T: Send,

§

impl<T> Sync for Arena<T>
where T: Sync,

§

impl<T> Unpin for Arena<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Arena<T>

§

impl<T> UnwindSafe for Arena<T>
where T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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.