pub struct SyncIndexMap<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> SyncIndexMap<K, V>
impl<K, V> SyncIndexMap<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<IndexMapGet<'_, V>>
pub fn get<Q>(&self, k: &Q) -> Option<IndexMapGet<'_, 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::{SyncIndexMap};
let mut map = SyncIndexMap::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<IndexMapRefMut<'_, K, V>>
pub fn get_mut(&self, k: &K) -> Option<IndexMapRefMut<'_, 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.