跳到主要内容

OwnedRwLockMappedWriteGuard

搜索

结构体 OwnedRwLockMappedWriteGuard 

Source
pub struct OwnedRwLockMappedWriteGuard<T: ?Sized, U: ?Sized = T> { /* private fields */ }
展开描述

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

此结构体 通过对 OwnedRwLockWriteGuard 进行 map 创建。 它是 一个 与 OwnedRwLockWriteGuard 不同的类型, 以禁止 对已 map 的 guard 进行降级, 因为 这样做 可能会 导致未定义行为。

实现§

Source§

impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U>

Source

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

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

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

这是一个关联函数,需要作为 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 try_map<F, V: ?Sized>( this: Self, f: F, ) -> Result<OwnedRwLockMappedWriteGuard<T, V>, Self>
where F: FnOnce(&mut U) -> Option<&mut V>,

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

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

这是一个关联函数,需要作为 OwnedRwLockMappedWriteGuard::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 rwlock(this: &Self) -> &Arc<RwLock<T>>

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

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

use tokio::sync::{

    RwLock,

    OwnedRwLockWriteGuard,

    OwnedRwLockMappedWriteGuard,

};



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



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

let guard = OwnedRwLockWriteGuard::map(guard, |x| x);

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

Trait 实现§

Source§

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

Source§

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

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

impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockMappedWriteGuard<T, U>

Source§

type Target = U

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

fn deref(&self) -> &U

解引用此值。
Source§

impl<T: ?Sized, U: ?Sized> DerefMut for OwnedRwLockMappedWriteGuard<T, U>

Source§

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

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

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

Source§

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

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

impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockMappedWriteGuard<T, U>

Source§

fn drop(&mut self)

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

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

Source§

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

自动 Trait 实现§

§

impl<T, U> Freeze for OwnedRwLockMappedWriteGuard<T, U>
where T: ?Sized, U: ?Sized,

§

impl<T, U = T> !RefUnwindSafe for OwnedRwLockMappedWriteGuard<T, U>

§

impl<T, U> Unpin for OwnedRwLockMappedWriteGuard<T, U>
where T: Unpin + ?Sized, U: ?Sized,

§

impl<T, U> UnsafeUnpin for OwnedRwLockMappedWriteGuard<T, U>
where T: ?Sized, U: ?Sized,

§

impl<T, U = T> !UnwindSafe for OwnedRwLockMappedWriteGuard<T, U>

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>

执行转换。