[go: up one dir, main page]

Struct CloneConMap

Source
pub struct CloneConMap<K, V, S = RandomState>
where K: Clone + Hash + Eq + 'static, V: Clone + 'static,
{ /* private fields */ }
Expand description

A concurrent map that clones its elements.

This flavour stores the data as (K, V) tuples; it clones independently both the elements (to be intended as the key and the value) of the tuple. The return values of its functions are clones of the stored values. This makes this data structure suitable for types cheap to clone.

Iteration returns cloned copies of its elements. The FromIterator and Extend traits accept tuples as arguments. Furthermore, the Extend is also implemented for shared references (to allow extending the same map concurrently from multiple threads).

§Examples

use contrie::CloneConMap;
use crossbeam_utils::thread;

let map = CloneConMap::new();

thread::scope(|s| {
    s.spawn(|_| {
        map.insert("hello", 1);
    });
    s.spawn(|_| {
        map.insert("world", 2);
    });
}).unwrap();
assert_eq!(1, map.get("hello").unwrap().1);
assert_eq!(2, map.get("world").unwrap().1);
use contrie::clonemap::{CloneConMap};

let map_1: CloneConMap<usize, Vec<usize>> = CloneConMap::new();

map_1.insert(42, vec![1, 2, 3]);
map_1.insert(43, vec![1, 2, 3, 4]);

assert_eq!(3, map_1.get(&42).unwrap().1.len());
assert_eq!(4, map_1.get(&43).unwrap().1.len());
assert_eq!(None, map_1.get(&44));

let map_2 = CloneConMap::new();
map_2.insert(44, map_1.get(&43).unwrap().1);
assert_eq!(4, map_2.get(&44).unwrap().1.len());

Implementations§

Source§

impl<K, V> CloneConMap<K, V>
where K: Clone + Hash + Eq + 'static, V: Clone + 'static,

Source

pub fn new() -> Self

Creates a new empty map.

Source§

impl<K, V, S> CloneConMap<K, V, S>
where K: Clone + Hash + Eq + 'static, V: Clone + 'static, S: BuildHasher,

Source

pub fn insert(&self, key: K, value: V) -> Option<(K, V)>

Inserts a new element as a tuple (key, value).

Any previous element with the same key is replaced and returned.

Source

pub fn get_or_insert(&self, key: K, value: V) -> ExistingOrNew<(K, V)>

Looks up or inserts an element as a tuple (key, value).

It looks up an element. If it isn’t present, the provided one is inserted instead. Either way, an element is returned.

Source

pub fn get_or_insert_with<F>(&self, key: K, create: F) -> ExistingOrNew<(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).

Source

pub fn get_or_insert_default(&self, key: K) -> ExistingOrNew<(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> CloneConMap<K, V, S>
where K: Clone + Hash + Eq, V: Clone, S: BuildHasher,

Source

pub fn with_hasher(hasher: S) -> Self

Creates a new empty map, but with the provided hasher implementation.

Source

pub fn get<Q>(&self, key: &Q) -> Option<(K, V)>
where Q: ?Sized + Eq + Hash, K: Borrow<Q>,

Looks up an element.

Source

pub fn remove<Q>(&self, key: &Q) -> Option<(K, V)>
where Q: ?Sized + Eq + Hash, K: Borrow<Q>,

Removes an element identified by the given key, returning it.

Source§

impl<K, V, S> CloneConMap<K, V, S>
where K: Clone + Hash + Eq, V: Clone,

Source

pub fn is_empty(&self) -> bool

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.

Source

pub fn iter(&self) -> Iter<'_, K, V, S>

Returns an iterator through the elements of the map.

Trait Implementations§

Source§

impl<K, V, S> Clone for CloneConMap<K, V, S>
where K: Clone + Hash + Eq, V: Clone, S: Clone + BuildHasher,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V, S> Debug for CloneConMap<K, V, S>
where K: Debug + Clone + Hash + Eq, V: Debug + Clone,

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for CloneConMap<K, V>
where K: Clone + Hash + Eq, V: Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, K, V, S> Extend<(K, V)> for &'a CloneConMap<K, V, S>
where K: Clone + Hash + Eq, V: Clone, S: BuildHasher,

Source§

fn extend<T>(&mut self, iter: T)
where T: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S> Extend<(K, V)> for CloneConMap<K, V, S>
where K: Clone + Hash + Eq, V: Clone, S: BuildHasher,

Source§

fn extend<T>(&mut self, iter: T)
where T: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V> FromIterator<(K, V)> for CloneConMap<K, V>
where K: Clone + Hash + Eq, V: Clone,

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
Source§

impl<K, V> FromParallelIterator<(K, V)> for CloneConMap<K, V>
where K: Clone + Hash + Eq + Send + Sync, V: Clone + Send + Sync,

Source§

fn from_par_iter<T>(par_iter: T) -> Self
where T: IntoParallelIterator<Item = (K, V)>,

Creates an instance of the collection from the parallel iterator par_iter. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a CloneConMap<K, V, S>
where K: Clone + Hash + Eq, V: Clone,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> ParallelExtend<(K, V)> for &'a CloneConMap<K, V, S>
where K: Clone + Hash + Eq + Send + Sync, S: BuildHasher + Sync, V: Clone + Send + Sync,

Source§

fn par_extend<T>(&mut self, par_iter: T)
where T: IntoParallelIterator<Item = (K, V)>,

Extends an instance of the collection with the elements drawn from the parallel iterator par_iter. Read more
Source§

impl<K, V, S> ParallelExtend<(K, V)> for CloneConMap<K, V, S>
where K: Clone + Hash + Eq + Send + Sync, S: BuildHasher + Sync, V: Clone + Send + Sync,

Source§

fn par_extend<T>(&mut self, par_iter: T)
where T: IntoParallelIterator<Item = (K, V)>,

Extends an instance of the collection with the elements drawn from the parallel iterator par_iter. Read more

Auto Trait Implementations§

§

impl<K, V, S = RandomState> !Freeze for CloneConMap<K, V, S>

§

impl<K, V, S> RefUnwindSafe for CloneConMap<K, V, S>

§

impl<K, V, S> Send for CloneConMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for CloneConMap<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for CloneConMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for CloneConMap<K, V, S>
where S: UnwindSafe, K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.