Struct DenseSlotMap

Source
pub struct DenseSlotMap<K: Key, V> {
    pub(crate) base: DenseSlotMap<K, Entry<V>>,
    pub(crate) n_unusable: usize,
    pub(crate) _valid: DenseSlotMapValidationToken,
}
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.

Fields§

§base: DenseSlotMap<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: DenseSlotMapValidationToken

A ZST, used to guarantee that we have spot-checked the behavior of the underlying SlotMap implementation.

Implementations§

Source§

impl<V> DenseSlotMap<DefaultKey, V>

Source

pub fn new() -> Self

Construct a new empty map, using a default key type.

See slotmap::DenseSlotMap::new().

Source

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>

Source

pub fn with_key() -> Self

Construct a new empty map, using a specialized key type.

See slotmap::DenseSlotMap::with_key().

Source

pub fn with_capacity_and_key(capacity: usize) -> Self

Construct a new empty map with a specified capacity, using a specialized key type.

See slotmap::DenseSlotMap::with_capacity_and_key().

Source

pub fn len(&self) -> usize

Return the number of items in this map.

See slotmap::DenseSlotMap::len().

Source

pub fn is_empty(&self) -> bool

Return true if this map has no items.

See slotmap::DenseSlotMap::is_empty().

Source

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.

Source

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.

See slotmap::DenseSlotMap::reserve().

Source

pub fn contains_key(&self, key: K) -> bool

Return true if the map contains an entry with a given key.

See slotmap::DenseSlotMap::contains_key().

Source

pub fn insert(&mut self, value: V) -> K

Insert a new value into the map, and return the key used for it.

See slotmap::DenseSlotMap::insert().

Source

pub fn insert_with_key<F>(&mut self, f: F) -> K
where 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.

See slotmap::DenseSlotMap::insert_with_key().

Source

pub fn try_insert_with_key<F, E>(&mut self, f: F) -> Result<K, E>
where F: FnOnce(K) -> Result<V, E>,

Source

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.

See slotmap::DenseSlotMap::remove().

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(K, &mut V) -> bool,

Remove every element of this map that does not satisfy a given predicate.

See slotmap::DenseSlotMap::retain().

Source

pub fn clear(&mut self)

Remove every element of this map.

See slotmap::DenseSlotMap::clear().

Source

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.

See slotmap::DenseSlotMap::get().

Source

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.

See slotmap::DenseSlotMap::get_mut().

Source

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.

See slotmap::DenseSlotMap::get_disjoint_mut().

Source

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.

Source

pub fn drain(&mut self) -> impl Iterator<Item = (K, V)> + '_

Remove every element of this map.

See slotmap::DenseSlotMap::drain().

Source

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.

Source

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.

Source

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.

Source

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>

Source§

fn clone(&self) -> DenseSlotMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug + Key, V: Debug> Debug for DenseSlotMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Key, V> Default for DenseSlotMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K: Key, V> Index<K> for DenseSlotMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: K) -> &V

Performs the indexing (container[index]) operation. Read more
Source§

impl<K: Key, V> IndexMut<K> for DenseSlotMap<K, V>

Source§

fn index_mut(&mut self, key: K) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for DenseSlotMap<K, V>

§

impl<K, V> RefUnwindSafe for DenseSlotMap<K, V>

§

impl<K, V> Send for DenseSlotMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for DenseSlotMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for DenseSlotMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for DenseSlotMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

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

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

Performs the conversion.