pub struct LinkedSlab<T> {
entries: Vec<Entry<T>>,
next_free: NonZeroU32,
}Expand description
A slab like structure that also maintains circular lists with its items.
Fields§
§entries: Vec<Entry<T>>§next_free: NonZeroU32Implementations§
Source§impl<T> LinkedSlab<T>
impl<T> LinkedSlab<T>
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn insert(&mut self, item: T) -> NonZeroU32
pub fn insert(&mut self, item: T) -> NonZeroU32
Inserts a new entry in the list. Initially, the item will belong to a list only containing itself.
§Panics
Panics if number of items exceed u32::MAX - 1.
Sourcepub fn get(&self, index: NonZeroU32) -> Option<(&T, NonZeroU32)>
pub fn get(&self, index: NonZeroU32) -> Option<(&T, NonZeroU32)>
Gets an entry and a token to the next entry.
Sourcepub fn get_mut(&mut self, index: NonZeroU32) -> Option<(&mut T, NonZeroU32)>
pub fn get_mut(&mut self, index: NonZeroU32) -> Option<(&mut T, NonZeroU32)>
Gets an entry and a token to the next entry.
Sourcepub unsafe fn get_unchecked(&self, index: NonZeroU32) -> (&T, NonZeroU32)
pub unsafe fn get_unchecked(&self, index: NonZeroU32) -> (&T, NonZeroU32)
Gets an entry and a token to the next entry w/o checking, thus unsafe.
Sourcepub unsafe fn get_mut_unchecked(
&mut self,
index: NonZeroU32,
) -> (&mut T, NonZeroU32)
pub unsafe fn get_mut_unchecked( &mut self, index: NonZeroU32, ) -> (&mut T, NonZeroU32)
Gets an entry and a token to the next entry w/o checking, thus unsafe.
Sourcepub fn link(
&mut self,
idx: NonZeroU32,
target_head: Option<NonZeroU32>,
) -> NonZeroU32
pub fn link( &mut self, idx: NonZeroU32, target_head: Option<NonZeroU32>, ) -> NonZeroU32
Links an entry before target_head. Returns the item next to the linked item,
which is either the item itself or target_head.
§Panics
Panics on out of bounds access. Panics (in debug mode) if linking an absent entry.
Sourcepub fn unlink(&mut self, idx: NonZeroU32) -> Option<NonZeroU32>
pub fn unlink(&mut self, idx: NonZeroU32) -> Option<NonZeroU32>
Unlinks an entry without removing it from the ring Returns next item in the list, if not self.
§Panics
Panics on out of bounds access. Panics (in debug mode) if unlinking an absent entry.
Sourcepub fn remove(&mut self, idx: NonZeroU32) -> Option<(T, Option<NonZeroU32>)>
pub fn remove(&mut self, idx: NonZeroU32) -> Option<(T, Option<NonZeroU32>)>
Unlinks and removes the entry from the ring. Returns next item in the list, if not self.
§Panics
Panics on out of bounds access.
Sourcepub fn next_free(&self) -> NonZeroU32
pub fn next_free(&self) -> NonZeroU32
The Token that will be returned by the next call to insert()
Sourcepub fn drain(&mut self) -> impl Iterator<Item = T> + '_
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_
Drains all items from the slab.
The slab will be emptied even if the returned iterator isn’t fully consumed.
Sourcepub fn iter_from(
&self,
continuation: Option<NonZeroU32>,
) -> impl Iterator<Item = (NonZeroU32, &T)> + '_
pub fn iter_from( &self, continuation: Option<NonZeroU32>, ) -> impl Iterator<Item = (NonZeroU32, &T)> + '_
Iterator for the items in the slab, starting after a given token.
Sourcepub fn memory_used(&self) -> usize
pub fn memory_used(&self) -> usize
Get the memory used by LinkedSlab
It should be noted that if cache key or value is some type like Vec<T>,
the memory allocated in the heap will not be counted.
Trait Implementations§
Source§impl<T: Clone> Clone for LinkedSlab<T>
impl<T: Clone> Clone for LinkedSlab<T>
Source§fn clone(&self) -> LinkedSlab<T>
fn clone(&self) -> LinkedSlab<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more