跳到主要内容

OwnedRwLockWriteGuard

搜索

结构体 OwnedRwLockWriteGuard 

Source
pub struct OwnedRwLockWriteGuard<T: ?Sized> { /* private fields */ }
展开描述

Owned RAII 结构,在 drop 时释放锁的独占写访问。

此结构体 由 RwLockwrite_owned 方法 创建。

实现§

Source§

impl<T: ?Sized> OwnedRwLockWriteGuard<T>

Source

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

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

该操作不会失败,因为传入的 OwnedRwLockWriteGuard 已经锁定了这些数据。

这是一个关联函数,需要作为 OwnedRwLockWriteGuard::map(..) 使用。如果用方法会和已锁定数据内容器上同名的方法冲突。

§示例
use std::sync::Arc;

use tokio::sync::{RwLock, OwnedRwLockWriteGuard};



#[derive(Debug, Clone, Copy, PartialEq, Eq)]

struct Foo(u32);



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



{

    let lock = Arc::clone(&lock);

    let mut mapped = OwnedRwLockWriteGuard::map(lock.write_owned().await, |f| &mut f.0);

    *mapped = 2;

}



assert_eq!(Foo(2), *lock.read().await);
Source

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

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

该操作不会失败,因为传入的 OwnedRwLockWriteGuard 已经锁定了这些数据。

这是一个关联函数,需要作为 OwnedRwLockWriteGuard::downgrade_map(..) 使用。如果用方法会和已锁定数据内容器上同名的方法冲突。

f 内部,尝者只接受了 &T,你仍然保持对数据的专有访问权。输出 &mut T 就会导致不安全的代码,因为你可能会使用内部可变性。

§示例
use std::sync::Arc;

use tokio::sync::{RwLock, OwnedRwLockWriteGuard};



#[derive(Debug, Clone, Copy, PartialEq, Eq)]

struct Foo(u32);



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



let guard = Arc::clone(&lock).write_owned().await;

let mapped = OwnedRwLockWriteGuard::downgrade_map(guard, |f| &f.0);

let foo = lock.read_owned().await;

assert_eq!(foo.0, *mapped);
Source

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

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

该操作不会失败,因为传入的 OwnedRwLockWriteGuard 已经锁定了这些数据。

这是一个关联函数,需要作为 OwnedRwLockWriteGuard::try_map(...) 使用。如果用方法会和已锁定数据内容器上同名的方法冲突。

§示例
use std::sync::Arc;

use tokio::sync::{RwLock, OwnedRwLockWriteGuard};



#[derive(Debug, Clone, Copy, PartialEq, Eq)]

struct Foo(u32);



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



{

    let guard = Arc::clone(&lock).write_owned().await;

    let mut guard = OwnedRwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail");

    *guard = 2;

}



assert_eq!(Foo(2), *lock.read().await);
Source

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

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

该操作不会失败,因为传入的 OwnedRwLockWriteGuard 已经锁定了这些数据。

这是一个关联函数,需要作为 OwnedRwLockWriteGuard::try_downgrade_map(...) 使用。如果使用方法会和已锁定数据内容器上同名的方法冲突。

f 内部,尝者只接受了 &T,你仍然保持对数据的专有访问权。输出 &mut T 就会导致不安全的代码,因为你可能会使用内部可变性。

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

§示例
use std::sync::Arc;

use tokio::sync::{RwLock, OwnedRwLockWriteGuard};



#[derive(Debug, Clone, Copy, PartialEq, Eq)]

struct Foo(u32);



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



let guard = Arc::clone(&lock).write_owned().await;

let guard = OwnedRwLockWriteGuard::try_downgrade_map(guard, |f| Some(&f.0)).expect("should not fail");

let foo = lock.read_owned().await;

assert_eq!(foo.0, *guard);
Source

pub fn into_mapped(this: Self) -> OwnedRwLockMappedWriteGuard<T>

将此 OwnedRwLockWriteGuard 转换为一个 OwnedRwLockMappedWriteGuard。此方法用于将非映射的的 guard 存储到预期映射 guard 的结构体字段中。

这等同于调用 OwnedRwLockWriteGuard::map(guard, |me| me)

Source

pub fn downgrade(self) -> OwnedRwLockReadGuard<T>

原子地将写锁降级为读锁,既不会允许其他写客在这段时间获取锁的专有权限。

注意:这不会一定允许其他读者获取锁,因为 RwLock 是公平的,写者可能会排在下个。

返回一个 RAII guard,当它被逐出时会释弃对 RwLock 的读访问权。

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



let n = lock.clone().write_owned().await;



let cloned_lock = lock.clone();

let handle = tokio::spawn(async move {

    *cloned_lock.write_owned().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");
Source

pub fn rwlock(this: &Self) -> &Arc<RwLock<T>>

返回原始 Arc<RwLock> 的引用。

§示例
use std::sync::Arc;

use tokio::sync::{RwLock, OwnedRwLockWriteGuard};



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



let guard = lock.clone().write_owned().await;

assert!(Arc::ptr_eq(&lock, OwnedRwLockWriteGuard::rwlock(&guard)));

Trait 实现§

Source§

impl<T> Debug for OwnedRwLockWriteGuard<T>
where T: Debug + ?Sized,

Source§

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

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

impl<T: ?Sized> Deref for OwnedRwLockWriteGuard<T>

Source§

type Target = T

解引用后得到的类型。
Source§

fn deref(&self) -> &T

解引用此值。
Source§

impl<T: ?Sized> DerefMut for OwnedRwLockWriteGuard<T>

Source§

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

以可变方式解引用此值。
Source§

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

Source§

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

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

impl<T: ?Sized> Drop for OwnedRwLockWriteGuard<T>

Source§

fn drop(&mut self)

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

impl<T> Send for OwnedRwLockWriteGuard<T>
where T: ?Sized + Send + Sync,

Source§

impl<T> Sync for OwnedRwLockWriteGuard<T>
where T: ?Sized + Send + Sync,

自动 Trait 实现§

Blanket 实现§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

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

Source§

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

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

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

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

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

Source§

type Target = T

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

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. 更多信息
Source§

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

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

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

执行转换。
Source§

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

Source§

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

转换出错时返回的类型。
Source§

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

执行转换。