Skip to main content

Arena

Struct Arena 

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

Source

pub const fn new() -> Self

Creates an empty arena.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an arena with pre-allocated capacity for capacity items.

Source

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).

Source

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).

Source

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).

Source

pub const fn len(&self) -> usize

Returns the number of allocated items.

Source

pub const fn is_empty(&self) -> bool

Returns true if the arena contains no items.

Source

pub const fn capacity(&self) -> usize

Returns the current capacity in items.

Source

pub const fn checkpoint(&self) -> Checkpoint<T>

Saves the current allocation state.

Use with rollback to discard allocations made after this point.

Source

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.

Source

pub fn reset(&mut self)

Removes all items, running their destructors.

Retains allocated memory for reuse.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over all allocated items.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns a mutable iterator over all allocated items.

Source

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.

Source

pub const fn is_valid(&self, idx: Idx<T>) -> bool

Returns true if idx points to a valid item in this arena.

An index becomes invalid after rollback or reset removes the item it pointed to.

Source

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.

Source

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.

Source

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.

Source

pub fn iter_indexed(&self) -> IterIndexed<'_, T>

Returns an iterator yielding (Idx<T>, &T) pairs in allocation order.

Source

pub fn iter_indexed_mut(&mut self) -> IterIndexedMut<'_, T>

Returns a mutable iterator yielding (Idx<T>, &mut T) pairs in allocation order.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more items.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the backing storage to fit the current number of items.

Trait Implementations§

Source§

impl<T> Default for Arena<T>

Source§

fn default() -> Self

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

impl<T> Extend<T> for Arena<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<T> for Arena<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Index<Idx<T>> for Arena<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, idx: Idx<T>) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<Idx<T>> for Arena<T>

Source§

fn index_mut(&mut self, idx: Idx<T>) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Arena<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Arena<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for Arena<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.