pub struct SyncHashMap<K: Eq + Hash, V> { /* private fields */ }Expand description
this sync map used to many reader,writer less.space-for-time strategy
Reads are lock-free: get/iter/dirty_ref/len/contains_key only
register a reader slot with an atomic counter and then read the map without
any lock (readers never block each other and never touch a lock word).
Writes take a mutex, raise a writing flag and wait until all in-flight
readers are gone before mutating the map in place — O(1), no whole-container
copy and no Clone requirement on K/V.
§Deadlock note
A read guard makes writers wait until it is dropped. Do not call a write
method while a read/write guard is alive in the same scope: drop the guard
first (e.g. drop(g) before insert/remove/get_mut), otherwise the
writer waits for its own guard and deadlocks.
Implementations§
Source§impl<K, V> SyncHashMap<K, V>
impl<K, V> SyncHashMap<K, V>
pub fn new_arc() -> Arc<Self> ⓘ
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn with_map(map: Map<K, V>) -> Self
pub fn insert(&self, k: K, v: V) -> Option<V>
pub fn insert_mut(&mut self, k: K, v: V) -> Option<V>
pub fn remove(&self, k: &K) -> Option<V>
pub fn remove_mut(&mut self, k: &K) -> Option<V>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn clear(&self)
pub fn clear_mut(&mut self)
pub fn shrink_to_fit(&self)
pub fn shrink_to_fit_mut(&mut self)
pub fn from(map: Map<K, V>) -> Self
Sourcepub fn get<Q>(&self, k: &Q) -> Option<HashMapGet<'_, V>>
pub fn get<Q>(&self, k: &Q) -> Option<HashMapGet<'_, V>>
Returns a read-guarded reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
Hash and Eq on the borrowed form must match those for
the key type.
The read is lock-free: it only registers a reader slot, so concurrent reads never block each other and never take a lock. Writers wait for the returned guard to be dropped before mutating the map.
§Examples
use dark_std::sync::{SyncHashMap};
let mut map = SyncHashMap::new();
map.insert_mut(1, "a");
assert_eq!(*map.get(&1).unwrap(), "a");
assert_eq!(map.get(&2).is_none(), true);Sourcepub fn get_mut(&self, k: &K) -> Option<HashMapRefMut<'_, K, V>>
pub fn get_mut(&self, k: &K) -> Option<HashMapRefMut<'_, K, V>>
Returns a write-guarded mutable reference to the value of the key.
The guard holds the writer lock (writers are mutually exclusive and wait for in-flight readers) until it is dropped, so the mutable reference can never race with concurrent readers or writers. Drop it before calling another method from the same scope.
pub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn iter(&self) -> HashMapIter<'_, K, V> ⓘ
pub fn iter_mut(&self) -> HashMapIterMut<'_, K, V> ⓘ
pub fn into_iter(self) -> MapIntoIter<K, V> ⓘ
pub fn dirty_ref(&self) -> ReadMapGuard<'_, Map<K, V>>
pub fn into_inner(self) -> Map<K, V>
Trait Implementations§
Source§impl<K, V> Debug for SyncHashMap<K, V>
impl<K, V> Debug for SyncHashMap<K, V>
Source§impl<'de, K, V> Deserialize<'de> for SyncHashMap<K, V>
impl<'de, K, V> Deserialize<'de> for SyncHashMap<K, V>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<K, V> Display for SyncHashMap<K, V>
impl<K, V> Display for SyncHashMap<K, V>
Source§impl<K, V> Index<&K> for SyncHashMap<K, V>
Index access, kept for compatibility with the pre-0.2.17 API.
impl<K, V> Index<&K> for SyncHashMap<K, V>
Index access, kept for compatibility with the pre-0.2.17 API.
§Contract
The returned reference is only valid while no other thread mutates the
container. Prefer SyncHashMap::get, which pins a reader slot.