[][src]Struct dashmap::DashMap

pub struct DashMap<K, V> where
    K: Eq + Hash
{ /* fields omitted */ }

DashMap is an implementation of a concurrent associative array/hashmap in Rust.

DashMap tries to implement an easy to use API similar to std::collections::HashMap with some slight changes to handle concurrency.

DashMap tries to be very simple to use and to be a direct replacement for RwLock<HashMap<K, V>>. To accomplish these all methods take &self instead modifying methods taking &mut self. This allows you to put a DashMap in an Arc<T> and share it between threads while being able to modify it.

Methods

impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V>[src]

pub fn new() -> Self[src]

Creates a new DashMap with a capacity of 0.

Examples

use dashmap::DashMap;

let reviews = DashMap::new();
reviews.insert("Veloren", "What a fantastic game!");

pub fn with_capacity(capacity: usize) -> Self[src]

Creates a new DashMap with a specified starting capacity.

Examples

use dashmap::DashMap;

let mappings = DashMap::with_capacity(2);
mappings.insert(2, 4);
mappings.insert(8, 16);

pub fn shards(&self) -> &[RwLock<HashMap<K, V, FxBuildHasher>>][src]

Allows you to peek at the inner shards that store your data. You should probably not use this.

Examples

use dashmap::DashMap;

let map = DashMap::<(), ()>::new();
println!("Amount of shards: {}", map.shards().len());

pub fn determine_map<Q: ?Sized>(&self, key: &Q) -> usize where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Finds which shard a certain key is stored in. You should probably not use this.

Examples

use dashmap::DashMap;

let map = DashMap::new();
map.insert("coca-cola", 1.4);
println!("coca-cola is stored in shard: {}", map.determine_map("coca-cola"));

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

Inserts a key and a value into the map.

Examples

use dashmap::DashMap;

let map = DashMap::with_capacity(2);
map.insert("I am the key!", "And I am the value!");

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

Removes an entry from the map, returning the key and value if they existed in the map.

Examples

use dashmap::DashMap;

let soccer_team = DashMap::with_capacity(2);
soccer_team.insert("Jack", "Goalie");
assert_eq!(soccer_team.remove("Jack").unwrap().1, "Goalie");

