pub struct UnboundedSlab<T> { /* private fields */ }Expand description
Growable slab allocator.
Uses independent chunks for growth — no copying when the slab grows.
Construction is unsafe — by creating a slab, you accept the contract:
- Free everything you allocate. Dropping the slab does NOT drop values in occupied slots. Unfree’d slots leak silently.
- Free from the same slab. Passing a
Slotto a different slab’sfree()corrupts the freelist. - Don’t share across threads. The slab is
!Sendand!Sync.
§Const Construction
use nexus_slab::unbounded::Slab;
struct MyType(u64);
// SAFETY: single slab per type, freed before thread exit
thread_local! {
static SLAB: Slab<MyType> = const { unsafe { Slab::new() } };
}
SLAB.with(|s| unsafe { s.init(4096) });For direct usage, prefer with_chunk_capacity().
Implementations§
Source§impl<T> Slab<T>
impl<T> Slab<T>
Sourcepub const unsafe fn new() -> Slab<T>
pub const unsafe fn new() -> Slab<T>
Creates an empty, uninitialized slab.
This is a const function that performs no allocation. Call init()
to configure chunk capacity before use.
For direct usage, prefer with_chunk_capacity().
§Safety
See struct-level safety contract.
§Example
use nexus_slab::unbounded::Slab;
// SAFETY: single slab per type, freed before thread exit
thread_local! {
static SLAB: Slab<u64> = const { unsafe { Slab::new() } };
}Sourcepub unsafe fn with_chunk_capacity(chunk_capacity: usize) -> Slab<T>
pub unsafe fn with_chunk_capacity(chunk_capacity: usize) -> Slab<T>
Creates a new slab with the given chunk capacity.
Chunks are allocated on-demand when slots are requested.
§Panics
§Safety
See struct-level safety contract.
§Panics
Panics if chunk_capacity is zero.
Sourcepub unsafe fn init(&self, chunk_capacity: usize)
pub unsafe fn init(&self, chunk_capacity: usize)
Initializes the slab with the given chunk capacity.
This configures the chunk parameters. Chunks are allocated on-demand when slots are requested. Must be called exactly once before any allocations.
§Safety
See struct-level safety contract.
§Panics
- Panics if the slab is already initialized (chunk_capacity > 0)
- Panics if chunk_capacity is zero
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Returns true if the slab has been initialized.
Sourcepub fn chunk_capacity(&self) -> usize
pub fn chunk_capacity(&self) -> usize
Returns the chunk capacity.
Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Returns the number of allocated chunks.
Sourcepub fn reserve_chunks(&self, count: usize)
pub fn reserve_chunks(&self, count: usize)
Ensures at least count chunks are allocated.
No-op if the slab already has count or more chunks. Only allocates
the difference.
Sourcepub fn claim(&self) -> Claim<'_, T>
pub fn claim(&self) -> Claim<'_, T>
Claims a slot from the freelist without writing a value.
Always succeeds — grows the slab if needed. 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::unbounded::Slab;
// SAFETY: caller guarantees slab contract (see struct docs)
let slab = unsafe { Slab::with_chunk_capacity(16) };
let claim = slab.claim();
let slot = claim.write(42u64);
assert_eq!(*slot, 42);
slab.free(slot);Sourcepub fn alloc(&self, value: T) -> Slot<T>
pub fn alloc(&self, value: T) -> Slot<T>
Allocates a slot and writes the value.
Always succeeds — grows the slab if needed.