From fb4fa82a55060952f55f24c74ad90069dd5d4eb0 Mon Sep 17 00:00:00 2001 From: Matthieu Le brazidec Date: Wed, 20 Mar 2024 15:33:20 +0100 Subject: [PATCH 1/3] sync: add a `rwlock()` method to owned `RwLock` guards Adds a `rwlock()` method returning a reference to the original `Arc` to the `Owned*` guards. --- 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 | 21 +++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/tokio/src/sync/rwlock/owned_read_guard.rs b/tokio/src/sync/rwlock/owned_read_guard.rs index 273e7b86f2f..88e667695a0 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, guard.rwlock())); + /// + /// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0); + /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); + /// # } + /// ``` + pub fn rwlock(&self) -> &Arc> { + &self.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 a8ce4a1603f..d40c0350f46 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; + /// + /// # #[tokio::main] + /// # async fn main() { + /// let lock = Arc::new(RwLock::new(1)); + /// + /// let guard = lock.clone().write_owned().await; + /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); + /// # } + /// ``` + pub fn rwlock(&self) -> &Arc> { + &self.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 9f4952100a5..906c62028c6 100644 --- a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs +++ b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs @@ -155,6 +155,27 @@ impl OwnedRwLockMappedWriteGuard { resource_span: this.resource_span, }) } + + /// Returns a reference to the original `Arc`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use tokio::sync::{RwLock, OwnedRwLockMappedWriteGuard}; + /// + /// # #[tokio::main] + /// # async fn main() { + /// let lock = Arc::new(RwLock::new(1)); + /// + /// let guard = lock.clone().write_owned().await; + /// let guard = OwnedRwLockMappedWriteGuard::map(guard, |x| x); + /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); + /// # } + /// ``` + pub fn rwlock(&self) -> &Arc> { + &self.lock + } } impl ops::Deref for OwnedRwLockMappedWriteGuard { From ba948d3a628b81b1d00315c9dc5f55a4c4e52e23 Mon Sep 17 00:00:00 2001 From: Matthieu Le brazidec Date: Wed, 20 Mar 2024 17:53:24 +0100 Subject: [PATCH 2/3] Fix example --- tokio/src/sync/rwlock/owned_write_guard_mapped.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs index 906c62028c6..4b60df9ee8d 100644 --- a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs +++ b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs @@ -162,14 +162,14 @@ impl OwnedRwLockMappedWriteGuard { /// /// ``` /// use std::sync::Arc; - /// use tokio::sync::{RwLock, OwnedRwLockMappedWriteGuard}; + /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard}; /// /// # #[tokio::main] /// # async fn main() { /// let lock = Arc::new(RwLock::new(1)); /// /// let guard = lock.clone().write_owned().await; - /// let guard = OwnedRwLockMappedWriteGuard::map(guard, |x| x); + /// let guard = OwnedRwLockWriteGuard::map(guard, |x| x); /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); /// # } /// ``` From e2b122d87e2a409534654a89c05a26eb5f761bef Mon Sep 17 00:00:00 2001 From: Matthieu Le brazidec Date: Fri, 22 Mar 2024 10:03:36 +0100 Subject: [PATCH 3/3] Use `this: &Self` instead of `&self` --- tokio/src/sync/rwlock/owned_read_guard.rs | 8 ++++---- tokio/src/sync/rwlock/owned_write_guard.rs | 8 ++++---- tokio/src/sync/rwlock/owned_write_guard_mapped.rs | 12 ++++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tokio/src/sync/rwlock/owned_read_guard.rs b/tokio/src/sync/rwlock/owned_read_guard.rs index 88e667695a0..f50b2abcaf5 100644 --- a/tokio/src/sync/rwlock/owned_read_guard.rs +++ b/tokio/src/sync/rwlock/owned_read_guard.rs @@ -155,14 +155,14 @@ impl OwnedRwLockReadGuard { /// let lock = Arc::new(RwLock::new(Foo(1))); /// /// let guard = lock.clone().read_owned().await; - /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockReadGuard::rwlock(&guard))); /// /// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0); - /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockReadGuard::rwlock(&guard))); /// # } /// ``` - pub fn rwlock(&self) -> &Arc> { - &self.lock + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock } } diff --git a/tokio/src/sync/rwlock/owned_write_guard.rs b/tokio/src/sync/rwlock/owned_write_guard.rs index d40c0350f46..11be26a9bad 100644 --- a/tokio/src/sync/rwlock/owned_write_guard.rs +++ b/tokio/src/sync/rwlock/owned_write_guard.rs @@ -397,18 +397,18 @@ impl OwnedRwLockWriteGuard { /// /// ``` /// use std::sync::Arc; - /// use tokio::sync::RwLock; + /// 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, guard.rwlock())); + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockWriteGuard::rwlock(&guard))); /// # } /// ``` - pub fn rwlock(&self) -> &Arc> { - &self.lock + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock } } diff --git a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs index 4b60df9ee8d..e0699d09794 100644 --- a/tokio/src/sync/rwlock/owned_write_guard_mapped.rs +++ b/tokio/src/sync/rwlock/owned_write_guard_mapped.rs @@ -162,7 +162,11 @@ impl OwnedRwLockMappedWriteGuard { /// /// ``` /// use std::sync::Arc; - /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard}; + /// use tokio::sync::{ + /// RwLock, + /// OwnedRwLockWriteGuard, + /// OwnedRwLockMappedWriteGuard, + /// }; /// /// # #[tokio::main] /// # async fn main() { @@ -170,11 +174,11 @@ impl OwnedRwLockMappedWriteGuard { /// /// let guard = lock.clone().write_owned().await; /// let guard = OwnedRwLockWriteGuard::map(guard, |x| x); - /// assert!(Arc::ptr_eq(&lock, guard.rwlock())); + /// assert!(Arc::ptr_eq(&lock, OwnedRwLockMappedWriteGuard::rwlock(&guard))); /// # } /// ``` - pub fn rwlock(&self) -> &Arc> { - &self.lock + pub fn rwlock(this: &Self) -> &Arc> { + &this.lock } }