pub trait Lookup<Idx, T>: Default {
// Required methods
fn len(&self) -> usize;
fn contains_key(&self, idx: &Idx) -> bool;
fn get(&self, idx: &Idx) -> Option<&T>;
fn insert(&mut self, idx: Idx, value: T);
fn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T;
fn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
where T: 'a;
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = (&'a Idx, &'a mut T)>
where T: 'a,
Idx: 'a;
fn clear(&mut self);
// Provided method
fn is_empty(&self) -> bool { ... }
}
Expand description
A map of indices to values.
Note that HashMap (with std) or BTreeMap (when no-std) satisfy the requirements and
implement the Lookup
trait, and hence, can be used as the storage in sparse vectors.
Alternatively, more advanced lookup structures can be provided depending on the use case.
Required Methods§
Sourcefn contains_key(&self, idx: &Idx) -> bool
fn contains_key(&self, idx: &Idx) -> bool
Returns whether or not the idx
is present as a key in the lookup.
Sourcefn get(&self, idx: &Idx) -> Option<&T>
fn get(&self, idx: &Idx) -> Option<&T>
Returns a reference to the value with the given idx
if it exists in the lookup;
returns None otherwise.
Sourcefn insert(&mut self, idx: Idx, value: T)
fn insert(&mut self, idx: Idx, value: T)
Inserts the value
with key idx
.
If the key already existed, its value is updated.
Sourcefn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T
fn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T
If the lookup contains an element with the given idx
, the method returns a
mutable reference to the value.
Otherwise,
- inserts
(idx, value)
to the lookup, and - returns a mutable reference to the newly inserted value.
Sourcefn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>where
T: 'a,
fn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>where
T: 'a,
Returns an iterator yielding mutable references to the values in the lookup.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.