pub struct SyncVec<V> { /* private fields */ }Expand description
A concurrent Vec with a Go sync.Map-style read/dirty architecture:
read: an immutable snapshot, atomically published.get/iterread it lock-free.dirty: the canonical, mutable vec, 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 set swaps the pointer
in place (O(1)) — no snapshot rebuild — and readers always see the latest
value. Appends are published lazily (tracked by the amended flag), while
index-shifting operations (insert/remove/pop) rebuild the snapshot.
Snapshots and retired values are kept alive until the vec is dropped, so
references returned by get stay valid.
Implementations§
Source§impl<V> SyncVec<V>
impl<V> SyncVec<V>
pub fn new_arc() -> Arc<Self> ⓘ
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn with_vec(vec: Vec<V>) -> Self
pub fn insert(&self, index: usize, v: V) -> Option<V>
pub fn set(&self, index: usize, v: V) -> Option<V>
pub fn push(&self, v: V) -> Option<V>
pub fn pushes(&self, arr: Vec<V>) -> Option<V>
pub fn push_mut(&mut self, v: V) -> Option<V>
Sourcepub fn pop(&self) -> Option<V>where
V: Clone,
pub fn pop(&self) -> Option<V>where
V: Clone,
Remove and return the last element.
This requires V: Clone because the removed value must stay alive
for concurrent readers. Use pop_discard when
the value is not Clone and the removed value is not needed.
pub fn pop_mut(&mut self) -> Option<V>where
V: Clone,
Sourcepub fn pop_discard(&self)
pub fn pop_discard(&self)
Remove and discard the last element without returning it. Unlike
pop this does not require V: Clone, so it works
with non-Clone values.
pub fn pop_discard_mut(&mut self)
Sourcepub fn remove(&self, index: usize) -> Option<V>where
V: Clone,
pub fn remove(&self, index: usize) -> Option<V>where
V: Clone,
Remove and return the element at index.
This requires V: Clone because the removed value must stay alive
for concurrent readers. Use
remove_discard when the value is not Clone
and the removed value is not needed.
pub fn remove_mut(&mut self, index: usize) -> Option<V>where
V: Clone,
Sourcepub fn remove_discard(&self, index: usize)
pub fn remove_discard(&self, index: usize)
Remove and discard the element at index without returning it. Unlike
remove this does not require V: Clone, so it
works with non-Clone values.
pub fn remove_discard_mut(&mut self, index: usize)
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn clear(&self)
pub fn shrink_to_fit(&self)
pub fn from(vec: Vec<V>) -> Self
Sourcepub fn get(&self, index: usize) -> Option<&V>
pub fn get(&self, index: usize) -> Option<&V>
Returns a reference to the element at index.
Reads are lock-free: the value is served from the immutable read
snapshot through a shared entry, so set is visible immediately.
If the index was appended to dirty since the last snapshot was
published, a fresh snapshot is published first.
pub unsafe fn get_uncheck(&self, index: usize) -> &V
Sourcepub fn get_mut(&self, index: usize) -> Option<VecRefMut<'_, V>>where
V: Clone,
pub fn get_mut(&self, index: usize) -> Option<VecRefMut<'_, V>>where
V: Clone,
Returns a mutable handle to the element at index, 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(&self, x: &V) -> boolwhere
V: PartialEq,
Sourcepub fn iter(&self) -> Iter<'_, V> ⓘ
pub fn iter(&self) -> Iter<'_, V> ⓘ
Iterate over the current contents. A fresh snapshot is published first, so all elements written so far are visible.
pub fn iter_mut(&self) -> IterMut<'_, V> ⓘwhere
V: Clone,
pub fn into_iter(self) -> IntoIter<V> ⓘ
pub fn into_inner(self) -> Vec<V>
Trait Implementations§
Source§impl<'de, V> Deserialize<'de> for SyncVec<V>where
V: Deserialize<'de>,
impl<'de, V> Deserialize<'de> for SyncVec<V>where
V: Deserialize<'de>,
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<'a, V> IntoIterator for &'a SyncVec<V>
impl<'a, V> IntoIterator for &'a SyncVec<V>
Source§impl<V> IntoIterator for SyncVec<V>
impl<V> IntoIterator for SyncVec<V>
impl<V> Send for SyncVec<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 vec is dropped, so references derived from get
remain valid for the lifetime of &self.