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

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

Creates a new TypedDashMap with specified marker type.

Examples:
use typedmap::TypedMap;

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

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

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

Creates a new TypedDashMap with specified hasher and marker type.

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);

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);

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);

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)));

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)));

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)));

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);

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());

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

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);
    }
}

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 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());

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.