Struct typedmap::dashmap::TypedDashMap[][src]

pub struct TypedDashMap<Marker = (), S = RandomState> { /* fields omitted */ }

A concurrent hash map that can store keys of any type that implements TypedMapKey and values of type defined by TypedMapKey::Value. One can use Marker to define multiple “key-value” type mappings. Under the hood the DashMap is used. Note: that it will deadlock whenever DashMap will.

Examples

use std::sync::Arc;
use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

struct Configs;
struct Services;

#[derive(Hash, PartialEq, Eq)]
struct ServiceA(usize);

// Implement key-value mapping for Configs marker
impl TypedMapKey<Configs> for ServiceA {
    type Value = usize;
}

// Implement key-value mapping for Services marker
impl TypedMapKey<Services> for ServiceA {
    type Value = &'static str;
}

#[derive(Hash, PartialEq, Eq)]
struct ServiceB(&'static str);

// Implement key-value mapping for Configs marker
impl TypedMapKey<Configs> for ServiceB {
    type Value = Vec<&'static str>;
}

// Implement key-value mapping for Services marker
impl TypedMapKey<Services> for ServiceB {
    type Value = usize;
}

// Implement key-value mapping for default (i.e. ()) marker
impl TypedMapKey for ServiceB {
    type Value = String;
}

let configs: Arc<TypedDashMap<Configs>> = Arc::new(TypedDashMap::new());
let services: Arc<TypedDashMap<Services>> = Arc::new(TypedDashMap::new());
let default: Arc<TypedDashMap> = Arc::new(TypedDashMap::new());

let configs1 = Arc::clone(&configs);
let services1 = Arc::clone(&services);
let t1 = std::thread::spawn(move ||{
    configs1.insert(ServiceA(0), 1);
    services1.insert(ServiceA(0), "one");
});
// Line below would not compile, because TypeMapKey<Marker=()>
// is not implemented for Key.
// default.insert(Key(0), 1);

// Line below would not compile, because SerivceA key defines
// type value as usize for Configs marker (not &'static str)
// configs.insert(ServiceA(0), "one");

let configs2 = Arc::clone(&configs);
let services2 = Arc::clone(&services);
let default2 = Arc::clone(&default);
let t2 = std::thread::spawn(move || {
    configs2.insert(ServiceB("zero"), vec!["one"]);
    services2.insert(ServiceB("zero"), 32);
    default2.insert(ServiceB("zero"), "one".to_owned());
});

t1.join().unwrap();
t2.join().unwrap();

assert_eq!(*configs.get(&ServiceB("zero")).unwrap(), vec!["one"]);
assert_eq!(*services.get(&ServiceB("zero")).unwrap(), 32);
assert_eq!(*default.get(&ServiceB("zero")).unwrap(), "one".to_owned());

Implementations

impl<Marker> TypedDashMap<Marker>[src]

pub fn new() -> Self[src]

Creates a new TypedDashMap with specified marker type.

Examples:

use typedmap::TypedMap;

struct Configs;
let map = TypedMap::<Configs>::new();

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

Creates a new TypedDashMap with a specified capacity and specified marker type

impl<Marker, S> TypedDashMap<Marker, S> where
    S: 'static + BuildHasher + Clone
[src]

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self[src]

Creates a new TypedDashMap with specified capacity, hasher and marker type.

pub fn with_hasher(hash_builder: S) -> Self[src]

Creates a new TypedDashMap with specified hasher and marker type.

pub fn insert<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &self,
    key: K,
    value: K::Value
) -> Option<K::Value> where
    K::Value: Send + Sync
[src]

Inserts a key and a value into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and old value is returned.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Debug, Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(map.insert(Key(3), 4).is_none());
assert_eq!(map.insert(Key(3), 5), Some(4));
assert_eq!(*map.get(&Key(3)).unwrap(), 5);

pub fn get<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &self,
    key: &K
) -> Option<Ref<'_, Marker, K, S>> where
    K::Value: Send + Sync
[src]

Get the entry of a key if it exists in the map.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(map.get(&Key(3)).is_none());
map.insert(Key(3), 4);
assert_eq!(*map.get(&Key(3)).unwrap(), 4);

pub fn get_mut<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &self,
    key: &K
) -> Option<RefMut<'_, Marker, K, S>> where
    K::Value: Send + Sync
[src]

Get mutable entry of a key if it exists in the map.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(map.get_mut(&Key(3)).is_none());
map.insert(Key(3), 4);
*map.get_mut(&Key(3)).unwrap() = 5;
assert_eq!(*map.get(&Key(3)).unwrap().value(), 5);

pub fn contains_key<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &self,
    key: &K
) -> bool where
    K::Value: Send + Sync
[src]

Check if the map contains a specific key.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(!map.contains_key(&Key(3)));
map.insert(Key(3), 4);
assert!(map.contains_key(&Key(3)));

pub fn remove<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &self,
    key: &K
) -> Option<(K, K::Value)> where
    K::Value: Send + Sync
[src]

Removes an entry from the map.

