Struct typedmap::dashmap::TypedDashMap [−][src]
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]
S: 'static + BuildHasher + Clone,
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]
&self,
key: K,
value: K::Value
) -> Option<K::Value> where
K::Value: Send + Sync,
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]
&self,
key: &K
) -> Option<Ref<'_, Marker, K, S>> where
K::Value: Send + Sync,
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]
&self,
key: &K
) -> Option<RefMut<'_, Marker, K, S>> where
K::Value: Send + Sync,
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]
&self,
key: &K
) -> bool where
K::Value: Send + Sync,
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]
&self,
key: &K
) -> Option<(K, K::Value)> where
K::Value: Send + Sync,
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]
&self,
key: &K,
f: impl FnOnce(&K, &K::Value) -> bool
) -> Option<(K, K::Value)> where
K::Value: Send + Sync,
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]
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>;
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]
&mut self,
key: K
) -> Entry<'_, K, Marker, S> where
K::Value: Send + Sync,
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]
&self,
predicate: impl FnMut(TypedKeyValueRef<'_, Marker>) -> bool
)
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,
Marker: Send,
S: Send,
impl<Marker, S> Sync for TypedDashMap<Marker, S> where
Marker: Sync,
S: Send + Sync,
Marker: Sync,
S: Send + Sync,
impl<Marker, S> Unpin for TypedDashMap<Marker, S> where
Marker: Unpin,
S: Unpin,
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for 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.
pub 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>,