Skip to content

Commit 4565b81

Browse files
authored
sync: add a rwlock() method to owned RwLock guards (#6418)
1 parent 3ce4720 commit 4565b81

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

tokio/src/sync/rwlock/owned_read_guard.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
138138
resource_span: this.resource_span,
139139
})
140140
}
141+
142+
/// Returns a reference to the original `Arc<RwLock>`.
143+
///
144+
/// # Examples
145+
///
146+
/// ```
147+
/// use std::sync::Arc;
148+
/// use tokio::sync::{RwLock, OwnedRwLockReadGuard};
149+
///
150+
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
151+
/// struct Foo(u32);
152+
///
153+
/// # #[tokio::main]
154+
/// # async fn main() {
155+
/// let lock = Arc::new(RwLock::new(Foo(1)));
156+
///
157+
/// let guard = lock.clone().read_owned().await;
158+
/// assert!(Arc::ptr_eq(&lock, OwnedRwLockReadGuard::rwlock(&guard)));
159+
///
160+
/// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0);
161+
/// assert!(Arc::ptr_eq(&lock, OwnedRwLockReadGuard::rwlock(&guard)));
162+
/// # }
163+
/// ```
164+
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
165+
&this.lock
166+
}
141167
}
142168

143169
impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockReadGuard<T, U> {

tokio/src/sync/rwlock/owned_write_guard.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,26 @@ impl<T: ?Sized> OwnedRwLockWriteGuard<T> {
390390

391391
guard
392392
}
393+
394+
/// Returns a reference to the original `Arc<RwLock>`.
395+
///
396+
/// # Examples
397+
///
398+
/// ```
399+
/// use std::sync::Arc;
400+
/// use tokio::sync::{RwLock, OwnedRwLockWriteGuard};
401+
///
402+
/// # #[tokio::main]
403+
/// # async fn main() {
404+
/// let lock = Arc::new(RwLock::new(1));
405+
///
406+
/// let guard = lock.clone().write_owned().await;
407+
/// assert!(Arc::ptr_eq(&lock, OwnedRwLockWriteGuard::rwlock(&guard)));
408+
/// # }
409+
/// ```
410+
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
411+
&this.lock
412+
}
393413
}
394414

395415
impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> {

tokio/src/sync/rwlock/owned_write_guard_mapped.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,31 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U> {
155155
resource_span: this.resource_span,
156156
})
157157
}
158+
159+
/// Returns a reference to the original `Arc<RwLock>`.
160+
///
161+
/// # Examples
162+
///
163+
/// ```
164+
/// use std::sync::Arc;
165+
/// use tokio::sync::{
166+
/// RwLock,
167+
/// OwnedRwLockWriteGuard,
168+
/// OwnedRwLockMappedWriteGuard,
169+
/// };
170+
///
171+
/// # #[tokio::main]
172+
/// # async fn main() {
173+
/// let lock = Arc::new(RwLock::new(1));
174+
///
175+
/// let guard = lock.clone().write_owned().await;
176+
/// let guard = OwnedRwLockWriteGuard::map(guard, |x| x);
177+
/// assert!(Arc::ptr_eq(&lock, OwnedRwLockMappedWriteGuard::rwlock(&guard)));
178+
/// # }
179+
/// ```
180+
pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
181+
&this.lock
182+
}
158183
}
159184

160185
impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockMappedWriteGuard<T, U> {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy