pub struct Arena<T, Options: ArenaOptions<T> = Options>(/* private fields */);
Expand description

An arena that allocates items of type T in non-amortized O(1) (constant) time.

The arena allocates fixed-size chunks of memory, each able to hold up to Options::ChunkSize items. All items are allocated on the heap.

Panics

The arena may panic when created or used if mem::size_of::<T>() times Options::ChunkSize is greater than usize::MAX.

Implementations§

source§

impl<T, Options: ArenaOptions<T>> Arena<T, Options>

source

pub fn new() -> Self

Creates a new Arena.

source

pub fn len(&self) -> usize

Returns the total number of items that have been allocated.

source

pub fn is_empty(&self) -> bool

Checks whether the arena is empty.

source

pub fn alloc(&self, value: T) -> &mut Twhere Options: ArenaOptions<T, Mutable = Bool<true>>,

Allocates a new item in the arena and initializes it with value. Returns a reference to the allocated item.

This method calls handle_alloc_error if memory allocation fails; for a version that returns None instead, see Self::try_alloc.

source

pub fn try_alloc(&self, value: T) -> Option<&mut T>where Options: ArenaOptions<T, Mutable = Bool<true>>,

Like Self::alloc, but returns None if memory allocation fails.

source

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

Allocates a new item in the arena and initializes it with value. Returns a shared/immutable reference to the allocated item.

This method calls handle_alloc_error if memory allocation fails; for a version that returns None instead, see Self::try_alloc.

source

pub fn try_alloc_shared(&self, value: T) -> Option<&T>

Like Self::alloc_shared, but returns None if memory allocation fails.

source

pub fn iter(&self) -> Iter<'_, T, Options> where Options: ArenaOptions<T, Mutable = Bool<false>>,

Returns an iterator over the items in this arena.

source

pub unsafe fn iter_unchecked(&self) -> Iter<'_, T, Options>

Returns an iterator over the items in this arena.

Safety

There must be no mutable references (or references derived from mutable references) to items (or parts of items) in this arena or instances of IterMut for this arena.

source

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

Returns a mutable iterator over the items in this arena.

source§

impl<T, Options> Arena<T, Options>where Options: ArenaOptions<T, SupportsPositions = Bool<true>>,

source

pub fn iter_at(&self, position: &Position) -> Iter<'_, T, Options> where Options: ArenaOptions<T, Mutable = Bool<false>>,

Returns an iterator starting at the specified position.

Panics

May panic if position does not refer to a position in this arena.

source

pub unsafe fn iter_at_unchecked( &self, position: &Position ) -> Iter<'_, T, Options>

Returns an iterator starting at the specified position.

Panics

May panic if position does not refer to a position in this arena.

Safety

Same requirements as Self::iter_unchecked.

source

pub fn iter_mut_at(&mut self, position: &Position) -> IterMut<'_, T, Options>

Returns a mutable iterator starting at the specified position.

Panics

May panic if position does not refer to a position in this arena.

Trait Implementations§

source§

impl<T, Options: ArenaOptions<T>> Default for Arena<T, Options>

source§

fn default() -> Self

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

impl<T, Options> Drop for Arena<T, Options>where Options: ArenaOptions<T>,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a, T, Options> IntoIterator for &'a Arena<T, Options>where Options: ArenaOptions<T, Mutable = Bool<false>>,

§

type IntoIter = Iter<'a, T, Options>

Which kind of iterator are we turning this into?
§

type Item = &'a T

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T, Options> IntoIterator for &'a mut Arena<T, Options>where Options: ArenaOptions<T>,

§

type IntoIter = IterMut<'a, T, Options>

Which kind of iterator are we turning this into?
§

type Item = &'a mut T

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, Options: ArenaOptions<T>> IntoIterator for Arena<T, Options>

§

type IntoIter = IntoIter<T, Options>

Which kind of iterator are we turning this into?
§

type Item = T

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

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

Auto Trait Implementations§

§

impl<T, Options = TypedOptions<Usize<16>, Bool<false>, Bool<true>>> !RefUnwindSafe for Arena<T, Options>

§

impl<T, Options = TypedOptions<Usize<16>, Bool<false>, Bool<true>>> !Sync for Arena<T, Options>

§

impl<T, Options> Unpin for Arena<T, Options>where <<Options as ArenaOptions<T>>::SupportsPositions as SupportsPositionsPriv>::Rc: Unpin,

§

impl<T, Options> UnwindSafe for Arena<T, Options>where T: UnwindSafe + RefUnwindSafe, <<Options as ArenaOptions<T>>::ChunkSize as ChunkSizePriv<T>>::Array: RefUnwindSafe, <<Options as ArenaOptions<T>>::SupportsPositions as SupportsPositionsPriv>::Rc: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.