Struct Arena

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

A container that can be used for arena allocation of values of a given type.

An Arena guarantees that all references returned are valid for the lifetime of the arena. A single value can be moved into the arena using Arena::alloc, and multiple values can be moved into one contiguous block of memory owned by the arena using Arena::alloc_extend.

All items in the arena are deallocated at the same time when the arena goes out of scope. There is no support for controlled deallocation of certain items in memory, so this arena type is most useful for designs where several groups of values should be allocated and deallocated together over the life of a program.

Internally, arena allocation is achieved by allocating contiguous blocks of memory that are never resized (to ensure values are never moved). All items will be allocated in a single, contiguous block of memory. When the arena’s current memory block is full, the block is “committed”, and a new, larger block of memory is allocated for future items.

This data type is not thread safe. In fact, usage across multiple threads will likely panic.

Implementations§

Source§

impl<T> Arena<T>

Source

pub fn new() -> Self

Creates a new arena.

Source

pub fn with_capacity(size: usize) -> Self

Creates a new arena with the given capacity.

Source

pub fn is_empty(&self) -> bool

Checks if the arena is empty.

Source

pub fn len(&self) -> usize

Returns the number of elements allocated in the arena.

Source

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

Allocates a new value in the arena, returning a mutable reference to that value.

Source

pub fn alloc_extend<I>(&self, iterable: I) -> &mut [T]
where I: IntoIterator<Item = T>,

Allocates the contents of an iterator in the arena into a contiguous block of memory.

Returns a mutable slice containing all allocated values.

Source

pub fn reserve(&self, additional: usize)

Ensures there is enough continuous space for at least additional values.

Source

pub fn into_vec(self) -> Vec<T>

Converts the Arena<T> into a Vec<T>.

Items will be in the same order of allocation in the arena.

Source

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

Returns an iterator that provides mutable access to all elements in the arena, in order of allocation.

Arenas only allow mutable iteration because the entire arena must be borrowed for the duration of the iteration. The mutable borrow to call this method allows Rust’s borrow checker to enforce this rule.

Source§

impl Arena<u8>

Source

pub fn alloc_str(&self, s: &str) -> &mut str

Allocates a string slice in the arena, returning a mutable reference to it.

Trait Implementations§

Source§

impl<T> Default for Arena<T>

Source§

fn default() -> Self

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

impl<A> FromIterator<A> for Arena<A>

Source§

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

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Arena<T>

§

impl<T> !RefUnwindSafe for Arena<T>

§

impl<T> Send for Arena<T>
where T: Send,

§

impl<T> !Sync for Arena<T>

§

impl<T> Unpin for Arena<T>
where T: Unpin,

§

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.