From 4565b81097e8938761431592c0ad36df3bd20cd2 Mon Sep 17 00:00:00 2001 From: "Matthieu Le brazidec (r3v2d0g)" Date: Sun, 24 Mar 2024 16:37:05 +0100 Subject: [PATCH] sync: add a `rwlock()` method to owned `RwLock` guards (#6418) --- tokio/src/sync/rwlock/owned_read_guard.rs | 26 +++++++++++++++++++ tokio/src/sync/rwlock/owned_write_guard.rs | 20 ++++++++++++++ .../sync/rwlock/owned_write_guard_mapped.rs | 25 ++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/tokio/src/sync/rwlock/owned_read_guard.rs b/tokio/src/sync/rwlock/owned_read_guard.rs index 273e7b86f..f50b2abca 100644 --- a/tokio/src/sync/rwlock/owned_read_guard.rs +++ b/tokio/src/sync/rwlock/owned_read_guard.rs @@ -138,6 +138,32 @@ impl OwnedRwLockReadGuard { resource_span: this.resource_span, }) } + + /// Returns a reference to the original `Arc`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use tokio::sync::{RwLock, OwnedRwLockReadGuard}; + /// + /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] + /// struct Foo(u32); + /// + /// # #[tokio::main] + /// # async fn main() { + /// let lock = Arc::new(RwLock::new(Foo(1))); + /// + /// let guard = lock.clone().read_owned().await; + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockReadGuard::rwlock(&guard))); + /// + /// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0); + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockReadGuard::rwlock(&guard))); + /// # } + /// ``` + 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 a8ce4a160..11be26a9b 100644 --- a/tokio/src/sync/rwlock/owned_write_guard.rs +++ b/tokio/src/sync/rwlock/owned_write_guard.rs @@ -390,6 +390,26 @@ impl OwnedRwLockWriteGuard { guard } + + /// Returns a reference to the original `Arc`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard}; + /// + /// # #[tokio::main] + /// # async fn main() { + /// let lock = Arc::new(RwLock::new(1)); + /// + /// let guard = lock.clone().write_owned().await; + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockWriteGuard::rwlock(&guard))); + /// # } + /// ``` + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock + } } impl ops::Deref for OwnedRwLockWriteGuard { diff --git a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs index 9f4952100..e0699d097 100644 --- a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs +++ b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs @@ -155,6 +155,31 @@ impl OwnedRwLockMappedWriteGuard { resource_span: this.resource_span, }) } + + /// Returns a reference to the original `Arc`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use tokio::sync::{ + /// RwLock, + /// OwnedRwLockWriteGuard, + /// OwnedRwLockMappedWriteGuard, + /// }; + /// + /// # #[tokio::main] + /// # async fn main() { + /// 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))); + /// # } + /// ``` + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock + } } impl ops::Deref for OwnedRwLockMappedWriteGuard {