pub(crate) struct SlotMap<K, V>where
K: Key,{
pub(crate) base: SlotMap<K, Entry<V>>,
pub(crate) n_unusable: usize,
pub(crate) _valid: SlotMapValidationToken,
}
Expand description
A variation of
slotmap::SlotMap
that can never give the same key for multiple objects.
Unlike a regular version of
SlotMap
,
this version will not allow a slot’s version counter to roll over to
0 if it reaches 2^31. Instead, it will mark the slot as unusable for future values.
§Limitations
The possibility of marking a slot as unusable
makes it possible, given enough removals and re-insertions,
for a slotmap to use an unbounded amount of memory, even if it is not storing much actual data.
(From a DOS point of view: Given the ability to re-insert an entry ~2^31 times, an attacker can
cause a slot-map to render approximately 4+sizeof(V)
bytes unusable.)
This type does not include implementations for:
get_unchecked_mut()
get_disjoint_unchecked_mut()
IntoIterator
.serde::{Serialize, Deserialize}
.
§Risky business!
This code relies upon stability of some undocumented properties of slotmap
keys.
In particular, it assumes:
- that the slotmap KeyData
serde
format is stable, - that slot versions are represented as
u32
. - that the least significant bit of a slot version is 1 if the slot is full, and 0 if the slot is empty.
- that slot versions start at 0, and increase monotonically as the slot is emptied and reused.
Note that these assumptions are probably okay: if slotmap
were to change them,
it would thereby create a breaking change in its serde version.
Fields§
§base: SlotMap<K, Entry<V>>
§n_unusable: usize
§_valid: SlotMapValidationToken
Implementations§
Source§impl<V> SlotMap<DefaultKey, V>
impl<V> SlotMap<DefaultKey, V>
Sourcepub fn new() -> SlotMap<DefaultKey, V>
pub fn new() -> SlotMap<DefaultKey, V>
Construct a new empty map, using a default key type.
Sourcepub fn with_capacity(capacity: usize) -> SlotMap<DefaultKey, V>
pub fn with_capacity(capacity: usize) -> SlotMap<DefaultKey, V>
Construct a new empty map with a specified capacity, using a default key type.
See
slotmap::SlotMap::with_capacity()
.
::with_capacity()`].
Source§impl<K, V> SlotMap<K, V>where
K: Key,
impl<K, V> SlotMap<K, V>where
K: Key,
Sourcepub fn with_capacity_and_key(capacity: usize) -> SlotMap<K, V>
pub fn with_capacity_and_key(capacity: usize) -> SlotMap<K, V>
Construct a new empty map with a specified capacity, using a specialized key type.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Return the total number of slots available for entries in this map.
This number includes used slots, as well as empty slots that may become used.
See
slotmap::SlotMap::capacity()
,
but note that a slotmap-careful
implementation may lose capacity over time,
as slots are marked unusable.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserve space as needed.
Allocates if needed, so that this map can hold additional
new entries
without having to resize.
Sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Return true if the map contains an entry with a given key.
Sourcepub fn insert(&mut self, value: V) -> K
pub fn insert(&mut self, value: V) -> K
Insert a new value into the map, and return the key used for it.
Sourcepub fn insert_with_key<F>(&mut self, f: F) -> Kwhere
F: FnOnce(K) -> V,
pub fn insert_with_key<F>(&mut self, f: F) -> Kwhere
F: FnOnce(K) -> V,
Insert a new value into the map, constructing it using its own new key.
This method is useful for the case where a value needs to refer to the key that will be assigned to it.
Sourcepub fn try_insert_with_key<F, E>(&mut self, f: F) -> Result<K, E>
pub fn try_insert_with_key<F, E>(&mut self, f: F) -> Result<K, E>
As Self::insert_with_key
, but may return an Err
.
Sourcepub fn remove(&mut self, key: K) -> Option<V>
pub fn remove(&mut self, key: K) -> Option<V>
Remove and return the element of this map with a given key.
Return None if the key is not present in the map.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Remove every element of this map that does not satisfy a given predicate.
Sourcepub fn get(&self, key: K) -> Option<&V>
pub fn get(&self, key: K) -> Option<&V>
Return a reference to the element of this map with a given key.
Return None if there is no such element.
Sourcepub fn get_mut(&mut self, key: K) -> Option<&mut V>
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
Return a mutable reference to the element of this map with a given key.
Return None if there is no such element.
Sourcepub fn get_disjoint_mut<const N: usize>(
&mut self,
keys: [K; N],
) -> Option<[&mut V; N]>
pub fn get_disjoint_mut<const N: usize>( &mut self, keys: [K; N], ) -> Option<[&mut V; N]>
Return an array of mutable references to the elements of this map with a given list of keys.
Return None if any key is not present, or if the same key is given twice.
Sourcepub fn iter(&self) -> impl Iterator<Item = (K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (K, &V)>
Return an iterator over the elements of this map.
§Current limitations
Does not return a named type.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (K, &mut V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (K, &mut V)>
Return a mutable iterator over the elements of this map.
See
slotmap::SlotMap::iter_mut()
.
§Current limitations
Does not return a named type.
Sourcepub fn keys(&self) -> impl Iterator<Item = K>
pub fn keys(&self) -> impl Iterator<Item = K>
Return an iterator over all the keys in this map.
§Current limitations
Does not return a named type.
Sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
Return an iterator over the values in this map.
See
slotmap::SlotMap::values()
.
§Current limitations
Does not return a named type.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
Return a mutable iterator over the values in this map.
See
slotmap::SlotMap::values_mut()
.
§Current limitations
Does not return a named type.
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for SlotMap<K, V>
impl<K, V> RefUnwindSafe for SlotMap<K, V>where
V: RefUnwindSafe,
impl<K, V> Send for SlotMap<K, V>where
V: Send,
impl<K, V> Sync for SlotMap<K, V>where
V: Sync,
impl<K, V> Unpin for SlotMap<K, V>where
V: Unpin,
impl<K, V> UnwindSafe for SlotMap<K, V>where
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more