pub struct SyncIndexMap<K: Eq + Hash, V> { /* private fields */ }Expand description
A concurrent IndexMap with a Go sync.Map-style read/dirty architecture:
read: an immutable snapshot, atomically published.get/iter/Indexread it lock-free.dirty: the canonical, mutable map, guarded bylock.
Every slot is an Arc<Entry<V>> shared between the snapshot and dirty.
The entry holds an atomic pointer to the value, so updating an existing key
swaps the pointer in place (O(1)) — no snapshot rebuild — and readers
always see the latest value. New keys and removals are published lazily
(tracked by the amended flag). Snapshots and retired values are kept
alive until the map is dropped, so references returned by get stay valid.
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
Sourcepub fn insert(&self, k: K, v: V) -> Option<V>
pub fn insert(&self, k: K, v: V) -> Option<V>
Insert or replace the value for k, returning the previous value if
the key already existed.
This requires V: Clone because the previous value must stay alive
for concurrent readers. Use set when the value is not
Clone and the previous value is not needed.
pub fn insert_mut(&mut self, k: K, v: V) -> Option<V>
Sourcepub fn set(&self, k: K, v: V)
pub fn set(&self, k: K, v: V)
Insert or overwrite the value for k without returning the previous
one. Unlike insert this does not require
V: Clone, so it works with non-Clone values. Updating an existing
key swaps the value in place (O(1)); readers observe the new value
immediately.
pub fn set_mut(&mut self, k: K, v: V)
Sourcepub fn remove(&self, k: &K) -> Option<V>
pub fn remove(&self, k: &K) -> Option<V>
Remove k and return its value.
This requires V: Clone because the removed value must stay alive
for concurrent readers. Use delete when the value is
not Clone and the removed value is not needed.
pub fn remove_mut(&mut self, k: &K) -> Option<V>
Sourcepub fn delete(&self, k: &K)where
K: Clone,
pub fn delete(&self, k: &K)where
K: Clone,
Remove k without returning its value. Unlike
remove this does not require V: Clone, so it
works with non-Clone values.
pub fn delete_mut(&mut self, k: &K)where
K: Clone,
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn clear(&self)where
K: Clone,
pub fn clear_mut(&mut self)where
K: Clone,
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<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a 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.
Reads are lock-free: the value is served from the immutable read
snapshot through a shared entry, so updates are visible immediately.
If the key was added to dirty since the last snapshot was published,
a fresh snapshot is published first.
§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<HashMapRefMut<'_, K, V>>
pub fn get_mut(&self, k: &K) -> Option<HashMapRefMut<'_, K, V>>
Returns a mutable handle to the value for k, implemented with
copy-on-write: the value is cloned, the handle mutates the clone, and
the result is swapped back into the shared entry (O(1)) when the handle
is dropped. Concurrent readers may observe the pre-mutation value until
the handle is dropped.
pub fn contains_key(&self, x: &K) -> boolwhere
K: PartialEq,
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘwhere
K: Clone,
pub fn iter(&self) -> Iter<'_, K, V> ⓘwhere
K: Clone,
Iterate over the current contents. A fresh snapshot is published first, so all entries written so far are visible.
pub fn iter_mut(&self) -> IterMut<'_, K, V> ⓘ
pub fn into_iter(self) -> MapIntoIter<K, V> ⓘ
pub fn into_inner(self) -> Map<K, V>
Trait Implementations§
Source§impl<K, V> Debug for SyncIndexMap<K, V>
impl<K, V> Debug for SyncIndexMap<K, V>
Source§impl<'de, K, V> Deserialize<'de> for SyncIndexMap<K, V>
impl<'de, K, V> Deserialize<'de> for SyncIndexMap<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 SyncIndexMap<K, V>
impl<K, V> Display for SyncIndexMap<K, V>
Source§impl<K, V> Index<&K> for SyncIndexMap<K, V>
impl<K, V> Index<&K> for SyncIndexMap<K, V>
Source§impl<'a, K, V> IntoIterator for &'a SyncIndexMap<K, V>
impl<'a, K, V> IntoIterator for &'a SyncIndexMap<K, V>
Source§impl<K, V> IntoIterator for SyncIndexMap<K, V>
impl<K, V> IntoIterator for SyncIndexMap<K, V>
impl<K: Eq + Hash, V> Send for SyncIndexMap<K, V>
Safety: dirty is only ever accessed under lock; the read snapshot is
immutable once published; values behind entries are immutable once
published and swapped out atomically; retired values and retired snapshots
are kept alive until the map is dropped, so references derived from get
remain valid for the lifetime of &self.