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 unsafe fn get_uncheck(&self, index: usize) -> &V
pub unsafe fn get_uncheck(&self, index: usize) -> &V
§Safety
index must be in bounds, and the returned reference is only valid
while no concurrent write mutates the container (same contract as the
pre-0.2.17 API).
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.
pub fn contains(&self, x: &V) -> boolwhere
V: PartialEq,
pub fn iter(&self) -> VecIter<'_, V> ⓘ
pub fn iter_mut(&self) -> VecIterMut<'_, V> ⓘ
pub fn into_iter(self) -> IntoIter<V> ⓘ
pub fn dirty_ref(&self) -> ReadMapGuard<'_, Vec<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<V> Index<usize> for SyncVec<V>
Index access, kept for compatibility with the pre-0.2.17 API (e.g. rbatis
reads rb.intercepts[0]).
impl<V> Index<usize> for SyncVec<V>
Index access, kept for compatibility with the pre-0.2.17 API (e.g. rbatis
reads rb.intercepts[0]).
§Contract
The returned reference is only valid while no other thread mutates the
container (same contract as std::slice::Index on an unsynchronized
Vec). Prefer SyncVec::get, which pins a reader slot and is safe
against concurrent writers.