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>
impl<T> SlotMap<'_, T>
Sourcepub fn reserve(&mut self) -> Option<(Key, &mut T)>
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.
Sourcepub fn insert(&mut self, value: T) -> Option<Key>
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.