pub struct DenseNatMap<K, V> { /* private fields */ }
Expand description

A map optimized for cases where each key corresponds with a unique entry in the range [0..self.len()] and vice versa (each number in that range corresponds with a unique key in the map). DenseNatMap<K, V> serves as a replacement for a similar Vec<V> pattern but provides additional type safety to distinguish indices derived from K1: Into<usize> versus some other K2: Into<usize>.

Purpose

For example, if a model’s state has no gaps in FileIds or ProcessIds, one approach is the following.

struct MyStruct {
    file_metadata:    Vec<Metadata>, // indexed by `Into::<usize>::into(FileId)`
    file_contents:    Vec<RawBytes>, // indexed by `Into::<usize>::into(FileId)`
    process_metadata: Vec<Metadata>, // indexed by `Into::<usize>::into(ProcessId)`
    process_memory:   Vec<RawBytes>, // indexed by `Into::<usize>::into(ProcessId)`
}

Unfortunately the above fails to indicate to the compiler that the file_* fields have a different indexing relationship than the process_* fields. DenseNatMap serves the same purpose as Vec, but in contrast with the above example, indexing by a key of the wrong type would be a type error.

use stateright::util::DenseNatMap;
struct MyStruct {
    file_metadata:    DenseNatMap<FileId,    Metadata>,
    file_contents:    DenseNatMap<FileId,    RawBytes>,
    process_metadata: DenseNatMap<ProcessId, Metadata>,
    process_memory:   DenseNatMap<ProcessId, RawBytes>,
}

Usage

Multiple mechanisms are available to construct a DenseNatMap. For example:

  1. Construct an empty map with DenseNatMap::new, then insert the key-value pairs in order: first the pair whose key corresponds with 0, then the pair whose key corresponds with 1, and so on. Note that inserting out of order will panic.
    let mut m = DenseNatMap::new();
    m.insert(Id::from(0), "first");
    m.insert(Id::from(1), "second");
  2. Or leverage Iterator::collect.
    let mut m: DenseNatMap<Id, &'static str> = vec![
        (Id::from(1), "second"),
        (Id::from(0), "first"),
    ].into_iter().collect();

Implementations§

source§

impl<K, V> DenseNatMap<K, V>

source

pub fn new() -> Self

Constructs an empty DenseNatMap.

source

pub fn get(&self, key: K) -> Option<&V>where usize: From<K>,

Accepts a key, and returns None if invalid, otherwise Some(value).

source

pub fn insert(&mut self, key: K, value: V) -> Option<V>where usize: From<K>, K: From<usize>,

Inserts a key-value pair and returns None if no value was previously associated, otherwise Some(previous_value). Panics if neither overwriting a key nor inserting at the end.

source

pub fn iter(&self) -> impl Iterator<Item = (K, &V)>where K: From<usize>,

Returns an iterator over pairs in the map whereby values are borrowed.

See also DenseNatMap::values.

source

pub fn len(&self) -> usize

Returns the number of elements in the map.

source

pub fn values(&self) -> impl Iterator<Item = &V>

Returns an iterator over values in the map.

See also DenseNatMap::iter.

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for DenseNatMap<K, V>

source§

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

Returns a copy 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, V: Debug> Debug for DenseNatMap<K, V>

source§

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

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

impl<K, V> Default for DenseNatMap<K, V>

source§

fn default() -> Self

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

impl<R, V> From<&DenseNatMap<R, V>> for RewritePlan<R, DenseNatMap<R, R>>where R: From<usize> + Copy, usize: From<R>, V: Ord,

source§

fn from(s: &DenseNatMap<R, V>) -> Self

Converts to this type from the input type.
source§

impl<R, V> From<DenseNatMap<R, V>> for RewritePlan<R, DenseNatMap<R, R>>where R: From<usize> + Copy, usize: From<R>, V: Ord,

source§

fn from(s: DenseNatMap<R, V>) -> Self

Converts to this type from the input type.
source§

impl<K, V> From<Vec<V, Global>> for DenseNatMap<K, V>

source§

fn from(values: Vec<V>) -> Self

Converts to this type from the input type.
source§

impl<K, V> FromIterator<(K, V)> for DenseNatMap<K, V>where usize: From<K>,

source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<K, V> FromIterator<V> for DenseNatMap<K, V>

source§

fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<K: Hash, V: Hash> Hash for DenseNatMap<K, V>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<K, V> Index<K> for DenseNatMap<K, V>where usize: From<K>,

§

type Output = V

The returned type after indexing.
source§

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

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

impl<K, V> IndexMut<K> for DenseNatMap<K, V>where usize: From<K>,

source§

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

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

impl<K, V> IntoIterator for DenseNatMap<K, V>where K: From<usize>,

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K: PartialEq, V: PartialEq> PartialEq<DenseNatMap<K, V>> for DenseNatMap<K, V>

source§

fn eq(&self, other: &DenseNatMap<K, V>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<R, K, V> Rewrite<R> for DenseNatMap<K, V>where K: From<usize> + Rewrite<R>, V: Rewrite<R>, usize: From<K>,

source§

fn rewrite<S>(&self, plan: &RewritePlan<R, S>) -> Self

Generates a corresponding instance with values revised based on a particular RewritePlan.
source§

impl<K: Eq, V: Eq> Eq for DenseNatMap<K, V>

source§

impl<K, V> StructuralEq for DenseNatMap<K, V>

source§

impl<K, V> StructuralPartialEq for DenseNatMap<K, V>

Auto Trait Implementations§

§

impl<K, V> RefUnwindSafe for DenseNatMap<K, V>where K: RefUnwindSafe, V: RefUnwindSafe,

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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

Checks if this value is equivalent to the given key. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V