pub struct SyncVec<V> { /* private fields */ }Expand description
Synchronous vector supporting fine-grained lock control 同步向量类型,支持细粒度锁控制,更安全地访问数据
Locking strategy:
- Read operations like
get: Vec read lock + value read lock - Value modification operations like
get_mut: Vec read lock + value write lock - Structure modification operations like
push/remove: Vec write lock 锁策略: - get 等读取操作: Vec读锁 + 值的读锁
- get_mut 等修改值操作: Vec读锁 + 值的写锁
- push/remove 等修改结构操作: Vec写锁
This allows:
- Multiple threads can read different values simultaneously
- When one thread modifies a value, other threads can still read other values
- Exclusive access when modifying the Vec structure 这样可以实现:
- 多个线程可以同时读取不同的值
- 一个线程修改某个值时,其他线程仍可读取其他值
- 修改Vec结构时独占访问
Implementations§
Source§impl<V> SyncVec<V>
impl<V> SyncVec<V>
pub fn new_arc() -> Arc<Self>
pub const fn new() -> Self
pub fn with_vec(vec: Vec<V>) -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn insert(&self, index: usize, v: V)
pub fn push(&self, v: V)
pub fn push_return(&self, v: V) -> usize
pub fn push_vec(&self, input_arr: Vec<V>)
pub fn pop(&self) -> Option<V>
pub fn remove(&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)
Sourcepub fn get(&self, index: usize) -> Option<RefGuard<'_, V>>
pub fn get(&self, index: usize) -> Option<RefGuard<'_, V>>
Get a reference to the value at index, protected by Vec read lock (value no lock) 获取索引处的值引用,使用Vec读锁 (值无锁)
§Safety
此方法绕过了保护值的 RwLock,仅当您确定没有其他线程正在修改该值时才应使用它。
This method bypasses the RwLock protecting the value.
It should only be used when you are sure that no other thread is modifying the value.
Sourcepub fn get_uncheck(&self, index: usize) -> RefGuard<'_, V>
pub fn get_uncheck(&self, index: usize) -> RefGuard<'_, V>
Get a reference to the value at index (unchecked), protected by Vec read lock (value no lock) 获取索引处的值引用(无检查),使用Vec读锁 (值无锁)
§Safety
此方法绕过了保护值的 RwLock,仅当您确定没有其他线程正在修改该值时才应使用它。
This method bypasses the RwLock protecting the value.
It should only be used when you are sure that no other thread is modifying the value.
Sourcepub fn get_rlock(&self, index: usize) -> Option<ReadGuard<'_, V>>
pub fn get_rlock(&self, index: usize) -> Option<ReadGuard<'_, V>>
Get a reference to the value at index, protected by Vec read lock + value read lock 获取索引处的值引用,使用Vec读锁 + 值的读锁保护
Sourcepub fn get_mut_lock(&self, index: usize) -> Option<WriteGuard<'_, V>>
pub fn get_mut_lock(&self, index: usize) -> Option<WriteGuard<'_, V>>
Get a mutable reference to the value at index, protected by Vec read lock + value write lock 获取索引处的可变值引用,使用Vec读锁 + 值的写锁保护
Sourcepub fn get_mut(&self, index: usize) -> Option<MutRefGuard<'_, V>>
pub fn get_mut(&self, index: usize) -> Option<MutRefGuard<'_, V>>
Get a mutable reference to the value at index, protected by Vec read lock (value no lock) 获取索引处的可变值引用,使用Vec读锁 (值无锁)
pub fn contains(&self, x: &V) -> boolwhere
V: PartialEq,
Sourcepub fn iter_rlock(&self) -> IterRLock<'_, V> ⓘ
pub fn iter_rlock(&self) -> IterRLock<'_, V> ⓘ
Safety; Get an iterator over the values, protected by Vec read lock + individual value read locks 安全地访问每个数据,获取迭代器,使用Vec读锁 + 各个值的读锁保护
Sourcepub fn iter(&self) -> Iter<'_, V> ⓘ
pub fn iter(&self) -> Iter<'_, V> ⓘ
不锁值,只锁Vec结构,适用于只读但不修改值的场景
§Safety
此方法是不安全的,因为它绕过了保护每个值的 RwLock。
仅当您确定没有其他线程正在修改这些值时才应使用它。
This method is unsafe because it bypasses the RwLock protecting each value.
It should only be used when you are sure that no other thread is modifying the values.
Sourcepub fn iter_mut_lock(&self) -> IterLock<'_, V> ⓘ
pub fn iter_mut_lock(&self) -> IterLock<'_, V> ⓘ
Get a mutable iterator over the values, protected by Vec read lock + individual value write locks 获取可变迭代器,使用Vec读锁 + 各个值的写锁保护
Sourcepub fn iter_mut(&self) -> IterMut<'_, V> ⓘ
pub fn iter_mut(&self) -> IterMut<'_, V> ⓘ
Get a mutable iterator over the values, protected by Vec read lock (value no lock) 获取可变迭代器,使用Vec读锁 (值无锁)
§Safety
此方法绕过了保护每个值的 RwLock。
仅当您确定没有其他线程正在修改这些值时才应使用它。
This method bypasses the RwLock protecting each value.
It should only be used when you are sure that no other thread is modifying the values.
pub fn into_iter(self) -> IntoIter<V>
pub fn dirty_ref(&self) -> RefGuard<'_, Vec<RwLock<V>>>
pub fn into_inner(self) -> Vec<V>
pub fn to_vec(&self) -> Vec<V>where
V: Clone,
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> Extend<V> for SyncVec<V>
impl<V> Extend<V> for SyncVec<V>
Source§fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T)
§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<V> FromIterator<V> for SyncVec<V>
impl<V> FromIterator<V> for SyncVec<V>
Source§fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self
Source§impl<V> Index<usize> for SyncVec<V>
impl<V> Index<usize> for SyncVec<V>
Source§fn index(&self, index: usize) -> &Self::Output
fn index(&self, index: usize) -> &Self::Output
Get a reference to the value at index, bypassing the lock. 获取索引处的值引用,绕过锁。
§Safety
This method is unsafe because it bypasses the RwLock protecting the value.
It should only be used when you are sure that no other thread is modifying the value.
Concurrent access using this method is undefined behavior.
§安全性
此方法是不安全的,因为它绕过了保护值的 RwLock。
仅当您确定没有其他线程正在修改该值时才应使用它。
使用此方法进行并发访问是未定义行为。
Source§impl<'a, V> IntoIterator for &'a SyncVec<V>
impl<'a, V> IntoIterator for &'a SyncVec<V>
Source§impl<'a, V> IntoIterator for &'a mut SyncVec<V>
impl<'a, V> IntoIterator for &'a mut SyncVec<V>
Source§impl<V> IntoIterator for SyncVec<V>
impl<V> IntoIterator for SyncVec<V>
impl<V> Send for SyncVec<V>
This is safe, dirty mutex ensures safety 这是安全的,dirty 互斥锁保证了安全性
impl<V> Sync for SyncVec<V>
This is safe, dirty mutex ensures safety 这是安全的,dirty 互斥锁保证了安全性
Auto Trait Implementations§
impl<V> !Freeze for SyncVec<V>
impl<V> !RefUnwindSafe for SyncVec<V>
impl<V> Unpin for SyncVec<V>where
V: Unpin,
impl<V> UnwindSafe for SyncVec<V>where
V: UnwindSafe,
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)