Skip to main content

SharedArena

Struct SharedArena 

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

Thread-safe typed arena allocator.

Concurrent allocation via &self. Wait-free reads returning &T directly (no guards or locks). Same Idx<T> handles and Checkpoint<T> semantics as Arena.

SharedArena<T> is Send + Sync when T: Send + Sync.

For single-thread usage with zero overhead, see Arena.

Implementations§

Source§

impl<T> SharedArena<T>

Source

pub const fn new() -> Self

Creates an empty shared arena.

Source

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

Allocates a value, returning its stable index.

Can be called concurrently from multiple threads (&self). O(1).

§Panics

Panics if internal slot reservation fails (should not happen in normal usage).

Source

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

Returns a reference to the value at idx.

Wait-free. Returns &T directly (no guard or lock).

§Panics

Panics if idx is out of bounds (stale after rollback/reset).

Source

pub fn len(&self) -> usize

Returns the number of published (visible) items.

Source

pub fn is_empty(&self) -> bool

Returns true if the arena contains no items.

Source

pub 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 storage for reuse.

Source

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

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

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 alloc_extend(&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 fn iter(&self) -> SharedArenaIter<'_, T>

Returns an iterator over all published items in allocation order.

Source

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

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

Source

pub fn drain(&mut self) -> IntoIter<T>

Removes all items, returning an iterator that yields them in allocation order.

The arena is empty after the iterator is consumed or dropped.

Trait Implementations§

Source§

impl<T> Default for SharedArena<T>

Source§

fn default() -> Self

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

impl<T> Extend<T> for SharedArena<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 SharedArena<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 SharedArena<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<'a, T> IntoIterator for &'a SharedArena<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = SharedArenaIter<'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 SharedArena<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 SharedArena<T>

§

impl<T> RefUnwindSafe for SharedArena<T>

§

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

§

impl<T> Sync for SharedArena<T>
where T: Sync + Send,

§

impl<T> Unpin for SharedArena<T>

§

impl<T> UnsafeUnpin for SharedArena<T>

§

impl<T> UnwindSafe for SharedArena<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.