Skip to main content

Arena

Struct Arena 

Source
pub struct Arena<T> { /* private fields */ }
Expand description

A typed arena for storing values contiguously.

Values are never moved once allocated, so references remain valid for the lifetime of the arena.

Implementations§

Source§

impl<T> Arena<T>

Source

pub fn new() -> Self

Create a new empty arena.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new arena with the specified capacity.

Source

pub fn alloc(&mut self, value: T) -> Idx<T>

Allocate a value in the arena and return its index.

Source

pub fn alloc_many(&mut self, values: impl IntoIterator<Item = T>) -> IdxRange<T>

Allocate multiple values at once, returning the index range.

Source

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

Get a reference to the value at the given index.

§Panics

Panics if the index is out of bounds (in debug mode).

Source

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

Get a mutable reference to the value at the given index.

Source

pub fn get_range(&self, range: IdxRange<T>) -> &[T]

Get a slice of values for the given range.

Source

pub fn len(&self) -> usize

Get the number of values in the arena.

Source

pub fn is_empty(&self) -> bool

Check if the arena is empty.

Source

pub fn next_idx(&self) -> Idx<T>

Get the index that would be assigned to the next allocation.

Source

pub fn iter_indexed(&self) -> impl Iterator<Item = (Idx<T>, &T)>

Iterate over all (index, value) pairs.

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Iterate over all values.

Source

pub fn stats(&self) -> ArenaStats

Collect the current size statistics.

Source

pub fn shrink_to_fit(&mut self)

Shrink the arena’s backing storage to fit.

Source

pub fn clear(&mut self)

Reset the arena, dropping all values.

Trait Implementations§

Source§

impl<T: Debug> Debug for Arena<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Arena<T>

Source§

fn default() -> Self

Returns the “default value” for a type. 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

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.