Returns both key and value if the key existed and the entry was removed. Otherwise returns None.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Debug, Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(map.remove(&Key(3)).is_none());
map.insert(Key(3), 4);
assert!(map.contains_key(&Key(3)));
assert_eq!(map.remove(&Key(3)), Some((Key(3), 4)));
assert!(!map.contains_key(&Key(3)));

pub fn remove_if<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &self,
    key: &K,
    f: impl FnOnce(&K, &K::Value) -> bool
) -> Option<(K, K::Value)> where
    K::Value: Send + Sync
[src]

Removes an entry from the map the provided conditional function returned true.

Returns both key and value if the key existed and the entry was removed. Otherwise returns None.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Debug, Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(map.remove(&Key(3)).is_none());
map.insert(Key(3), 4);
assert!(map.contains_key(&Key(3)));
assert_eq!(map.remove_if(&Key(3), |k, v| false), None);
assert!(map.contains_key(&Key(3)));
assert_eq!(map.remove_if(&Key(3), |k, v| true), Some((Key(3), 4)));
assert!(!map.contains_key(&Key(3)));

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

Get the amount of entries in the map.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq, Debug)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert_eq!(map.len(), 0);
map.insert(Key(3), 4);
assert_eq!(map.len(), 1);

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

Returns true if the map contains no elements.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = f32;
}

let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
assert!(map.is_empty());
map.insert(Key(3), 4.0);
assert!(!map.is_empty());

pub fn clear(&self)[src]

Clears the map, removing all key-value pairs.

Examples:

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq)]
struct Key(usize);

impl TypedMapKey for Key {
    type Value = f32;
}

let mut map: TypedDashMap = TypedDashMap::new();
map.insert(Key(3), 4.0);
map.clear();
assert!(map.get(&Key(3)).is_none())
// assert!(map.is_empty()); // for some reason this fails

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

Notable traits for Iter<'a, Marker, S>

impl<'a, Marker, S: BuildHasher + Clone> Iterator for Iter<'a, Marker, S> type Item = TypedKeyValueGuard<'a, Marker, S>;
[src]

An iterator visiting all key-value pairs in arbitrary order.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq, Debug)]
struct Key(usize);
#[derive(Hash, PartialEq, Eq, Debug)]
struct SKey(&'static str);

impl TypedMapKey for Key {
    type Value = u32;
}

impl TypedMapKey for SKey {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::new();
map.insert(Key(3), 3);
map.insert(SKey("four"), 4);

for key_value in map.iter() {
    if let Some((key, value)) = key_value.downcast_pair_ref::<Key>() {
        assert_eq!(key, &Key(3));
        assert_eq!(value, &3u32);
    }

    if let Some((key, value)) = key_value.downcast_pair_ref::<SKey>() {
        assert_eq!(key, &SKey("four"));
        assert_eq!(value, &4);
    }
}

pub fn entry<K: 'static + TypedMapKey<Marker> + Send + Sync>(
    &mut self,
    key: K
) -> Entry<'_, K, Marker, S> where
    K::Value: Send + Sync
[src]

Gets the given key’s corresponding entry in the map for in-place manipulation.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq)]
struct Key(char);

impl TypedMapKey for Key {
    type Value = usize;
}

let mut letters: TypedDashMap = TypedDashMap::new();
for ch in "a short treatise on fungi".chars() {
   let mut counter = letters.entry(Key(ch)).or_insert(0);
   *counter += 1;
}
assert_eq!(letters.get(&Key('s')).unwrap().value(), &2);
assert_eq!(letters.get(&Key('t')).unwrap().value(), &3);
assert_eq!(letters.get(&Key('u')).unwrap().value(), &1);
assert!(letters.get(&Key('y')).is_none());

pub fn retain(
    &self,
    predicate: impl FnMut(TypedKeyValueRef<'_, Marker>) -> bool
)
[src]

Retain elements that the filter closure returns true for.

Examples

use typedmap::TypedDashMap;
use typedmap::TypedMapKey;

#[derive(Hash, PartialEq, Eq, Debug)]
struct Key(usize);
#[derive(Hash, PartialEq, Eq, Debug)]
struct SKey(&'static str);

impl TypedMapKey for Key {
    type Value = u32;
}

impl TypedMapKey for SKey {
    type Value = usize;
}

let mut map: TypedDashMap = TypedDashMap::new();
map.insert(Key(3), 3);
map.insert(SKey("four"), 4);

map.retain(|kv| kv.downcast_key_ref::<Key>().is_some());
assert!(map.contains_key(&Key(3)));

Trait Implementations

impl<Marker> Default for TypedDashMap<Marker>[src]

Auto Trait Implementations

impl<Marker = (), S = RandomState> !RefUnwindSafe for TypedDashMap<Marker, S>

impl<Marker, S> Send for TypedDashMap<Marker, S> where
    Marker: Send,
    S: Send

impl<Marker, S> Sync for TypedDashMap<Marker, S> where
    Marker: Sync,
    S: Send + Sync

impl<Marker, S> Unpin for TypedDashMap<Marker, S> where
    Marker: Unpin,
    S: Unpin

impl<Marker = (), S = RandomState> !UnwindSafe for TypedDashMap<Marker, S>

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.