pub struct Slab<T> { /* private fields */ }
Expand description
A simple, uni-typed slab arena.
Implementations§
Source§impl<T> Slab<T>
impl<T> Slab<T>
Sourcepub const MAX_CAPACITY: usize = 4_294_967_294usize
pub const MAX_CAPACITY: usize = 4_294_967_294usize
The maximum capacity any Slab
can have: u32::MAX - 1
.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct a new, empty slab, pre-reserving space for at least capacity
elements.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensure that there is space for at least additional
elements in this
slab.
§Panics
Panics if the new capacity exceeds Self::MAX_CAPACITY
.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
What is the capacity of this slab? That is, how many entries can it contain within its current underlying storage?
Sourcepub fn try_alloc(&mut self, value: T) -> Result<Id, T>
pub fn try_alloc(&mut self, value: T) -> Result<Id, T>
Try to allocate a T
value within this slab.
If there is no available capacity, ownership of the given value is
returned via Err(value)
.
Sourcepub fn alloc(&mut self, value: T) -> Id
pub fn alloc(&mut self, value: T) -> Id
Allocate a T
value within this slab, allocating additional underlying
storage if there is no available capacity.
§Panics
Panics if allocating this value requires reallocating the underlying
storage, and the new capacity exceeds Slab::MAX_CAPACITY
.
Sourcepub fn next_id(&self) -> Id
pub fn next_id(&self) -> Id
Get the Id
that will be returned for the next allocation in this slab.
Sourcepub fn get(&self, id: Id) -> Option<&T>
pub fn get(&self, id: Id) -> Option<&T>
Get a shared borrow of the value associated with id
.
Returns None
if the value has since been deallocated.
If id
comes from a different Slab
instance, this method may panic,
return None
, or return an arbitrary value.
Sourcepub fn get_mut(&mut self, id: Id) -> Option<&mut T>
pub fn get_mut(&mut self, id: Id) -> Option<&mut T>
Get an exclusive borrow of the value associated with id
.
Returns None
if the value has since been deallocated.
If id
comes from a different Slab
instance, this method may panic,
return None
, or return an arbitrary value.
Sourcepub fn dealloc(&mut self, id: Id) -> T
pub fn dealloc(&mut self, id: Id) -> T
Deallocate the value associated with the given id
.
If id
comes from a different Slab
instance, this method may panic or
deallocate an arbitrary value.
Sourcepub fn iter(&self) -> impl Iterator<Item = (Id, &T)> + '_
pub fn iter(&self) -> impl Iterator<Item = (Id, &T)> + '_
Iterate over all values currently allocated within this Slab
.
Yields pairs of an Id
and the Id
’s associated value.
Iteration order is undefined.