跳到主要内容

RwLockWriteGuard

搜索

结构体 RwLockWriteGuard 

源代码
pub struct RwLockWriteGuard<'a, T: ?Sized> { /* 私有字段 */ }
展开描述

用于在 drop 时释放锁的独占写访问权的 RAII 结构。

此结构体由 RwLock 上的 write 方法创建。

实现§

源代码§

impl<'a, T: ?Sized> RwLockWriteGuard<'a, T>

源代码

pub fn map<F, U: ?Sized>(this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
where F: FnOnce(&mut T) -> &mut U,

为锁定数据的组件创建一个新的 RwLockMappedWriteGuard

此操作不会失败,因为传入的 RwLockWriteGuard 已经锁定了数据。

这是一个关联函数,需要用作 RwLockWriteGuard::map(..)。方法会与锁定数据内容上的同名方法冲突。

这是来自 parking_lot crateRwLockWriteGuard::map 的异步版本。

§示例
use tokio::sync::{RwLock, RwLockWriteGuard};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Foo(u32);

let lock = RwLock::new(Foo(1));

{
    let mut mapped = RwLockWriteGuard::map(lock.write().await, |f| &mut f.0);
    *mapped = 2;
}

assert_eq!(Foo(2), *lock.read().await);
源代码

pub fn downgrade_map<F, U: ?Sized>(this: Self, f: F) -> RwLockReadGuard<'a, U>
where F: FnOnce(&T) -> &U,

为锁定数据的组件创建一个新的 RwLockReadGuard

此操作不会失败,因为传入的 RwLockWriteGuard 已经锁定了数据。

这是一个关联函数,需要用作 RwLockWriteGuard::downgrade_map(..)。方法会与锁定数据内容上的同名方法冲突。

这等效于来自 parking_lot crate 的异步 RwLockWriteGuard::mapRwLockWriteGuard::downgrade 的组合。

f 内部,即使只获得了 &T,你仍然保留对数据的独占访问权。交出 &mut T 将导致内存不安全,因为你可以使用内部可变性。

§示例
use tokio::sync::{RwLock, RwLockWriteGuard};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Foo(u32);

let lock = RwLock::new(Foo(1));

let mapped = RwLockWriteGuard::downgrade_map(lock.write().await, |f| &f.0);
let foo = lock.read().await;
assert_eq!(foo.0, *mapped);
源代码

pub fn try_map<F, U: ?Sized>( this: Self, f: F, ) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
where F: FnOnce(&mut T) -> Option<&mut U>,

尝试为已锁定数据的某个组件创建一个新的 RwLockMappedWriteGuard。如果闭包返回 None,则返回原始守卫。

此操作不会失败,因为传入的 RwLockWriteGuard 已经锁定了数据。

这是一个关联函数,需要用作 RwLockWriteGuard::try_map(...)。方法会与锁定数据内容上的同名方法冲突。

这是来自 parking_lot crateRwLockWriteGuard::try_map 的异步版本。

§示例
use tokio::sync::{RwLock, RwLockWriteGuard};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Foo(u32);

let lock = RwLock::new(Foo(1));

{
    let guard = lock.write().await;
    let mut guard = RwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail");
    *guard = 2;
}

assert_eq!(Foo(2), *lock.read().await);
源代码

pub fn try_downgrade_map<F, U: ?Sized>( this: Self, f: F, ) -> Result<RwLockReadGuard<'a, U>, Self>
where F: FnOnce(&T) -> Option<&U>,

尝试为锁定数据的组件创建一个新的 RwLockReadGuard。如果闭包返回 None,则返回原始 guard。

此操作不会失败,因为传入的 RwLockWriteGuard 已经锁定了数据。

这是一个关联函数,需要用作 RwLockWriteGuard::try_downgrade_map(...)。方法会与锁定数据内容上的同名方法冲突。

这等效于来自 parking_lot crate 的异步 RwLockWriteGuard::try_mapRwLockWriteGuard::downgrade 的组合。

f 内部,即使只获得了 &T,你仍然保留对数据的独占访问权。交出 &mut T 将导致内存不安全,因为你可以使用内部可变性。

如果此函数返回 Err(...),则锁既不会被解锁,也不会被降级。

§示例
use tokio::sync::{RwLock, RwLockWriteGuard};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Foo(u32);

let lock = RwLock::new(Foo(1));

let guard = RwLockWriteGuard::try_downgrade_map(lock.write().await, |f| Some(&f.0)).expect("should not fail");
let foo = lock.read().await;
assert_eq!(foo.0, *guard);
源代码

pub fn into_mapped(this: Self) -> RwLockMappedWriteGuard<'a, T>

将此 RwLockWriteGuard 转换为 RwLockMappedWriteGuard。此方法可用于将非映射 guard 存储在期望映射 guard 的结构体字段中。

这等效于调用 RwLockWriteGuard::map(guard, |me| me)

源代码

pub fn downgrade(self) -> RwLockReadGuard<'a, T>

原子地将写锁降级为读锁,期间不允许任何写入者获取锁的独占访问权。

注意:不一定会允许任何额外的读取者获取锁,因为 RwLock 是公平的,并且可能有写入者排在下一位。

返回一个 RAII 守卫,当它被 drop 时会释放此 RwLock 的读访问权。

§示例
let lock = Arc::new(RwLock::new(1));

let n = lock.write().await;

let cloned_lock = lock.clone();
let handle = tokio::spawn(async move {
    *cloned_lock.write().await = 2;
});

let n = n.downgrade();
assert_eq!(*n, 1, "downgrade is atomic");

drop(n);
handle.await.unwrap();
assert_eq!(*lock.read().await, 2, "second writer obtained write lock");

trait 实现§

源代码§

impl<'a, T> Debug for RwLockWriteGuard<'a, T>
where T: Debug + ?Sized,

源代码§

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

使用给定的格式化器格式化此值。 更多信息
源代码§

impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T>

源代码§

type Target = T

解引用后得到的类型。
源代码§

fn deref(&self) -> &T

解引用此值。
源代码§

impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T>

源代码§

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

以可变方式解引用此值。
源代码§

impl<'a, T> Display for RwLockWriteGuard<'a, T>
where T: Display + ?Sized,

源代码§

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

使用给定的格式化器格式化此值。 更多信息
源代码§

impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T>

源代码§

fn drop(&mut self)

执行此类型的析构函数。 更多信息
源代码§

impl<T> Send for RwLockWriteGuard<'_, T>
where T: ?Sized + Send + Sync,

源代码§

impl<T> Sync for RwLockWriteGuard<'_, T>
where T: ?Sized + Send + Sync,

自动 trait 实现§

§

impl<'a, T> Freeze for RwLockWriteGuard<'a, T>
where T: ?Sized,

§

impl<'a, T> RefUnwindSafe for RwLockWriteGuard<'a, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'a, T> Unpin for RwLockWriteGuard<'a, T>
where T: ?Sized,

§

impl<'a, T> UnsafeUnpin for RwLockWriteGuard<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for RwLockWriteGuard<'a, T>

blanket 实现§

源代码§

impl<T> Any for T
where T: 'static + ?Sized,

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

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

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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

Mutably borrows from an owned value. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

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

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

源代码§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
源代码§

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

源代码§

fn to_string(&self) -> String

Converts the given value to a String. 更多信息
源代码§

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

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

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

执行转换。
源代码§

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

源代码§

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

转换出错时返回的类型。
源代码§

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

执行转换。