Struct slabby::Slab

source ·
pub struct Slab<T, K: Key> { /* private fields */ }
Expand description

A Slab which can hold some number of elements, depending on the chosen K.

§Examples

let mut slab = slabby::Slab32::new();
unsafe {
    let key1 = slab.insert(1);
    let key2 = slab.insert(2);
    let key3 = slab.insert(3);

    assert_eq!(slab.get(key1), &1);
    assert_eq!(slab.get(key2), &2);
    assert_eq!(slab.get(key3), &3);

    assert_eq!(slab.remove(key2), 2);
    assert_eq!(slab.remove(key1), 1);

    assert_eq!(slab.get(key3), &3);

    slab.insert(4);
    let key5 = slab.insert(5);
    slab.insert(6);

    assert_eq!(slab.len(), 4);

    *slab.get_mut(key5) += 1;
    assert_eq!(slab.remove(key5), 6);

    assert_eq!(slab.len(), 3);
}

Implementations§

source§

impl<T, K: Key> Slab<T, K>

source

pub fn new() -> Self

Create a new Slab. No allocations will occur until the first Slab::insert.

source

pub unsafe fn insert(&mut self, val: T) -> K

§Safety

The number of occupied slots must be lower than the maximum value of K. This is trivially true if the maximum value of K is greater or equal to that of usize.

source

pub unsafe fn remove(&mut self, key: K) -> T

Remove a previously inserted element from the Slab. Returns the contained T.

§Safety

The provided key must have been obtained from this instance of Slab and not removed between the insertion and this call.

source

pub unsafe fn get(&self, key: K) -> &T

§Safety

The provided key must have been obtained from this instance of Slab and not removed between the insertion and this call.

source

pub unsafe fn get_mut(&mut self, key: K) -> &mut T

§Safety

The provided key must have been obtained from this instance of Slab and not removed between the insertion and this call.

source

pub fn next(&self) -> K

Get the key of the next element to be inserted into this Slab.

source

pub fn len(&self) -> K

Get the number of elements contained within this Slab.

Trait Implementations§

source§

impl<T, K: Key> Debug for Slab<T, K>

source§

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

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

impl<T, K: Key> Default for Slab<T, K>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, K> Freeze for Slab<T, K>
where K: Freeze,

§

impl<T, K> RefUnwindSafe for Slab<T, K>

§

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

§

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

§

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

§

impl<T, K> UnwindSafe for Slab<T, K>
where K: UnwindSafe, 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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.