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>
impl<V, T> Arena<V, 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.
pub fn get_mut(&mut self, index: &Index) -> Option<&mut T>
pub fn get(&self, index: &Index) -> Option<&T>
pub fn capacity(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn len(&self) -> usize
Auto Trait Implementations§
impl<V, T> RefUnwindSafe for Arena<V, T>where
T: RefUnwindSafe,
V: RefUnwindSafe,
impl<V, T> Send for Arena<V, T>
impl<V, T> Sync for Arena<V, T>
impl<V, T> Unpin for Arena<V, T>
impl<V, T> UnwindSafe for Arena<V, T>where
T: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more