pub struct RwLockWriteGuard<'a, T: ?Sized> { /* private fields */ }实现§
Source§impl<'a, T: ?Sized> RwLockWriteGuard<'a, T>
impl<'a, T: ?Sized> RwLockWriteGuard<'a, T>
Sourcepub fn map<F, U: ?Sized>(this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
pub fn map<F, U: ?Sized>(this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
为已锁定数据的一个组件创建一个新的 。RwLockMappedWriteGuard
该操作不会失败,因为传入的 已经锁定了这些数据。RwLockWriteGuard
这是一个关联函数,需要作为
使用。如果用方法会和已锁定数据内容器上同名的方法冲突。RwLockWriteGuard::map(..)
这是 crate 中 parking_lot 的异步版本。RwLockWriteGuard::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);Sourcepub fn downgrade_map<F, U: ?Sized>(this: Self, f: F) -> RwLockReadGuard<'a, U>
pub fn downgrade_map<F, U: ?Sized>(this: Self, f: F) -> RwLockReadGuard<'a, U>
为已锁定数据的一个组件创建一个新的 。RwLockReadGuard
该操作不会失败,因为传入的 已经锁定了这些数据。RwLockWriteGuard
这是一个关联函数,需要作为
使用。如果用方法会和已锁定数据内容器上同名的方法冲突。RwLockWriteGuard::downgrade_map(..)
这等同于 crate 中异步 parking_lot 和 RwLockWriteGuard::map 的组合。RwLockWriteGuard::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);Sourcepub fn try_map<F, U: ?Sized>(
this: Self,
f: F,
) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
pub fn try_map<F, U: ?Sized>( this: Self, f: F, ) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
尝试为已锁定数据的一个组件创建一个新的 。如果闭包返回 RwLockMappedWriteGuard,则返回原始 guard。None
该操作不会失败,因为传入的 已经锁定了这些数据。RwLockWriteGuard
这是一个关联函数,需要作为 RwLockWriteGuard::try_map(...) 使用。如果使用方法会和已锁定数据内容器上同名的方法冲突。
这是 crate 中 parking_lot 的异步版本。RwLockWriteGuard::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);Sourcepub fn try_downgrade_map<F, U: ?Sized>(
this: Self,
f: F,
) -> Result<RwLockReadGuard<'a, U>, Self>
pub fn try_downgrade_map<F, U: ?Sized>( this: Self, f: F, ) -> Result<RwLockReadGuard<'a, U>, Self>
尝试为已锁定数据的一个组件创建新的 RwLockReadGuard。如果闭包返回 None,则返回原始 guard。
该操作不会失败,因为传入的 已经锁定了这些数据。RwLockWriteGuard
这是一个关联函数,需要作为 RwLockWriteGuard::try_downgrade_map(...) 使用。如果使用方法会和已锁定数据内容器上同名的方法冲突。
这等同于 crate 中异步 parking_lot 和 RwLockWriteGuard::try_map 的组合。RwLockWriteGuard::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);Sourcepub fn into_mapped(this: Self) -> RwLockMappedWriteGuard<'a, T>
pub fn into_mapped(this: Self) -> RwLockMappedWriteGuard<'a, T>
将此 RwLockWriteGuard 转换为 RwLockMappedWriteGuard。此方法可用于将非映射 guard 存储到期望映射 guard 的结构体字段中。
这等价于调用 RwLockWriteGuard::map(guard, |me| me)。
Sourcepub fn downgrade(self) -> RwLockReadGuard<'a, T>
pub fn downgrade(self) -> RwLockReadGuard<'a, T>
原子地将写锁降级为读锁,既不会允许其他写客在这段时间获取锁的专有权限。
注意:这不会一定允许其他读者获取锁,因为 是公平的,写者可能会排在下个。RwLock
返回一个 RAII guard,当它被逐出时会释弃对 的读访问权。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");