Skip to main content

Slab

Struct Slab 

Source
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>

Source

pub fn new() -> Self

Returns an empty Slab.

Source

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.

Source

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.

Source

pub fn get(&self, index: SlabIndex) -> Option<&T>

Return a reference to the item at index.

Returns None if index has been removed.

Source

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.

Source

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

Source

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

Source

pub fn total_len(&self) -> usize

Returns the number of allocated slots in the slab, some of them could be empty.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over pairs of (SlabIndex, [&T]).

Source

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.

Trait Implementations§

Source§

impl<T: Clone + Sized> Clone for Slab<T>

Source§

fn clone(&self) -> Slab<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Sized> Debug for Slab<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Slab<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> IntoIterator for Slab<T>

Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

type Item = (SlabIndex, T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Slab<T>

§

impl<T> RefUnwindSafe for Slab<T>
where T: RefUnwindSafe,

§

impl<T> Send for Slab<T>
where T: Send,

§

impl<T> Sync for Slab<T>
where T: Sync,

§

impl<T> Unpin for Slab<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Slab<T>

§

impl<T> UnwindSafe for Slab<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.