Struct generational_cache::arena::Arena
source · pub struct Arena<V, T> { /* private fields */ }
Expand description
A generational arena for allocating memory based off a vector. Every entry is associated with a generation counter to uniquely identify newer allocations from older reclaimed allocations at the same position in the vector.
This is inspired from the crate “generational-arena”
Usage
#[no_std]
use generational_cache::prelude::*;
const CAPACITY: usize = 5;
let mut arena = Arena::<_, i32>::with_vector(Array::<_, CAPACITY>::new());
let index = arena.insert(78).unwrap(); // allocate new element in arena
let i_ref = arena.get(&index);
assert_eq!(i_ref, Some(&78));
let i_m_ref = arena.get_mut(&index).unwrap();
*i_m_ref = -68418;
assert_eq!(arena.get(&index), Some(&-68418));
arena.remove(&index).unwrap();
assert!(arena.get(&index).is_none());
Implementations§
source§impl<V, T> Arena<V, T>where
V: Vector<Entry<T>>,
impl<V, T> Arena<V, T>where V: Vector<Entry<T>>,
A generational arena.
sourcepub fn reserve(&mut self, additional: usize) -> Result<(), ArenaError<V::Error>>
pub fn reserve(&mut self, additional: usize) -> Result<(), ArenaError<V::Error>>
Reserves space for the given number of additional items in this arena.
sourcepub fn clear(&mut self) -> Result<(), ArenaError<V::Error>>
pub fn clear(&mut self) -> Result<(), ArenaError<V::Error>>
Removes all items from this arena and reclaims all allocated memory.
sourcepub fn with_vector(vector: V) -> Self
pub fn with_vector(vector: V) -> Self
sourcepub fn insert(&mut self, item: T) -> Result<Index, ArenaError<V::Error>>
pub fn insert(&mut self, item: T) -> Result<Index, ArenaError<V::Error>>
Allocates space for the given items and inserts it into this arena.
sourcepub fn remove(&mut self, index: &Index) -> Option<T>
pub fn remove(&mut self, index: &Index) -> Option<T>
Reclaims the allocated space for the item at the given index and removes it from the arena.
sourcepub fn get_mut(&mut self, index: &Index) -> Option<&mut T>
pub fn get_mut(&mut self, index: &Index) -> Option<&mut T>
Returns a mutable reference to the allocated items referenced by the given Index
.
sourcepub fn get(&self, index: &Index) -> Option<&T>
pub fn get(&self, index: &Index) -> Option<&T>
Returns an immutable reference to the allocated items referenced by the given Index
.