pub trait Map<K, V>where
Self: Sized,{
// Required methods
fn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a>
where K: Borrow<Q>,
Q: Hash + Ord + ?Sized;
fn inc(self, k: K, v: V) -> Self;
fn dec<Q>(self, k: &Q) -> Self
where K: Borrow<Q>,
Q: Hash + Ord + ?Sized;
fn zero(self) -> Self;
fn shrink(self) -> Self;
fn update<F>(self, k: K, f: F) -> Self
where F: FnOnce(Option<V>) -> V;
// Provided methods
fn plus<I>(self, coll: I) -> Self
where I: IntoIterator<Item = (K, V)> { ... }
fn update_all<F>(self, f: F) -> Self
where Self: IntoIterator<Item = (K, V)> + FromIterator<(K, V)>,
F: FnMut(&K, V) -> V { ... }
fn merge<I, F>(self, coll: I, f: F) -> Self
where I: IntoIterator<Item = (K, V)>,
F: FnMut(V, V) -> V { ... }
}
Expand description
basic protocol for maps.
Required Methods§
Sourcefn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a>
fn fun<'a, Q>(&'a self) -> Box<dyn Fn(&Q) -> Option<&'a V> + 'a>
a map maps from keys to values.
Provided Methods§
Sourcefn plus<I>(self, coll: I) -> Selfwhere
I: IntoIterator<Item = (K, V)>,
fn plus<I>(self, coll: I) -> Selfwhere
I: IntoIterator<Item = (K, V)>,
pours another collection into this one.
like clojure
’s into
.
Sourcefn update_all<F>(self, f: F) -> Self
fn update_all<F>(self, f: F) -> Self
updates all values by f
§example
use protocoll::Map;
use std::collections::HashMap;
let m = [0,0,0,1,1,0,0,0].iter().fold
(HashMap::new(), |m,&k| Map::update
(m, k, |n| 1 + n.unwrap_or(0)))
.update_all(|_,v| v + 1);
assert_eq!(7, m[&0]);
assert_eq!(3, m[&1]);
Sourcefn merge<I, F>(self, coll: I, f: F) -> Self
fn merge<I, F>(self, coll: I, f: F) -> Self
merges coll
into this one, resolving conflicts by f
.
like clojure
’s merge-with
.
§example
use protocoll::Map;
use std::collections::HashMap;
use std::ops::Add;
let m = [0,0,0,1,1,0,0,0].iter().fold
(HashMap::new(), |m,&k| Map::update
(m, k, |n| 1 + n.unwrap_or(0)));
let m = Map::merge(m.clone(), m, usize::add);
assert_eq!(12, m[&0]);
assert_eq!(4, m[&1]);
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.