Important traits for Iter<'a, K, V, M>
pub fn iter(&'a self) -> Iter<'a, K, V, DashMap<K, V>>[src]

Creates an iterator over a DashMap yielding immutable references.

Examples

use dashmap::DashMap;

let words = DashMap::new();
words.insert("hello", "world");
assert_eq!(words.iter().count(), 1);

Important traits for IterMut<'a, K, V, M>
pub fn iter_mut(&'a self) -> IterMut<'a, K, V, DashMap<K, V>>[src]

Iterator over a DashMap yielding mutable references.

Examples

use dashmap::DashMap;

let map = DashMap::new();
map.insert("Johnny", 21);
map.iter_mut().for_each(|mut r| *r += 1);
assert_eq!(*map.get("Johnny").unwrap(), 22);

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

Get a immutable reference to an entry in the map

Examples

use dashmap::DashMap;

let youtubers = DashMap::new();
youtubers.insert("Bosnian Bill", 457000);
assert_eq!(*youtubers.get("Bosnian Bill").unwrap(), 457000);

pub fn get_mut<Q: ?Sized>(&'a self, key: &Q) -> Option<RefMut<'a, K, V>> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Get a mutable reference to an entry in the map

Examples

use dashmap::DashMap;

let class = DashMap::new();
class.insert("Albin", 15);
*class.get_mut("Albin").unwrap() -= 1;
assert_eq!(*class.get("Albin").unwrap(), 14);

pub fn shrink_to_fit(&self)[src]

Remove excess capacity to reduce memory usage.

pub fn retain(&self, f: impl FnMut(&K, &mut V) -> bool)[src]

Retain elements that whose predicates return true and discard elements whose predicates return false.

Examples

use dashmap::DashMap;

let people = DashMap::new();
people.insert("Albin", 15);
people.insert("Jones", 22);
people.insert("Charlie", 27);
people.retain(|_, v| *v > 20);
assert_eq!(people.len(), 2);

pub fn len(&self) -> usize[src]

Fetches the total amount of key-value pairs stored in the map.

Examples

use dashmap::DashMap;

let people = DashMap::new();
people.insert("Albin", 15);
people.insert("Jones", 22);
people.insert("Charlie", 27);
assert_eq!(people.len(), 3);

pub fn is_empty(&self) -> bool[src]

Checks if the map is empty or not.

Examples

use dashmap::DashMap;

let map = DashMap::<(), ()>::new();
assert!(map.is_empty());

pub fn clear(&self)[src]

Removes all key-value pairs in the map.

Examples

use dashmap::DashMap;

let stats = DashMap::new();
stats.insert("Goals", 4);
assert!(!stats.is_empty());
stats.clear();
assert!(stats.is_empty());

pub fn capacity(&self) -> usize[src]

Returns how many key-value pairs the map can store without reallocating.

pub fn alter<Q: ?Sized>(&self, key: &Q, f: impl FnOnce(&K, V) -> V) where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Modify a specific value according to a function.

Examples

use dashmap::DashMap;

let stats = DashMap::new();
stats.insert("Goals", 4);
stats.alter("Goals", |_, v| v * 2);
assert_eq!(*stats.get("Goals").unwrap(), 8);

pub fn alter_all(&self, f: impl FnMut(&K, V) -> V)[src]

Modify every value in the map according to a function.

Examples

use dashmap::DashMap;

let stats = DashMap::new();
stats.insert("Wins", 4);
stats.insert("Losses", 2);
stats.alter_all(|_, v| v + 1);
assert_eq!(*stats.get("Wins").unwrap(), 5);
assert_eq!(*stats.get("Losses").unwrap(), 3);

pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Checks if the map contains a specific key.

Examples

use dashmap::DashMap;

let team_sizes = DashMap::new();
team_sizes.insert("Dakota Cherries", 23);
assert!(team_sizes.contains_key("Dakota Cherries"));

pub fn entry(&'a self, key: K) -> Entry<'a, K, V>[src]

Advanced entry API that tries to mimic std::collections::HashMap. See the documentation on dashmap::mapref::entry for more details.

Trait Implementations

impl<'a, '_, K: 'a + Eq + Hash, V: 'a, Q: ?Sized> BitAnd<&'_ Q> for &'a DashMap<K, V> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

type Output = bool

The resulting type after applying the & operator.

impl<'a, '_, K: 'a + Eq + Hash, V: 'a, Q: ?Sized> BitOr<&'_ Q> for &'a DashMap<K, V> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

type Output = RefMut<'a, K, V>

The resulting type after applying the | operator.

impl<K: Default, V: Default> Default for DashMap<K, V> where
    K: Eq + Hash
[src]

impl<K: Eq + Hash, V> Extend<(K, V)> for DashMap<K, V>[src]

impl<K: Eq + Hash, V> FromIterator<(K, V)> for DashMap<K, V>[src]

impl<'a, K: 'a + Eq + Hash, V: 'a> Shl<(K, V)> for &'a DashMap<K, V>[src]

type Output = Option<V>

The resulting type after applying the << operator.

impl<'a, '_, K: 'a + Eq + Hash, V: 'a, Q: ?Sized> Shr<&'_ Q> for &'a DashMap<K, V> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

type Output = Ref<'a, K, V>

The resulting type after applying the >> operator.

impl<'a, '_, K: 'a + Eq + Hash, V: 'a, Q: ?Sized> Sub<&'_ Q> for &'a DashMap<K, V> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

type Output = Option<(K, V)>

The resulting type after applying the - operator.

Auto Trait Implementations

impl<K, V> !RefUnwindSafe for DashMap<K, V>

impl<K, V> Send for DashMap<K, V> where
    K: Send,
    V: Send

impl<K, V> Sync for DashMap<K, V> where
    K: Send + Sync,
    V: Send + Sync

impl<K, V> Unpin for DashMap<K, V>

impl<K, V> UnwindSafe for DashMap<K, V> where
    K: UnwindSafe,
    V: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.