From 02b1274ae6da56ad4aaaff6864bffc482f1ccbc9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 5 Jul 2021 17:32:26 +0100 Subject: [PATCH] Provide OwnedMutexGuard::mutex() Signed-off-by: Ian Jackson --- tokio/src/sync/mutex.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 11c38163d3a..2a78218a4fc 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -621,6 +621,34 @@ impl fmt::Display for MutexGuard<'_, T> { // === impl OwnedMutexGuard === +impl OwnedMutexGuard { + /// Returns a reference to the original `Arc` + /// + /// ``` + /// # use std::sync::Arc; + /// # use tokio::sync::{Mutex, OwnedMutexGuard}; + /// async fn unlock_and_relock(guard: OwnedMutexGuard) -> OwnedMutexGuard + /// { + /// println!("1. contains: {:?}", *guard); + /// let mutex: Arc> = OwnedMutexGuard::mutex(&guard).clone(); + /// drop(guard); + /// let guard = mutex.lock_owned().await; + /// println!("2. contains: {:?}", *guard); + /// guard + /// } + /// # #[tokio::main] + /// # async fn main() { + /// # let mutex = Arc::new(Mutex::new(0u32)); + /// # let guard = mutex.lock_owned().await; + /// # unlock_and_relock(guard).await; + /// # } + /// ``` + #[inline] + pub fn mutex(this: &Self) -> &Arc> { + &this.lock + } +} + impl Drop for OwnedMutexGuard { fn drop(&mut self) { self.lock.s.release(1)