pub struct ConMap<K, V, S = RandomState>{ /* private fields */ }Expand description
A concurrent map.
This flavour stores the data as Arc<Element<K, V>>. This allows returning handles
to the held values cheaply even if the data is larger or impossible to clone. This has several
consequences:
- It is sometimes less convenient to use.
- It allows the values to be
?Sized‒ you can store trait objects or slices as the values (not the keys). - Entries can be shared between multiple maps.
- Cloning of the map doesn’t clone the data, it will point to the same objects.
- There’s another indirection in play.
Iteration returns (cloned) handles to the elements. The FromIterator and Extend traits
accept both tuples and element handles. Furthermore, the Extend is also implemented for
shared references (to allow extending the same map concurrently from multiple threads).
TODO: Support for rayon iterators/extend.
If this is not suitable, the CloneConMap can be used instead (TODO: Implement it).
§Examples
use contrie::ConMap;
use crossbeam_utils::thread;
let map = ConMap::new();
thread::scope(|s| {
s.spawn(|_| {
map.insert("hello", 1);
});
s.spawn(|_| {
map.insert("world", 2);
});
}).unwrap();
assert_eq!(1, *map.get("hello").unwrap().value());
assert_eq!(2, *map.get("world").unwrap().value());use std::sync::Arc;
use contrie::map::{ConMap, Element};
let map_1: ConMap<usize, [usize]> = ConMap::new();
map_1.insert_element(Arc::new(Element::new(42, [1, 2, 3])));
map_1.insert_element(Arc::new(Element::new(43, [1, 2, 3, 4])));
assert_eq!(3, map_1.get(&42).unwrap().value().len());
let map_2 = ConMap::new();
map_2.insert_element(map_1.get(&43).unwrap());Implementations§
Source§impl<K, V, S> ConMap<K, V, S>
impl<K, V, S> ConMap<K, V, S>
Sourcepub fn insert(&self, key: K, value: V) -> Option<Arc<Element<K, V>>>
pub fn insert(&self, key: K, value: V) -> Option<Arc<Element<K, V>>>
Inserts a new element.
Any previous element with the same key is replaced and returned.
Sourcepub fn get_or_insert(
&self,
key: K,
value: V,
) -> ExistingOrNew<Arc<Element<K, V>>>
pub fn get_or_insert( &self, key: K, value: V, ) -> ExistingOrNew<Arc<Element<K, V>>>
Looks up or inserts an element.
It looks up an element. If it isn’t present, the provided one is inserted instead. Either way, an element is returned.
Sourcepub fn get_or_insert_with<F>(
&self,
key: K,
create: F,
) -> ExistingOrNew<Arc<Element<K, V>>>where
F: FnOnce() -> V,
pub fn get_or_insert_with<F>(
&self,
key: K,
create: F,
) -> ExistingOrNew<Arc<Element<K, V>>>where
F: FnOnce() -> V,
Looks up or inserts a newly created element.
It looks up an element. If it isn’t present, the provided closure is used to create a new one insert it. Either way, an element is returned.
§Quirks
Due to races in case of concurrent accesses, the closure may be called even if the value is not subsequently inserted and an existing element is returned. This should be relatively rare (another thread must insert the new element between this method observes an empty slot and manages to insert the new element).
Sourcepub fn get_or_insert_default(&self, key: K) -> ExistingOrNew<Arc<Element<K, V>>>where
V: Default,
pub fn get_or_insert_default(&self, key: K) -> ExistingOrNew<Arc<Element<K, V>>>where
V: Default,
Looks up or inserts a default value of an element.
This is like get_or_insert_with, but a default value is used instead of manually providing a closure.
Source§impl<K, V, S> ConMap<K, V, S>
impl<K, V, S> ConMap<K, V, S>
Sourcepub fn with_hasher(hasher: S) -> Self
pub fn with_hasher(hasher: S) -> Self
Creates a new empty map, but with the provided hasher implementation.
Sourcepub fn insert_element(
&self,
element: Arc<Element<K, V>>,
) -> Option<Arc<Element<K, V>>>
pub fn insert_element( &self, element: Arc<Element<K, V>>, ) -> Option<Arc<Element<K, V>>>
Inserts a new element.
This acts the same as insert, but takes the already created element. It can be used when:
V: ?Sized.- You want to insert the same element into multiple maps.
Sourcepub fn get_or_insert_with_element<F>(
&self,
key: K,
create: F,
) -> ExistingOrNew<Arc<Element<K, V>>>
pub fn get_or_insert_with_element<F>( &self, key: K, create: F, ) -> ExistingOrNew<Arc<Element<K, V>>>
Looks up or inserts a new element.
This is the same as get_or_insert_with, but the closure returns a pre-created element. This can be used when:
V: ?Sized.- You want to insert the same element into multiple maps.
Trait Implementations§
Source§impl<'a, K, V, S> Extend<(K, V)> for &'a ConMap<K, V, S>
impl<'a, K, V, S> Extend<(K, V)> for &'a ConMap<K, V, S>
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V, S> Extend<(K, V)> for ConMap<K, V, S>
impl<K, V, S> Extend<(K, V)> for ConMap<K, V, S>
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = (K, V)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, K, V, S> Extend<Arc<Element<K, V>>> for &'a ConMap<K, V, S>
impl<'a, K, V, S> Extend<Arc<Element<K, V>>> for &'a ConMap<K, V, S>
Source§fn extend<T>(&mut self, iter: T)
fn extend<T>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V, S> Extend<Arc<Element<K, V>>> for ConMap<K, V, S>
impl<K, V, S> Extend<Arc<Element<K, V>>> for ConMap<K, V, S>
Source§fn extend<T>(&mut self, iter: T)
fn extend<T>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V> FromIterator<(K, V)> for ConMap<K, V>
impl<K, V> FromIterator<(K, V)> for ConMap<K, V>
Source§impl<K, V> FromParallelIterator<(K, V)> for ConMap<K, V>
impl<K, V> FromParallelIterator<(K, V)> for ConMap<K, V>
Source§fn from_par_iter<T>(par_iter: T) -> Selfwhere
T: IntoParallelIterator<Item = (K, V)>,
fn from_par_iter<T>(par_iter: T) -> Selfwhere
T: IntoParallelIterator<Item = (K, V)>,
par_iter. Read moreSource§impl<K, V> FromParallelIterator<Arc<Element<K, V>>> for ConMap<K, V>
impl<K, V> FromParallelIterator<Arc<Element<K, V>>> for ConMap<K, V>
Source§fn from_par_iter<T>(par_iter: T) -> Self
fn from_par_iter<T>(par_iter: T) -> Self
par_iter. Read moreSource§impl<'a, K, V, S> IntoIterator for &'a ConMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a ConMap<K, V, S>
Source§impl<'a, K, V, S> ParallelExtend<(K, V)> for &'a ConMap<K, V, S>
impl<'a, K, V, S> ParallelExtend<(K, V)> for &'a ConMap<K, V, S>
Source§fn par_extend<T>(&mut self, par_iter: T)where
T: IntoParallelIterator<Item = (K, V)>,
fn par_extend<T>(&mut self, par_iter: T)where
T: IntoParallelIterator<Item = (K, V)>,
par_iter. Read moreSource§impl<K, V, S> ParallelExtend<(K, V)> for ConMap<K, V, S>
impl<K, V, S> ParallelExtend<(K, V)> for ConMap<K, V, S>
Source§fn par_extend<T>(&mut self, par_iter: T)where
T: IntoParallelIterator<Item = (K, V)>,
fn par_extend<T>(&mut self, par_iter: T)where
T: IntoParallelIterator<Item = (K, V)>,
par_iter. Read moreSource§impl<'a, K, V, S> ParallelExtend<Arc<Element<K, V>>> for &'a ConMap<K, V, S>
impl<'a, K, V, S> ParallelExtend<Arc<Element<K, V>>> for &'a ConMap<K, V, S>
Source§fn par_extend<T>(&mut self, par_iter: T)
fn par_extend<T>(&mut self, par_iter: T)
par_iter. Read moreSource§impl<K, V, S> ParallelExtend<Arc<Element<K, V>>> for ConMap<K, V, S>
impl<K, V, S> ParallelExtend<Arc<Element<K, V>>> for ConMap<K, V, S>
Source§fn par_extend<T>(&mut self, par_iter: T)
fn par_extend<T>(&mut self, par_iter: T)
par_iter. Read moreAuto Trait Implementations§
impl<K, V, S = RandomState> !Freeze for ConMap<K, V, S>
impl<K, V, S> RefUnwindSafe for ConMap<K, V, S>
impl<K, V, S> Send for ConMap<K, V, S>
impl<K, V, S> Sync for ConMap<K, V, S>
impl<K, V, S> Unpin for ConMap<K, V, S>
impl<K, V, S> UnwindSafe for ConMap<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more