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>>,

A generational arena.

source

pub fn reserve(&mut self, additional: usize) -> Result<(), ArenaError<V::Error>>

Reserves space for the given number of additional items in this arena.

source

pub fn clear(&mut self) -> Result<(), ArenaError<V::Error>>

Removes all items from this arena and reclaims all allocated memory.

source

pub fn with_vector(vector: V) -> Self

Creates an Arena instance with the given Vector implemenation instance as the backing memory.

source

pub fn insert(&mut self, item: T) -> Result<Index, ArenaError<V::Error>>

Allocates space for the given items and inserts it into this arena.

source

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.

source

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

Returns a mutable reference to the allocated items referenced by the given Index.

source

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

Returns an immutable reference to the allocated items referenced by the given Index.

source

pub fn capacity(&self) -> usize

Returns the number of elements this Arena is capable of allocating.

source

pub fn is_empty(&self) -> bool

Returns whether this Arena is empty.

source

pub fn len(&self) -> usize

Returns the number of elements allocated in this Arena.

Auto Trait Implementations§

§

impl<V, T> RefUnwindSafe for Arena<V, T>where T: RefUnwindSafe, V: RefUnwindSafe,

§

impl<V, T> Send for Arena<V, T>where T: Send, V: Send,

§

impl<V, T> Sync for Arena<V, T>where T: Sync, V: Sync,

§

impl<V, T> Unpin for Arena<V, T>where T: Unpin, V: Unpin,

§

impl<V, T> UnwindSafe for Arena<V, T>where T: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.