Skip to main content

ByteSlab

Struct ByteSlab 

Source
pub struct ByteSlab<const N: usize> { /* private fields */ }
Expand description

Growable byte slab. Mirrors crate::unbounded::Slab but stores heterogeneous types in fixed-size byte slots.

Grows via independent chunks — no copying, no reallocation of existing slots. Pointers remain valid.

§Safety Contract

Construction is unsafe because it opts you into manual memory management. By creating a slab, you accept these invariants:

  • Free from the correct slab. Passing a Slot to a different slab’s free() is undefined behavior — it corrupts the freelist. In debug builds, this is caught by debug_assert!.
  • Free everything you allocate. Dropping the slab does NOT drop values in occupied slots. Unfreed slots leak silently.
  • Single-threaded. The slab is !Send and !Sync.

§Why free() is safe

The safety contract is accepted once, at construction. After that:

  • Slot is move-only (no Copy, no Clone) — double-free is prevented by the type system.
  • free() consumes the Slot — the handle cannot be used after.
  • Cross-slab misuse is the only remaining hazard, and it was accepted as the caller’s responsibility at construction time.

Implementations§

Source§

impl<const N: usize> Slab<N>

Source

pub unsafe fn with_chunk_capacity(chunk_capacity: usize) -> Slab<N>

Creates a new unbounded byte slab with the given chunk capacity.

§Safety

See struct-level safety contract.

§Panics

Panics if chunk_capacity is zero.

Source

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

Allocates a value. Never fails — grows if needed.

§Panics
  • Panics if size_of::<T>() > N
  • Panics if align_of::<T>() > 8
Source

pub fn claim(&self) -> ByteClaim<'_>

Reserve a slot without writing. Always succeeds (grows if needed).

The returned super::ByteClaim can be written to with .write(value) or .write_raw(src, size). If dropped without writing, the slot is returned to the freelist.

Source

pub unsafe fn free_raw(&self, ptr: *mut u8)

Free a raw pointer without dropping content.

§Safety

ptr must point to a slot in this slab.

Source

pub unsafe fn free_raw_in_chunk(&self, ptr: *mut u8, chunk_idx: usize)

Free a raw pointer with known chunk index. O(1) — no linear scan.

§Safety
  • ptr must point to a slot in chunk chunk_idx of this slab.
Source

pub unsafe fn alloc_raw(&self, src: *const u8, size: usize) -> *mut u8

Claim a slot and copy raw bytes into it. Returns a raw pointer.

§Safety
  • src must point to size valid bytes.
  • size must be <= N.
§Panics
  • Panics if size > N.
Source

pub fn free<T>(&self, ptr: Slot<T>)

Frees a value, dropping it and returning the slot to the freelist.

Consumes the handle — the slot cannot be used after this call.

Source

pub fn take<T>(&self, ptr: Slot<T>) -> T

Takes the value out without dropping it, freeing the slot.

Consumes the handle — the slot cannot be used after this call.

Trait Implementations§

Source§

impl<const N: usize> Debug for Slab<N>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize> !Freeze for Slab<N>

§

impl<const N: usize> !RefUnwindSafe for Slab<N>

§

impl<const N: usize> !Send for Slab<N>

§

impl<const N: usize> !Sync for Slab<N>

§

impl<const N: usize> Unpin for Slab<N>

§

impl<const N: usize> UnsafeUnpin for Slab<N>

§

impl<const N: usize> UnwindSafe for Slab<N>

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.