[−][src]Struct contrie::map::ConMap
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());
Methods
impl<K, V: ?Sized> ConMap<K, V> where
K: Hash + Eq + 'static,
V: 'static,
[src]
K: Hash + Eq + 'static,
V: 'static,
impl<K, V, S> ConMap<K, V, S> where
K: Hash + Eq + 'static,
V: 'static,
S: BuildHasher,
[src]
K: Hash + Eq + 'static,
V: 'static,
S: BuildHasher,
pub fn insert(&self, key: K, value: V) -> Option<Arc<Element<K, V>>>
[src]
Inserts a new element.
Any previous element with the same key is replaced and returned.
pub fn get_or_insert(
&self,
key: K,
value: V
) -> ExistingOrNew<Arc<Element<K, V>>>
[src]
&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.
pub fn get_or_insert_with<F>(
&self,
key: K,
create: F
) -> ExistingOrNew<Arc<Element<K, V>>> where
F: FnOnce() -> V,
[src]
&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).
pub fn get_or_insert_default(&self, key: K) -> ExistingOrNew<Arc<Element<K, V>>> where
V: Default,
[src]
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.
impl<K, V, S> ConMap<K, V, S> where
K: Hash + Eq,
V: ?Sized,
S: BuildHasher,
[src]
K: Hash + Eq,
V: ?Sized,
S: BuildHasher,
pub fn with_hasher(hasher: S) -> Self
[src]
Creates a new empty map, but with the provided hasher implementation.
pub fn insert_element(
&self,
element: Arc<Element<K, V>>
) -> Option<Arc<Element<K, V>>>
[src]
&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.
pub fn get_or_insert_with_element<F>(
&self,
key: K,
create: F
) -> ExistingOrNew<Arc<Element<K, V>>> where
F: FnOnce(K) -> Arc<Element<K, V>>,
[src]
&self,
key: K,
create: F
) -> ExistingOrNew<Arc<Element<K, V>>> where
F: FnOnce(K) -> 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.
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<Arc<Element<K, V>>> where
Q: Eq + Hash,
K: Borrow<Q>,
[src]
Q: Eq + Hash,
K: Borrow<Q>,
Looks up an element.
pub fn remove<Q: ?Sized>(&self, key: &Q) -> Option<Arc<Element<K, V>>> where
Q: Eq + Hash,
K: Borrow<Q>,
[src]
Q: Eq + Hash,
K: Borrow<Q>,
Removes an element identified by the given key, returning it.
impl<K, V, S> ConMap<K, V, S> where
K: Hash + Eq,
V: ?Sized,
[src]
K: Hash + Eq,
V: ?Sized,
pub fn is_empty(&self) -> bool
[src]
Checks if the map is currently empty.
Note that due to the nature of concurrent map, this is inherently racy ‒ another thread may add or remove elements between you call this method and act based on the result.
ⓘImportant traits for Iter<'a, K, V, S>pub fn iter(&self) -> Iter<K, V, S>
[src]
Returns an iterator through the elements of the map.
Trait Implementations
impl<K, V, S> Clone for ConMap<K, V, S> where
K: Hash + Eq,
V: ?Sized,
S: Clone + BuildHasher,
[src]
K: Hash + Eq,
V: ?Sized,
S: Clone + BuildHasher,
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<K, V> Default for ConMap<K, V> where
K: Hash + Eq,
V: ?Sized,
[src]
K: Hash + Eq,
V: ?Sized,
impl<'a, K, V, S> IntoIterator for &'a ConMap<K, V, S> where
K: Hash + Eq,
V: ?Sized,
[src]
K: Hash + Eq,
V: ?Sized,
type Item = Arc<Element<K, V>>
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V, S>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<'a, K, V, S> Extend<Arc<Element<K, V>>> for &'a ConMap<K, V, S> where
K: Hash + Eq,
V: ?Sized,
S: BuildHasher,
[src]
K: Hash + Eq,
V: ?Sized,
S: BuildHasher,
fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = Arc<Element<K, V>>>,
[src]
T: IntoIterator<Item = Arc<Element<K, V>>>,
impl<'a, K, V, S> Extend<(K, V)> for &'a ConMap<K, V, S> where
K: Hash + Eq,
S: BuildHasher,
[src]
K: Hash + Eq,
S: BuildHasher,
fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = (K, V)>,
[src]
T: IntoIterator<Item = (K, V)>,
impl<K, V, S> Extend<Arc<Element<K, V>>> for ConMap<K, V, S> where
K: Hash + Eq,
V: ?Sized,
S: BuildHasher,
[src]
K: Hash + Eq,
V: ?Sized,
S: BuildHasher,
fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = Arc<Element<K, V>>>,
[src]
T: IntoIterator<Item = Arc<Element<K, V>>>,
impl<K, V, S> Extend<(K, V)> for ConMap<K, V, S> where
K: Hash + Eq,
S: BuildHasher,
[src]
K: Hash + Eq,
S: BuildHasher,
fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = (K, V)>,
[src]
T: IntoIterator<Item = (K, V)>,
impl<K, V: ?Sized, S> Debug for ConMap<K, V, S> where
K: Debug + Hash + Eq,
V: Debug,
[src]
K: Debug + Hash + Eq,
V: Debug,
impl<K, V> FromIterator<Arc<Element<K, V>>> for ConMap<K, V> where
K: Hash + Eq,
V: ?Sized,
[src]
K: Hash + Eq,
V: ?Sized,
fn from_iter<T>(iter: T) -> Self where
T: IntoIterator<Item = Arc<Element<K, V>>>,
[src]
T: IntoIterator<Item = Arc<Element<K, V>>>,
impl<K, V> FromIterator<(K, V)> for ConMap<K, V> where
K: Hash + Eq,
[src]
K: Hash + Eq,
fn from_iter<T>(iter: T) -> Self where
T: IntoIterator<Item = (K, V)>,
[src]
T: IntoIterator<Item = (K, V)>,
impl<K, V: ?Sized> FromParallelIterator<Arc<Element<K, V>>> for ConMap<K, V> where
K: Hash + Eq + Send + Sync,
V: Send + Sync,
[src]
K: Hash + Eq + Send + Sync,
V: Send + Sync,
fn from_par_iter<T>(par_iter: T) -> Self where
T: IntoParallelIterator<Item = Arc<Element<K, V>>>,
[src]
T: IntoParallelIterator<Item = Arc<Element<K, V>>>,
impl<K, V> FromParallelIterator<(K, V)> for ConMap<K, V> where
K: Hash + Eq + Send + Sync,
V: Send + Sync,
[src]
K: Hash + Eq + Send + Sync,
V: Send + Sync,
fn from_par_iter<T>(par_iter: T) -> Self where
T: IntoParallelIterator<Item = (K, V)>,
[src]
T: IntoParallelIterator<Item = (K, V)>,
impl<'a, K, V: ?Sized, S> ParallelExtend<Arc<Element<K, V>>> for &'a ConMap<K, V, S> where
K: Hash + Eq + Send + Sync,
V: Send + Sync,
S: BuildHasher + Sync,
[src]
K: Hash + Eq + Send + Sync,
V: Send + Sync,
S: BuildHasher + Sync,
fn par_extend<T>(&mut self, par_iter: T) where
T: IntoParallelIterator<Item = Arc<Element<K, V>>>,
[src]
T: IntoParallelIterator<Item = Arc<Element<K, V>>>,
impl<K, V, S> ParallelExtend<(K, V)> for ConMap<K, V, S> where
K: Hash + Eq + Send + Sync,
S: BuildHasher + Sync,
V: Send + Sync,
[src]
K: Hash + Eq + Send + Sync,
S: BuildHasher + Sync,
V: Send + Sync,
fn par_extend<T>(&mut self, par_iter: T) where
T: IntoParallelIterator<Item = (K, V)>,
[src]
T: IntoParallelIterator<Item = (K, V)>,
impl<K, V: ?Sized, S> ParallelExtend<Arc<Element<K, V>>> for ConMap<K, V, S> where
K: Hash + Eq + Send + Sync,
V: Send + Sync,
S: BuildHasher + Sync,
[src]
K: Hash + Eq + Send + Sync,
V: Send + Sync,
S: BuildHasher + Sync,
fn par_extend<T>(&mut self, par_iter: T) where
T: IntoParallelIterator<Item = Arc<Element<K, V>>>,
[src]
T: IntoParallelIterator<Item = Arc<Element<K, V>>>,
impl<'a, K, V, S> ParallelExtend<(K, V)> for &'a ConMap<K, V, S> where
K: Hash + Eq + Send + Sync,
S: BuildHasher + Sync,
V: Send + Sync,
[src]
K: Hash + Eq + Send + Sync,
S: BuildHasher + Sync,
V: Send + Sync,
fn par_extend<T>(&mut self, par_iter: T) where
T: IntoParallelIterator<Item = (K, V)>,
[src]
T: IntoParallelIterator<Item = (K, V)>,
Auto Trait Implementations
impl<K, V: ?Sized, S> Unpin for ConMap<K, V, S> where
S: Unpin,
S: Unpin,
impl<K, V: ?Sized, S> Sync for ConMap<K, V, S> where
K: Send + Sync,
S: Sync,
V: Send + Sync,
K: Send + Sync,
S: Sync,
V: Send + Sync,
impl<K, V: ?Sized, S> Send for ConMap<K, V, S> where
K: Send + Sync,
S: Send,
V: Send + Sync,
K: Send + Sync,
S: Send,
V: Send + Sync,
impl<K, V: ?Sized, S> RefUnwindSafe for ConMap<K, V, S> where
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V: ?Sized, S> UnwindSafe for ConMap<K, V, S> where
K: RefUnwindSafe,
S: UnwindSafe,
V: RefUnwindSafe,
K: RefUnwindSafe,
S: UnwindSafe,
V: RefUnwindSafe,
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,