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>
impl<T> Arena<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
An empty arena with room for capacity entries.
Sourcepub const fn scope(&self) -> u32
pub const fn scope(&self) -> u32
Which arena this is, for stamping keys that were rebuilt elsewhere.
UNSCOPED until the first insert.
Sourcepub const fn issued(&self, key: Key<T>) -> bool
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.
Sourcepub fn next_index(&self) -> Option<u32>
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.
Sourcepub fn key_at(&self, index: u32) -> Option<Key<T>>
pub fn key_at(&self, index: u32) -> Option<Key<T>>
The live key at slot index, if the slot holds a value.
Sourcepub fn insert(&mut self, value: T) -> Key<T>
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.
Sourcepub fn get(&self, key: Key<T>) -> Option<&T>
pub fn get(&self, key: Key<T>) -> Option<&T>
Borrow the value behind key, or None if the key is stale.
Sourcepub fn get_mut(&mut self, key: Key<T>) -> Option<&mut T>
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.
Sourcepub fn remove(&mut self, key: Key<T>) -> Option<T>
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.
Sourcepub fn retain(&mut self, keep: impl FnMut(Key<T>, &T) -> bool)
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.
Sourcepub fn iter(&self) -> impl Iterator<Item = (Key<T>, &T)>
pub fn iter(&self) -> impl Iterator<Item = (Key<T>, &T)>
Iterate over live (key, &value) pairs, in slot order.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (Key<T>, &mut T)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (Key<T>, &mut T)>
Iterate over live (key, &mut value) pairs, in slot order.
Sourcepub fn into_values(self) -> impl Iterator<Item = T>
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.
Sourcepub fn is_dense(&self) -> bool
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.