pub struct BoundedSlab<T> { /* private fields */ }Expand description
Fixed-capacity slab allocator for manual memory management.
Uses pointer-based freelist for O(1) allocation. ~20-24 cycle operations.
§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<T> Slab<T>
impl<T> Slab<T>
Sourcepub unsafe fn with_capacity(capacity: usize) -> Slab<T>
pub unsafe fn with_capacity(capacity: usize) -> Slab<T>
Creates a new slab with the given capacity.
§Safety
See struct-level safety contract.
§Panics
Panics if capacity is zero.
Sourcepub fn claim(&self) -> Option<Claim<'_, T>>
pub fn claim(&self) -> Option<Claim<'_, T>>
Claims a slot from the freelist without writing a value.
Returns None if the slab is full. The returned Claim must be
consumed via Claim::write() to complete the allocation.
This two-phase allocation enables placement new optimization: the value can be constructed directly into the slot memory.
§Example
use nexus_slab::bounded::Slab;
// SAFETY: caller guarantees slab contract (see struct docs)
let slab = unsafe { Slab::with_capacity(10) };
if let Some(claim) = slab.claim() {
let slot = claim.write(42u64);
assert_eq!(*slot, 42);
slab.free(slot);
}Sourcepub fn try_alloc(&self, value: T) -> Result<Slot<T>, Full<T>>
pub fn try_alloc(&self, value: T) -> Result<Slot<T>, Full<T>>
Tries to allocate a slot and write the value.
Returns Err(Full(value)) if the slab is at capacity.