diff --git a/tokio/src/sync/rwlock/owned_read_guard.rs b/tokio/src/sync/rwlock/owned_read_guard.rs index b7f3926a485..ab44730658b 100644 --- a/tokio/src/sync/rwlock/owned_read_guard.rs +++ b/tokio/src/sync/rwlock/owned_read_guard.rs @@ -113,6 +113,32 @@ impl OwnedRwLockReadGuard { _p: PhantomData, }) } + + /// Returns a reference to the original `Arc` + /// + /// ``` + /// # use std::sync::Arc; + /// # use tokio::sync::{OwnedRwLockReadGuard, RwLock}; + /// async fn unlock_and_relock(guard: OwnedRwLockReadGuard) -> OwnedRwLockReadGuard + /// { + /// println!("1. contains: {:?}", *guard); + /// let mutex: Arc> = OwnedRwLockReadGuard::rwlock(&guard).clone(); + /// drop(guard); + /// let guard = mutex.read_owned().await; + /// println!("2. contains: {:?}", *guard); + /// guard + /// } + /// # #[tokio::main] + /// # async fn main() { + /// # let rwlock = Arc::new(RwLock::new(0u32)); + /// # let guard = rwlock.read_owned().await; + /// # unlock_and_relock(guard).await; + /// # } + /// ``` + #[inline] + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock + } } impl ops::Deref for OwnedRwLockReadGuard { diff --git a/tokio/src/sync/rwlock/owned_write_guard.rs b/tokio/src/sync/rwlock/owned_write_guard.rs index 91b659524f5..efa11815e43 100644 --- a/tokio/src/sync/rwlock/owned_write_guard.rs +++ b/tokio/src/sync/rwlock/owned_write_guard.rs @@ -192,6 +192,32 @@ impl OwnedRwLockWriteGuard { _p: PhantomData, } } + + /// Returns a reference to the original `Arc` + /// + /// ``` + /// # use std::sync::Arc; + /// # use tokio::sync::{OwnedRwLockWriteGuard, RwLock}; + /// async fn unlock_and_relock(guard: OwnedRwLockWriteGuard) -> OwnedRwLockWriteGuard + /// { + /// println!("1. contains: {:?}", *guard); + /// let mutex: Arc> = OwnedRwLockWriteGuard::rwlock(&guard).clone(); + /// drop(guard); + /// let guard = mutex.write_owned().await; + /// println!("2. contains: {:?}", *guard); + /// guard + /// } + /// # #[tokio::main] + /// # async fn main() { + /// # let rwlock = Arc::new(RwLock::new(0u32)); + /// # let guard = rwlock.write_owned().await; + /// # unlock_and_relock(guard).await; + /// # } + /// ``` + #[inline] + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock + } } impl ops::Deref for OwnedRwLockWriteGuard {