pub struct UnboundedSlab<T> { /* private fields */ }Expand description
Growable slab allocator.
Uses independent chunks for growth — no copying when the slab grows.
§Drop Behavior
Dropping a Slab does not drop values in occupied slots. SlotCell is
a union, so the compiler cannot know which slots contain live values. The
caller must free all outstanding RawSlot handles before dropping the
slab, or the values will be leaked.
This is acceptable for the primary use case (TLS via thread_local!) where
thread exit leaks all TLS storage anyway.
§Const Construction
Supports const construction via new() followed by
runtime initialization via init(). This enables use with
thread_local! using the const { } block syntax for zero-overhead TLS access.
thread_local! {
static SLAB: Slab<MyType> = const { Slab::new() };
}
// Later, at runtime:
SLAB.with(|s| s.init(4096));For direct usage, prefer with_chunk_capacity().
Implementations§
Source§impl<const N: usize> Slab<AlignedBytes<N>>
impl<const N: usize> Slab<AlignedBytes<N>>
Sourcepub unsafe fn remove<T>(&self, slot: Slot<T>)where
T: ?Sized,
pub unsafe fn remove<T>(&self, slot: Slot<T>)where
T: ?Sized,
Drops the value and frees the slot.
Handles both thin and fat pointers: extracts the data pointer for
freeing regardless of whether T is Sized or dyn Trait.
§Safety
slotmust have been allocated from this slab
Sourcepub unsafe fn take_value<T>(&self, slot: Slot<T>) -> T
pub unsafe fn take_value<T>(&self, slot: Slot<T>) -> T
Extracts the value and frees the slot (Sized only).
§Safety
slotmust have been allocated from this slab
Source§impl<T> Slab<T>
impl<T> Slab<T>
Sourcepub const fn new() -> Slab<T>
pub const 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().
§Example
// For use with thread_local! const initialization
thread_local! {
static SLAB: Slab<u64> = const { Slab::new() };
}Sourcepub fn with_chunk_capacity(chunk_capacity: usize) -> Slab<T>
pub 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
Panics if chunk_capacity is zero.
Sourcepub fn init(&self, chunk_capacity: usize)
pub 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.
§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;
let slab = Slab::with_chunk_capacity(16);
let claim = slab.claim();
let slot = claim.write(42u64);
assert_eq!(*slot, 42);
// SAFETY: slot was allocated from this slab
unsafe { slab.free(slot) };Sourcepub fn alloc(&self, value: T) -> RawSlot<T>
pub fn alloc(&self, value: T) -> RawSlot<T>
Allocates a slot and writes the value.
Always succeeds — grows the slab if needed.