Skip to main content

SlabArena

Struct SlabArena 

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

An arena with O(1) deallocation via a free list.

Freed slots are tracked and reused on the next allocation.

Implementations§

Source§

impl<T> SlabArena<T>

Source

pub fn new() -> Self

Create a new empty slab arena.

Source

pub fn with_capacity(cap: usize) -> Self

Create a slab arena with the given initial capacity.

Source

pub fn alloc(&mut self, value: T) -> Idx<T>

Allocate a value, reusing a freed slot if available.

Source

pub fn free(&mut self, idx: Idx<T>) -> Option<T>

Free the value at idx, returning it.

Returns None if the slot was already free.

Source

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

Get a reference to the value at idx.

Returns None if the slot is free.

Source

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

Get a mutable reference to the value at idx.

Source

pub fn len(&self) -> usize

Number of live (non-free) slots.

Source

pub fn capacity_slots(&self) -> usize

Total number of slots (including free ones).

Source

pub fn is_empty(&self) -> bool

Returns true if there are no live values.

Source

pub fn free_count(&self) -> usize

Number of slots on the free list.

Source

pub fn iter_live(&self) -> impl Iterator<Item = (Idx<T>, &T)>

Iterate over all live (non-free) (index, value) pairs.

Trait Implementations§

Source§

impl<T: Debug> Debug for SlabArena<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for SlabArena<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SlabArena<T>

§

impl<T> RefUnwindSafe for SlabArena<T>
where T: RefUnwindSafe,

§

impl<T> Send for SlabArena<T>
where T: Send,

§

impl<T> Sync for SlabArena<T>
where T: Sync,

§

impl<T> Unpin for SlabArena<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SlabArena<T>

§

impl<T> UnwindSafe for SlabArena<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.