pub struct FrozenMap<K, V> { /* private fields */ }
Expand description
Append-only threadsafe version of std::collections::HashMap
where
insertion does not require mutable access
Implementations§
source§impl<K: Eq + Hash, V: StableDeref> FrozenMap<K, V>
impl<K: Eq + Hash, V: StableDeref> FrozenMap<K, V>
sourcepub fn insert(&self, k: K, v: V) -> &V::Target
pub fn insert(&self, k: K, v: V) -> &V::Target
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and returns a reference to the given value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
assert_eq!(map.insert(1, Box::new("a")), &"a");
assert_eq!(map.insert(1, Box::new("b")), &"a");
sourcepub fn insert_with(&self, k: K, f: impl FnOnce() -> V) -> &V::Target
pub fn insert_with(&self, k: K, f: impl FnOnce() -> V) -> &V::Target
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but Hash
and
Eq
on the borrowed form must match those for the key type.
Note that the write lock is held for the duration of this function’s
execution, even while the value creation function is executing (if
needed). This will block any concurrent get
or insert
calls.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
assert_eq!(map.insert_with(1, || Box::new("a")), &"a");
assert_eq!(map.insert_with(1, || unreachable!()), &"a");
sourcepub fn insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> &V::Target
pub fn insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> &V::Target
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but Hash
and
Eq
on the borrowed form must match those for the key type.
Note that the write lock is held for the duration of this function’s
execution, even while the value creation function is executing (if
needed). This will block any concurrent get
or insert
calls.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
assert_eq!(map.insert_with_key(1, |_| Box::new("a")), &"a");
assert_eq!(map.insert_with_key(1, |_| unreachable!()), &"a");
sourcepub fn get<Q>(&self, k: &Q) -> Option<&V::Target>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get<Q>(&self, k: &Q) -> Option<&V::Target>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
map.insert(1, Box::new("a"));
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
sourcepub fn map_get<Q, T, F>(&self, k: &Q, f: F) -> Option<T>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
F: FnOnce(&V) -> T,
pub fn map_get<Q, T, F>(&self, k: &Q, f: F) -> Option<T>where K: Borrow<Q>, Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T,
Applies a function to the owner of the value corresponding to the key (if any).
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
map.insert(1, Box::new("a"));
assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
assert_eq!(map.map_get(&2, Clone::clone), None);
source§impl<K, V> FrozenMap<K, V>
impl<K, V> FrozenMap<K, V>
sourcepub fn into_tuple_vec(self) -> Vec<(K, V)>
pub fn into_tuple_vec(self) -> Vec<(K, V)>
Collects the contents of this map into a vector of tuples.
The order of the entries is as if iterating a HashMap
(stochastic).
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
map.insert(1, Box::new("a"));
map.insert(2, Box::new("b"));
let mut tuple_vec = map.into_tuple_vec();
tuple_vec.sort();
assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]);
source§impl<K: Eq + Hash, V: Copy> FrozenMap<K, V>
impl<K: Eq + Hash, V: Copy> FrozenMap<K, V>
sourcepub fn get_copy<Q>(&self, k: &Q) -> Option<V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_copy<Q>(&self, k: &Q) -> Option<V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a copy of the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
map.get_copy_or_insert(1, 6);
assert_eq!(map.get_copy(&1), Some(6));
assert_eq!(map.get_copy(&2), None);
sourcepub fn get_copy_or_insert(&self, k: K, v: V) -> V
pub fn get_copy_or_insert(&self, k: K, v: V) -> V
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and returns a reference to the given value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
assert_eq!(map.get_copy_or_insert(1, 6), 6);
assert_eq!(map.get_copy_or_insert(1, 12), 6);
sourcepub fn get_copy_or_insert_with(&self, k: K, f: impl FnOnce() -> V) -> V
pub fn get_copy_or_insert_with(&self, k: K, f: impl FnOnce() -> V) -> V
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but Hash
and
Eq
on the borrowed form must match those for the key type.
Note that the write lock is held for the duration of this function’s
execution, even while the value creation function is executing (if
needed). This will block any concurrent get
or insert
calls.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
assert_eq!(map.get_copy_or_insert_with(1, || 6), 6);
assert_eq!(map.get_copy_or_insert_with(1, || unreachable!()), 6);
sourcepub fn get_copy_or_insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> V
pub fn get_copy_or_insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> V
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and the value returned by the creation function, and returns a reference to the generated value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but Hash
and
Eq
on the borrowed form must match those for the key type.
Note that the write lock is held for the duration of this function’s
execution, even while the value creation function is executing (if
needed). This will block any concurrent get
or insert
calls.
Examples
use elsa::sync::FrozenMap;
let map = FrozenMap::new();
assert_eq!(map.get_copy_or_insert_with_key(1, |_| 6), 6);
assert_eq!(map.get_copy_or_insert_with_key(1, |_| unreachable!()), 6);