pub struct SyncVec<V> { /* private fields */ }Expand description
An asynchronous vector that can be safely shared between threads.
Reads are lock-free: get/iter/dirty_ref/len/contains only
register a reader slot with an atomic counter and then read the vector
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 vector in place (amortised
O(1) push, no whole-container copy).
§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 push/remove/get_mut), otherwise the
writer waits for its own guard and deadlocks.
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>
pub fn pop(&self) -> Option<V>
pub fn pop_mut(&mut self) -> Option<V>
pub fn remove(&self, index: usize) -> Option<V>
pub fn remove_mut(&mut self, index: usize) -> Option<V>
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<VecGet<'_, V>>
pub fn get(&self, index: usize) -> Option<VecGet<'_, V>>
Returns a read-guarded reference to the value at index.
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 vector.
Sourcepub fn get_mut(&self, index: usize) -> Option<VecRefMut<'_, V>>
pub fn get_mut(&self, index: usize) -> Option<VecRefMut<'_, V>>
Returns a write-guarded mutable reference to the value at index.
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.