pub struct SlabAlloc<T, IndexType = u32, IdType = u32> {
    data: Vec<Element<IdType, T>>,
    free_indices: Vec<IndexType>,
    len: usize,
}
Expand description

Container which efficiently allocates space for its elements.

Fields§

§data: Vec<Element<IdType, T>>§free_indices: Vec<IndexType>§len: usize

Number of elements currently in the map.

Implementations§

source§

impl<T, IndexType, IdType> SlabAlloc<T, IndexType, IdType>where IndexType: PrimInt + Unsigned + ToPrimitive + FromPrimitive, IdType: PrimInt + Unsigned,

source

pub fn new() -> Self

Create an empty container.

source

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

Access an element.

source

pub fn get_mut(&mut self, index: SlabIndex<IndexType, IdType>) -> Option<&mut T>

Access an element.

source

pub fn contains_key(&self, index: SlabIndex<IndexType, IdType>) -> bool

source

pub fn insert(&mut self, value: T) -> SlabIndex<IndexType, IdType>

Insert an element and return the index of it.

Panics

Panics when the amount of indices is exhausted. This happens when there are already IndexType::max_value() elements in the container.

source

pub fn reserve(&mut self, n: usize)

Allocate space to hold n elements.

source

pub fn remove(&mut self, index: SlabIndex<IndexType, IdType>) -> Option<T>

Remove an element.

source

pub fn len(&self) -> usize

source

pub fn is_empty(&self) -> bool

source

pub fn shrink(&mut self)

Reclaim as much space as possible.

source

pub fn clear(&mut self)

Drop all data.

source

pub fn iter(&self) -> impl Iterator<Item = (SlabIndex<IndexType, IdType>, &T)>

Iterate over all key-value pairs.

source

pub fn iter_mut( &mut self ) -> impl Iterator<Item = (SlabIndex<IndexType, IdType>, &mut T)>

Iterate over all key-value pairs.

source

pub fn values(&self) -> impl Iterator<Item = &T>

Iterate over all values in the map.

source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T>

Iterate over all mutable values in the map.

source

pub fn keys(&self) -> impl Iterator<Item = SlabIndex<IndexType, IdType>> + '_

Iterate over all keys in the map.

Trait Implementations§

source§

impl<T: Clone, IndexType: Clone, IdType: Clone> Clone for SlabAlloc<T, IndexType, IdType>

source§

fn clone(&self) -> SlabAlloc<T, IndexType, IdType>

Returns a copy 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, IndexType: Debug, IdType: Debug> Debug for SlabAlloc<T, IndexType, IdType>

source§

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

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

impl<T: Default, IndexType: Default, IdType: Default> Default for SlabAlloc<T, IndexType, IdType>

source§

fn default() -> SlabAlloc<T, IndexType, IdType>

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

impl<'de, T, IndexType, IdType> Deserialize<'de> for SlabAlloc<T, IndexType, IdType>where T: Deserialize<'de>, IndexType: Deserialize<'de>, IdType: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T, IndexType, IdType> Serialize for SlabAlloc<T, IndexType, IdType>where T: Serialize, IndexType: Serialize, IdType: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T, IndexType, IdType> RefUnwindSafe for SlabAlloc<T, IndexType, IdType>where IdType: RefUnwindSafe, IndexType: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, IndexType, IdType> Send for SlabAlloc<T, IndexType, IdType>where IdType: Send, IndexType: Send, T: Send,

§

impl<T, IndexType, IdType> Sync for SlabAlloc<T, IndexType, IdType>where IdType: Sync, IndexType: Sync, T: Sync,

§

impl<T, IndexType, IdType> Unpin for SlabAlloc<T, IndexType, IdType>where IdType: Unpin, IndexType: Unpin, T: Unpin,

§

impl<T, IndexType, IdType> UnwindSafe for SlabAlloc<T, IndexType, IdType>where IdType: UnwindSafe, IndexType: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,