pub struct HopSlotMap<K: Key, V> {
pub(crate) base: HopSlotMap<K, Entry<V>>,
pub(crate) n_unusable: usize,
pub(crate) _valid: HopSlotMapValidationToken,
}
Expand description
A variation of
slotmap::HopSlotMap
that can never give the same key for multiple objects.
Unlike a regular version of
HopSlotMap
,
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: HopSlotMap<K, Entry<V>>
An underlying SlotMap, obeying the invariants above.
n_unusable: usize
The number of entries in this SlotMap that are filled with Entry::Unusable
values.
_valid: HopSlotMapValidationToken
A ZST, used to guarantee that we have spot-checked the behavior of the underlying SlotMap implementation.
Implementations§
Source§impl<V> HopSlotMap<DefaultKey, V>
impl<V> HopSlotMap<DefaultKey, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct a new empty map with a specified capacity, using a default key type.
See
slotmap::HopSlotMap::with_capacity()
.
::with_capacity()`].
Source§impl<K: Key, V> HopSlotMap<K, V>
impl<K: Key, V> HopSlotMap<K, V>
Sourcepub fn with_capacity_and_key(capacity: usize) -> Self
pub fn with_capacity_and_key(capacity: usize) -> Self
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::HopSlotMap::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.
See
slotmap::HopSlotMap::iter()
.
§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::HopSlotMap::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.
See
slotmap::HopSlotMap::keys()
.
§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::HopSlotMap::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::HopSlotMap::values_mut()
.
§Current limitations
Does not return a named type.
Trait Implementations§
Source§impl<K: Clone + Key, V: Clone> Clone for HopSlotMap<K, V>
impl<K: Clone + Key, V: Clone> Clone for HopSlotMap<K, V>
Source§fn clone(&self) -> HopSlotMap<K, V>
fn clone(&self) -> HopSlotMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more