pub struct DenseSlotMap<K: Key, V> { /* private fields */ }
Expand description
A variation of
slotmap::DenseSlotMap
that can never give the same key for multiple objects.
Unlike a regular version of
DenseSlotMap
,
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.
Implementations§
Source§impl<V> DenseSlotMap<DefaultKey, V>
impl<V> DenseSlotMap<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::DenseSlotMap::with_capacity()
.
::with_capacity()`].
Source§impl<K: Key, V> DenseSlotMap<K, V>
impl<K: Key, V> DenseSlotMap<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::DenseSlotMap::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::DenseSlotMap::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::DenseSlotMap::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::DenseSlotMap::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::DenseSlotMap::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::DenseSlotMap::values_mut()
.
§Current limitations
Does not return a named type.
Trait Implementations§
Source§impl<K: Clone + Key, V: Clone> Clone for DenseSlotMap<K, V>
impl<K: Clone + Key, V: Clone> Clone for DenseSlotMap<K, V>
Source§fn clone(&self) -> DenseSlotMap<K, V>
fn clone(&self) -> DenseSlotMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<K: Key, V> Default for DenseSlotMap<K, V>
impl<K: Key, V> Default for DenseSlotMap<K, V>
Source§impl<K: Key, V> Index<K> for DenseSlotMap<K, V>
impl<K: Key, V> Index<K> for DenseSlotMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for DenseSlotMap<K, V>
impl<K, V> RefUnwindSafe for DenseSlotMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for DenseSlotMap<K, V>
impl<K, V> Sync for DenseSlotMap<K, V>
impl<K, V> Unpin for DenseSlotMap<K, V>
impl<K, V> UnwindSafe for DenseSlotMap<K, V>where
K: UnwindSafe,
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,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 88 bytes