pub trait Mapping<K, V>: IndexMut<K, Output = V> + IntoIterator<Item = V> {
// Required methods
fn map<VV>(self, f: impl FnMut(V) -> VV) -> impl Mapping<K, VV>;
fn iter<'a>(&'a self) -> impl Iterator<Item = &'a V>
where V: 'a;
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut V>
where V: 'a;
unsafe fn get_unchecked(&self, key: K) -> &V;
unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V;
}Expand description
A trait for associative containers that map keys to values.
This trait provides a common interface for data structures that associate keys with values, similar to hash maps or arrays. It’s used extensively throughout the library for creating mappings from node/edge indices to computed values.
§Type Parameters
K: The key type (typically node or edge indices)V: The value type
§Examples
use gotgraph::prelude::*;
let mut graph: VecGraph<i32, &str> = VecGraph::default();
graph.scope_mut(|mut ctx| {
ctx.add_node(10);
ctx.add_node(20);
});
graph.scope(|ctx| {
// Create a mapping that doubles each node's value
let doubled = ctx.init_node_map(|_tag, &value| value * 2);
for node_tag in ctx.node_indices() {
println!("Doubled: {}", doubled[node_tag]);
}
});Required Methods§
Sourcefn iter<'a>(&'a self) -> impl Iterator<Item = &'a V>where
V: 'a,
fn iter<'a>(&'a self) -> impl Iterator<Item = &'a V>where
V: 'a,
Returns an iterator over references to the values in this mapping.
The order of iteration is implementation-defined.
Sourcefn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut V>where
V: 'a,
fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut V>where
V: 'a,
Returns an iterator over mutable references to the values in this mapping.
The order of iteration is implementation-defined.
Sourceunsafe fn get_unchecked(&self, key: K) -> &V
unsafe fn get_unchecked(&self, key: K) -> &V
Gets a reference to the value associated with the given key without bounds checking.
§Safety
The caller must ensure that the key exists in the mapping. Calling this method with a non-existent key results in undefined behavior.
§Parameters
key: The key to look up
§Returns
A reference to the value associated with the key.
Sourceunsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V
unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V
Gets a mutable reference to the value associated with the given key without bounds checking.
§Safety
The caller must ensure that the key exists in the mapping. Calling this method with a non-existent key results in undefined behavior.
§Parameters
key: The key to look up
§Returns
A mutable reference to the value associated with the key.
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.