SyncVec

Struct SyncVec 

Source
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>

Source

pub fn new_arc() -> Arc<Self>

Source

pub const fn new() -> Self

Source

pub fn with_vec(vec: Vec<V>) -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn insert(&self, index: usize, v: V)

Source

pub fn push(&self, v: V)

Source

pub fn push_return(&self, v: V) -> usize

Source

pub fn push_vec(&self, input_arr: Vec<V>)

Source

pub fn pop(&self) -> Option<V>

Source

pub fn remove(&self, index: usize) -> Option<V>

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn clear(&self)

Source

pub fn shrink_to_fit(&self)

Source

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.

Source

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.

Source

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读锁 + 值的读锁保护

Source

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读锁 + 值的写锁保护

Source

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读锁 (值无锁)

Source

pub fn contains(&self, x: &V) -> bool
where V: PartialEq,

Source

pub fn iter_rlock(&self) -> IterRLock<'_, V>

Safety; Get an iterator over the values, protected by Vec read lock + individual value read locks 安全地访问每个数据,获取迭代器,使用Vec读锁 + 各个值的读锁保护

Source

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.

Source

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读锁 + 各个值的写锁保护

Source

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.

Source

pub fn into_iter(self) -> IntoIter<V>

Source

pub fn dirty_ref(&self) -> RefGuard<'_, Vec<RwLock<V>>>

Source

pub fn into_inner(self) -> Vec<V>

Source

pub fn to_vec(&self) -> Vec<V>
where V: Clone,

Trait Implementations§

Source§

impl<V: Clone> Clone for SyncVec<V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V> Debug for SyncVec<V>
where V: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V> Default for SyncVec<V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<V: Display> Display for SyncVec<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V> Extend<V> for SyncVec<V>

Source§

fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<V> From<Vec<V>> for SyncVec<V>

Source§

fn from(v: Vec<V>) -> Self

Converts to this type from the input type.
Source§

impl<V> FromIterator<V> for SyncVec<V>

Source§

fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<V> Index<usize> for SyncVec<V>

Source§

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§

type Output = V

The returned type after indexing.
Source§

impl<V> IndexMut<usize> for SyncVec<V>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, V> IntoIterator for &'a SyncVec<V>

Source§

type Item = &'a V

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, V> IntoIterator for &'a mut SyncVec<V>

Source§

type Item = &'a mut V

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V> IntoIterator for SyncVec<V>

Source§

type Item = V

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<V: PartialEq> PartialEq for SyncVec<V>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<V> Serialize for SyncVec<V>
where V: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<V> Send for SyncVec<V>

This is safe, dirty mutex ensures safety 这是安全的,dirty 互斥锁保证了安全性

Source§

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,