跳到主要内容

OwnedMutexGuard

搜索

结构体 OwnedMutexGuard 

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

持有的 Mutex 的 owned 句柄。

此 guard 仅在 Arc 包装的 Mutex 中可用。 它与 MutexGuard 相同, 只是 它 克隆 Arc, 增加引用计数, 而不是 借用 Mutex。 这意味着 与 MutexGuard 不同, 它将具有 'static 生命周期。

只要你持有 此 guard, 就拥有 对底层 T 的独占访问权。 guard 内部 保留 指向 原始 Mutex 的 引用计数指针, 因此 即使 lock 被丢弃, guard 仍然有效。

无论 guard 何时 被丢弃, lock 都会 自动释放, 此时 lock 将 再次成功。

实现§

Source§

impl<T: ?Sized> OwnedMutexGuard<T>

Source

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

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

该操作不会失败,因为传入的 OwnedMutexGuard 已经锁定了这个互合。

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

§示例
use tokio::sync::{Mutex, OwnedMutexGuard};

use std::sync::Arc;



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

struct Foo(u32);



let foo = Arc::new(Mutex::new(Foo(1)));



{

    let mut mapped = OwnedMutexGuard::map(foo.clone().lock_owned().await, |f| &mut f.0);

    *mapped = 2;

}



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

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

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

该操作不会失败,因为传入的 OwnedMutexGuard 已经锁定了这个互合。

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

§示例
use tokio::sync::{Mutex, OwnedMutexGuard};

use std::sync::Arc;



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

struct Foo(u32);



let foo = Arc::new(Mutex::new(Foo(1)));



{

    let mut mapped = OwnedMutexGuard::try_map(foo.clone().lock_owned().await, |f| Some(&mut f.0))

        .expect("should not fail");

    *mapped = 2;

}



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

pub fn mutex(this: &Self) -> &Arc<Mutex<T>>

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

use std::sync::Arc;

use tokio::sync::{Mutex, OwnedMutexGuard};



async fn unlock_and_relock(guard: OwnedMutexGuard<u32>) -> OwnedMutexGuard<u32> {

    println!("1. contains: {:?}", *guard);

    let mutex: Arc<Mutex<u32>> = OwnedMutexGuard::mutex(&guard).clone();

    drop(guard);

    let guard = mutex.lock_owned().await;

    println!("2. contains: {:?}", *guard);

    guard

}

Trait 实现§

Source§

impl<T: ?Sized + Debug> Debug for OwnedMutexGuard<T>

Source§

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

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

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

Source§

type Target = T

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

fn deref(&self) -> &Self::Target

解引用此值。
Source§

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

Source§

fn deref_mut(&mut self) -> &mut Self::Target

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

impl<T: ?Sized + Display> Display for OwnedMutexGuard<T>

Source§

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

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

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

Source§

fn drop(&mut self)

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

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

自动 Trait 实现§

§

impl<T> Freeze for OwnedMutexGuard<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for OwnedMutexGuard<T>

§

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

§

impl<T> Unpin for OwnedMutexGuard<T>
where T: ?Sized,

§

impl<T> UnsafeUnpin for OwnedMutexGuard<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for OwnedMutexGuard<T>

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>

执行转换。