pub struct Arena<T> { /* private fields */ }Expand description
Single-thread typed arena allocator.
Stores values of type T in a contiguous buffer, returning stable
Idx<T> handles for O(1) access. Values are dropped when the arena
is dropped, reset, or rolled back past their allocation point.
For thread-safe concurrent allocation, see SharedArena.
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 an arena with pre-allocated capacity for capacity items.
Sourcepub fn alloc(&mut self, value: T) -> Idx<T>
pub fn alloc(&mut self, value: T) -> Idx<T>
Allocates a value in the arena, returning its stable index.
O(1) amortized (backed by Vec::push).
Sourcepub fn get(&self, idx: Idx<T>) -> &T
pub fn get(&self, idx: Idx<T>) -> &T
Returns a reference to the value at idx.
§Panics
Panics if idx is out of bounds (stale after rollback/reset).
Sourcepub fn get_mut(&mut self, idx: Idx<T>) -> &mut T
pub fn get_mut(&mut self, idx: Idx<T>) -> &mut T
Returns a mutable reference to the value at idx.
§Panics
Panics if idx is out of bounds (stale after rollback/reset).
Sourcepub const fn checkpoint(&self) -> Checkpoint<T>
pub const fn checkpoint(&self) -> Checkpoint<T>
Saves the current allocation state.
Use with rollback to discard allocations
made after this point.
Sourcepub fn rollback(&mut self, cp: Checkpoint<T>)
pub fn rollback(&mut self, cp: Checkpoint<T>)
Rolls back to a previous checkpoint, dropping all values allocated after it.
O(k) where k = number of items dropped (destructors run).
§Panics
Panics if cp points beyond the current length.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Removes all items, running their destructors.
Retains allocated memory for reuse.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns a mutable iterator over all allocated items.
Sourcepub fn alloc_extend(
&mut self,
iter: impl IntoIterator<Item = T>,
) -> Option<Idx<T>>
pub fn alloc_extend( &mut self, iter: impl IntoIterator<Item = T>, ) -> Option<Idx<T>>
Allocates multiple values from an iterator, returning the index of the first allocated item.
Returns None if the iterator is empty.
O(n) where n = items yielded by the iterator.
Sourcepub fn try_get(&self, idx: Idx<T>) -> Option<&T>
pub fn try_get(&self, idx: Idx<T>) -> Option<&T>
Returns a reference to the value at idx, or None if the
index is out of bounds.
Sourcepub fn try_get_mut(&mut self, idx: Idx<T>) -> Option<&mut T>
pub fn try_get_mut(&mut self, idx: Idx<T>) -> Option<&mut T>
Returns a mutable reference to the value at idx, or None
if the index is out of bounds.
Sourcepub fn drain(&mut self) -> Drain<'_, T>
pub fn drain(&mut self) -> Drain<'_, T>
Removes all items, returning an iterator that yields them in allocation order.
The arena is empty after the iterator is consumed or dropped. Capacity is retained.
Sourcepub fn iter_indexed(&self) -> IterIndexed<'_, T> ⓘ
pub fn iter_indexed(&self) -> IterIndexed<'_, T> ⓘ
Returns an iterator yielding (Idx<T>, &T) pairs in allocation order.
Sourcepub fn iter_indexed_mut(&mut self) -> IterIndexedMut<'_, T> ⓘ
pub fn iter_indexed_mut(&mut self) -> IterIndexedMut<'_, T> ⓘ
Returns a mutable iterator yielding (Idx<T>, &mut T) pairs in
allocation order.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more items.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the backing storage to fit the current number of items.
Trait Implementations§
Source§impl<T> Extend<T> for Arena<T>
impl<T> Extend<T> for Arena<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)