pub struct Slab<T: Sized> { /* private fields */ }Expand description
Simple slab allocator. Stores items of the same type and can reuse removed indexes.
§Example
let mut s = Slab::new();
let index = s.insert(5);
assert_eq!(s.get(index), Some(&5));
assert_eq!(s.remove(index), Some(5));
assert_eq!(s.get(index), None);Implementations§
Source§impl<T: Sized> Slab<T>
impl<T: Sized> Slab<T>
Sourcepub fn insert(&mut self, item: T) -> SlabIndex
pub fn insert(&mut self, item: T) -> SlabIndex
Inserts an item into the slab and returns its index.
Will reuse an empty index if one is available.
Sourcepub fn get_mut(&mut self, index: SlabIndex) -> Option<&mut T>
pub fn get_mut(&mut self, index: SlabIndex) -> Option<&mut T>
Returns a mutable reference to the item at index.
Returns None if index has been removed.
Sourcepub fn get(&self, index: SlabIndex) -> Option<&T>
pub fn get(&self, index: SlabIndex) -> Option<&T>
Return a reference to the item at index.
Returns None if index has been removed.
Sourcepub fn remove(&mut self, index: SlabIndex) -> Option<T>
pub fn remove(&mut self, index: SlabIndex) -> Option<T>
Removes an item from the Slab and returns it.
Returns None if index has been removed.
index will be reused on the next call to Slab::insert.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in the slab.
This is different from the number of allocated slots in the slab, see Slab::total_len
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the number of items in the slab is 0.
This is different from the number of allocated slots in the slab, see Slab::total_len
Sourcepub fn total_len(&self) -> usize
pub fn total_len(&self) -> usize
Returns the number of allocated slots in the slab, some of them could be empty.
Sourcepub unsafe fn get_very_unsafely(&self, index: SlabIndex) -> &T
pub unsafe fn get_very_unsafely(&self, index: SlabIndex) -> &T
Returns the item at index without performing bounds checking or checking if the slot contains initialized data.
§Safety
This function is safe if index < Slab::total_len()
and the item at index has not been removed.
Will panic in debug mode if the invariants are broken.
Annoyingly long names discourage use and make you really think about what you are doing.