Skip to main content

Arena

Struct Arena 

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

Generational arena. See the module-level docs.

§Examples

use arena_lib::Arena;

let mut arena = Arena::new();
let a = arena.insert("alpha");
let b = arena.insert("bravo");

assert_eq!(arena.len(), 2);
assert_eq!(arena.get(a), Some(&"alpha"));

let removed = arena.remove(a);
assert_eq!(removed, Some("alpha"));
assert!(arena.get(a).is_none()); // stale handle
assert_eq!(arena.get(b), Some(&"bravo"));

Implementations§

Source§

impl<T> Arena<T>

Source

pub const fn new() -> Self

Creates an empty arena that performs no allocation until first insert.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty arena with space pre-reserved for capacity elements.

The arena will still grow on demand; this is a hint, not a hard cap.

Source

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

Reserves capacity for at least additional more elements.

Source

pub const fn len(&self) -> usize

Number of live elements currently in the arena.

Source

pub const fn is_empty(&self) -> bool

Returns true when the arena holds no live elements.

Source

pub fn capacity(&self) -> usize

Returns the number of slots the arena can hold before reallocating.

This counts both occupied and vacant slots.

Source

pub fn contains(&self, idx: Index) -> bool

Returns true if the index refers to a live element.

Source

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

Returns a shared reference to the element behind idx, or None if the handle is stale.

Source

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

Returns a unique reference to the element behind idx, or None if the handle is stale.

Source

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

Inserts value and returns a fresh Index.

Panics on the catastrophic case where the slot counter would overflow u32::MAX. Use Arena::try_insert for an explicit fallible variant.

Source

pub fn try_insert(&mut self, value: T) -> Result<Index>

Inserts value, returning an Index on success or Error::CounterOverflow if the arena cannot represent more slots.

Source

pub fn remove(&mut self, idx: Index) -> Option<T>

Removes the element behind idx and returns it, or None if the handle is stale.

The slot’s generation counter advances so the consumed handle — and any other handle previously issued for the same slot — never resolves again, even if the slot is reused by a later insert.

§Examples
use arena_lib::Arena;

let mut arena = Arena::new();
let h = arena.insert("payload");

assert_eq!(arena.remove(h), Some("payload"));
assert_eq!(arena.remove(h), None); // stale on every subsequent call
Source

pub fn clear(&mut self)

Removes every element and resets the free list.

Underlying capacity is retained. Generation counters are preserved, so handles issued before the clear remain stale afterwards.

Source

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

Returns an iterator over (Index, &T) for every live element.

Vacant slots are skipped; iteration order follows slot indices, which is not the insertion order if any removals have happened.

§Examples
use arena_lib::Arena;

let mut arena = Arena::new();
let _a = arena.insert("alpha");
let killed = arena.insert("dead");
let _b = arena.insert("bravo");
arena.remove(killed);

let live: Vec<&&str> = arena.iter().map(|(_idx, value)| value).collect();
assert_eq!(live, vec![&"alpha", &"bravo"]);
Source

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

Returns an iterator over (Index, &mut T) for every live element.

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

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.