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
Slotto a different slab’sfree()is undefined behavior — it corrupts the freelist. In debug builds, this is caught bydebug_assert!. - Free everything you allocate. Dropping the slab does NOT drop values in occupied slots. Unfreed slots leak silently.
- Single-threaded. The slab is
!Sendand!Sync.
§Why free() is safe
The safety contract is accepted once, at construction. After that:
Slotis move-only (noCopy, noClone) — double-free is prevented by the type system.free()consumes theSlot— 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>
impl<const N: usize> Slab<N>
Sourcepub unsafe fn with_chunk_capacity(chunk_capacity: usize) -> Slab<N>
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.
Sourcepub fn alloc<T>(&self, value: T) -> Slot<T>
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
Sourcepub fn claim(&self) -> ByteClaim<'_>
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.
Sourcepub unsafe fn free_raw_in_chunk(&self, ptr: *mut u8, chunk_idx: usize)
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
ptrmust point to a slot in chunkchunk_idxof this slab.