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>
impl<T> Arena<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates an empty arena that performs no allocation until first insert.
Sourcepub fn with_capacity(capacity: usize) -> Self
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.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of slots the arena can hold before reallocating.
This counts both occupied and vacant slots.
Sourcepub fn get(&self, idx: Index) -> Option<&T>
pub fn get(&self, idx: Index) -> Option<&T>
Returns a shared reference to the element behind idx, or None
if the handle is stale.
Sourcepub fn get_mut(&mut self, idx: Index) -> Option<&mut T>
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.
Sourcepub fn insert(&mut self, value: T) -> Index
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.
Sourcepub fn try_insert(&mut self, value: T) -> Result<Index>
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.
Sourcepub fn remove(&mut self, idx: Index) -> Option<T>
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 callSourcepub fn clear(&mut self)
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.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
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"]);