Skip to main content

GenerationalSlab

Struct GenerationalSlab 

Source
pub struct GenerationalSlab<T, B: Allocator + FixedRange, G: GenerationInt = u32> { /* private fields */ }
Expand description

Generational slab.

capacity slots, each holding either a live T or a free-list link. Handles encode (slot_index, generation_at_allocate); access via get / get_mut / remove only succeeds when the slot’s current generation equals the handle’s.

§Thread safety

All mutation goes through &mut self. The slab is Send + Sync if T and B are, but concurrent mutation requires external synchronization (Mutex<GenerationalSlab<T, B>>).

Implementations§

Source§

impl<T, B: Allocator + FixedRange> GenerationalSlab<T, B, u32>

Source

pub fn new(capacity: usize, backing: B) -> Result<Self, AllocError>

Construct with default u32 generation counter.

Source§

impl<T, B: Allocator + FixedRange, G: GenerationInt> GenerationalSlab<T, B, G>

Source

pub fn with_generation(capacity: usize, backing: B) -> Result<Self, AllocError>

Construct with an explicit generation-width parameter.

Use u64 for high-churn long-running servers where 2^32 reuses of the same slot could realistically occur.

Source

pub fn capacity(&self) -> usize

Number of slots.

Source

pub fn insert(&mut self, value: T) -> Result<Handle<T, G>, AllocError>

Insert a value and return its handle. Errors if the slab is full.

Source

pub fn get(&self, handle: Handle<T, G>) -> Option<&T>

Borrow the value behind a handle, or None if stale / vacant.

Source

pub fn get_mut(&mut self, handle: Handle<T, G>) -> Option<&mut T>

Borrow mutably behind a handle, or None if stale / vacant.

Source

pub fn remove(&mut self, handle: Handle<T, G>) -> Option<T>

Remove the value behind a handle and return it, or None if stale. Bumps the slot’s generation so the handle (and any copies) become invalid.

Source

pub fn contains(&self, handle: Handle<T, G>) -> bool

True if the handle would currently access a live value.

Trait Implementations§

Source§

impl<T, B: Allocator + FixedRange, G: GenerationInt> Drop for GenerationalSlab<T, B, G>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Send, B: Allocator + FixedRange + Send, G: GenerationInt + Send> Send for GenerationalSlab<T, B, G>

Source§

impl<T: Sync, B: Allocator + FixedRange + Sync, G: GenerationInt + Sync> Sync for GenerationalSlab<T, B, G>

Auto Trait Implementations§

§

impl<T, B, G> Freeze for GenerationalSlab<T, B, G>
where B: Freeze,

§

impl<T, B, G> RefUnwindSafe for GenerationalSlab<T, B, G>

§

impl<T, B, G> Unpin for GenerationalSlab<T, B, G>
where B: Unpin, G: Unpin,

§

impl<T, B, G> UnsafeUnpin for GenerationalSlab<T, B, G>
where B: UnsafeUnpin,

§

impl<T, B, G> UnwindSafe for GenerationalSlab<T, B, G>

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.