Struct stateright::util::DenseNatMap
source · 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 FileId
s or ProcessId
s, 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:
- Construct an empty map with
DenseNatMap::new
, then insert the key-value pairs in order: first the pair whose key corresponds with0
, then the pair whose key corresponds with1
, 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");
- 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>
impl<K, V> DenseNatMap<K, V>
sourcepub fn iter(&self) -> impl Iterator<Item = (K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (K, &V)>
Returns an iterator over pairs in the map whereby values are borrowed.
See also DenseNatMap::values
.
sourcepub fn values(&self) -> impl Iterator<Item = &V>
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>
impl<K: Clone, V: Clone> Clone for DenseNatMap<K, V>
source§fn clone(&self) -> DenseNatMap<K, V>
fn clone(&self) -> DenseNatMap<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, V> Default for DenseNatMap<K, V>
impl<K, V> Default for DenseNatMap<K, V>
source§impl<R, V> From<&DenseNatMap<R, V>> for RewritePlan<R, DenseNatMap<R, R>>
impl<R, V> From<&DenseNatMap<R, V>> for RewritePlan<R, DenseNatMap<R, R>>
source§fn from(s: &DenseNatMap<R, V>) -> Self
fn from(s: &DenseNatMap<R, V>) -> Self
source§impl<R, V> From<DenseNatMap<R, V>> for RewritePlan<R, DenseNatMap<R, R>>
impl<R, V> From<DenseNatMap<R, V>> for RewritePlan<R, DenseNatMap<R, R>>
source§fn from(s: DenseNatMap<R, V>) -> Self
fn from(s: DenseNatMap<R, V>) -> Self
source§impl<K, V> From<Vec<V>> for DenseNatMap<K, V>
impl<K, V> From<Vec<V>> for DenseNatMap<K, V>
source§impl<K, V> FromIterator<(K, V)> for DenseNatMap<K, V>
impl<K, V> FromIterator<(K, V)> for DenseNatMap<K, V>
source§impl<K, V> FromIterator<V> for DenseNatMap<K, V>
impl<K, V> FromIterator<V> for DenseNatMap<K, V>
source§fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self
source§impl<K, V> Index<K> for DenseNatMap<K, V>
impl<K, V> Index<K> for DenseNatMap<K, V>
source§impl<K, V> IndexMut<K> for DenseNatMap<K, V>
impl<K, V> IndexMut<K> for DenseNatMap<K, V>
source§impl<K, V> IntoIterator for DenseNatMap<K, V>
impl<K, V> IntoIterator for DenseNatMap<K, V>
source§impl<K: PartialEq, V: PartialEq> PartialEq for DenseNatMap<K, V>
impl<K: PartialEq, V: PartialEq> PartialEq for DenseNatMap<K, V>
source§fn eq(&self, other: &DenseNatMap<K, V>) -> bool
fn eq(&self, other: &DenseNatMap<K, V>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<R, K, V> Rewrite<R> for DenseNatMap<K, V>
impl<R, K, V> Rewrite<R> for DenseNatMap<K, V>
source§fn rewrite<S>(&self, plan: &RewritePlan<R, S>) -> Self
fn rewrite<S>(&self, plan: &RewritePlan<R, S>) -> Self
RewritePlan
.