managed

Struct SlotMap

Source
pub struct SlotMap<'a, T> { /* private fields */ }
Expand description

Provides a slotmap based on external memory.

A slotmap provides a Vec-like interface where each entry is associated with a stable index-like key. Lookup with the key will detect if an entry has been removed but does not require a lifetime relation. Compared to other slotmap implementations this does not internally allocate any memory on its own but only relies on the Slice arguments in the constructor.

§Usage

The important aspect is that the slotmap does not create the storage of its own elements, it merely manages one given to it at construction time.


let mut elements = [0usize; 1024];
let mut slots = [SlotIndex::default(); 1024];

let mut map = SlotMap::new(
    ManagedSlice::Borrowed(&mut elements[..]),
    ManagedSlice::Borrowed(&mut slots[..]));
let index = map.insert(42).unwrap();
assert_eq!(map.get(index).cloned(), Some(42));

Implementations§

Source§

impl<T> SlotMap<'_, T>

Source

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

Retrieve a value by index.

Source

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

Retrieve a mutable value by index.

Source

pub fn reserve(&mut self) -> Option<(Key, &mut T)>

Reserve a new entry.

In case of success, the returned key refers to the entry until it is removed. The entry itself is not initialized with any particular value but instead retains the value it had in the backing slice. It is only logically placed into the slot map.

Source

pub fn insert(&mut self, value: T) -> Option<Key>

Try to insert a value into the map.

This will fail if there is not enough space. Sugar wrapper around reserve for inserting values. Note that on success, an old value stored in the backing slice will be overwritten. Use reserve directly if it is vital that no old value is dropped.

Source

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

Remove an element.

If successful, return a mutable reference to the removed element so that the caller can swap it with a logically empty value. Returns None if the provided index did not refer to an element that could be freed.

Source§

impl<'a, T> SlotMap<'a, T>

Source

pub fn new(elements: Slice<'a, T>, slots: Slice<'a, Slot>) -> Self

Create a slot map.

The capacity is the minimum of the capacity of the element and slot slices.

Auto Trait Implementations§

§

impl<'a, T> Freeze for SlotMap<'a, T>

§

impl<'a, T> RefUnwindSafe for SlotMap<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for SlotMap<'a, T>
where T: Send,

§

impl<'a, T> Sync for SlotMap<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for SlotMap<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for SlotMap<'a, T>

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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.
§

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

Performs the conversion.