pub struct Arena<T> { /* private fields */ }Expand description
A simple arena allocator with generation-based indices.
This arena provides stable indices for inserted elements, with generation counters to detect use-after-free errors (ABA problem).
Implementations§
Source§impl<T> Arena<T>
impl<T> Arena<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new arena with the specified capacity.
Sourcepub fn insert(&mut self, value: T) -> ArenaIndex
pub fn insert(&mut self, value: T) -> ArenaIndex
Inserts a value into the arena and returns its index.
Sourcepub fn insert_with<F>(&mut self, f: F) -> ArenaIndexwhere
F: FnOnce(ArenaIndex) -> T,
pub fn insert_with<F>(&mut self, f: F) -> ArenaIndexwhere
F: FnOnce(ArenaIndex) -> T,
Inserts a value produced by f into the arena and returns its index.
The closure receives the assigned ArenaIndex, allowing callers to
construct records that embed their final ID without placeholder updates.
Sourcepub fn remove(&mut self, index: ArenaIndex) -> Option<T>
pub fn remove(&mut self, index: ArenaIndex) -> Option<T>
Removes the value at the given index and returns it.
Returns None if the index is invalid or the slot is vacant.
Sourcepub fn get(&self, index: ArenaIndex) -> Option<&T>
pub fn get(&self, index: ArenaIndex) -> Option<&T>
Returns a reference to the value at the given index.
Returns None if the index is invalid or the slot is vacant.
Sourcepub fn get_mut(&mut self, index: ArenaIndex) -> Option<&mut T>
pub fn get_mut(&mut self, index: ArenaIndex) -> Option<&mut T>
Returns a mutable reference to the value at the given index.
Returns None if the index is invalid or the slot is vacant.
Sourcepub fn contains(&self, index: ArenaIndex) -> bool
pub fn contains(&self, index: ArenaIndex) -> bool
Returns true if the index is valid and points to an occupied slot.
Sourcepub fn iter(&self) -> impl Iterator<Item = (ArenaIndex, &T)>
pub fn iter(&self) -> impl Iterator<Item = (ArenaIndex, &T)>
Iterates over all occupied slots.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (ArenaIndex, &mut T)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (ArenaIndex, &mut T)>
Iterates mutably over all occupied slots.