pub struct Arena<T> { /* private fields */ }Expand description
A generational arena of T.
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 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.
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: the precondition for extending the arena by offset,
where a caller predicts the keys of entries it is about to append.