Skip to main content

Slab

Struct Slab 

Source
pub struct Slab<'a, T: Pod + FixedLayout> { /* private fields */ }
Expand description

A fixed-size slab allocator over a byte slice.

Tracks slot occupancy with an inline bitmap. Double-free, reads of freed slots, and writes to freed slots are all rejected.

Implementations§

Source§

impl<'a, T: Pod + FixedLayout> Slab<'a, T>

Source

pub fn from_bytes_mut(data: &'a mut [u8]) -> Result<Self, ProgramError>

Parse a slab from a mutable byte slice.

Source

pub fn init(data: &mut [u8], capacity: usize) -> Result<(), ProgramError>

Initialize a slab with the given capacity.

Must be called on a zeroed buffer. Sets up the free list and clears the occupancy bitmap.

Source

pub fn count(&self) -> u32

Number of allocated slots.

Source

pub fn capacity(&self) -> usize

Total slot capacity.

Source

pub fn is_full(&self) -> bool

Whether the slab is full.

Source

pub fn is_slot_allocated(&self, index: u32) -> bool

Whether a slot index is currently allocated.

Source

pub fn alloc(&mut self, value: T) -> Result<u32, ProgramError>

Allocate a slot and write the value. Returns the slot index.

Source

pub fn free(&mut self, index: u32) -> Result<(), ProgramError>

Free a slot and return it to the free list.

Fails if the slot is not currently allocated (prevents double-free).

Source

pub fn get(&self, index: u32) -> Result<T, ProgramError>

Read a value from a slot (copy).

Fails if the slot is not allocated.

Source

pub fn get_ref(&self, index: u32) -> Result<&T, ProgramError>

Get a reference to a value in a slot.

Fails if the slot is not allocated.

Source

pub fn get_mut(&mut self, index: u32) -> Result<&mut T, ProgramError>

Get a mutable reference to a value in a slot.

Fails if the slot is not allocated.

Source

pub fn set(&mut self, index: u32, value: T) -> Result<(), ProgramError>

Write a value into an allocated slot.

Fails if the slot is not allocated.

Source

pub const fn required_bytes(capacity: usize) -> usize

Bytes required for a slab of given capacity.

Auto Trait Implementations§

§

impl<'a, T> Freeze for Slab<'a, T>

§

impl<'a, T> RefUnwindSafe for Slab<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Slab<'a, T>
where T: Send,

§

impl<'a, T> Sync for Slab<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Slab<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for Slab<'a, T>

§

impl<'a, T> !UnwindSafe for Slab<'a, T>

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.