Skip to content

Commit fb4fa82

Browse files
committed
sync: add a rwlock() method to owned RwLock guards
Adds a `rwlock()` method returning a reference to the original `Arc<RwLock>` to the `Owned*` guards.
1 parent d51f168 commit fb4fa82

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

tokio/src/sync/rwlock/owned_read_guard.rs

+26
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, guard.rwlock()));
159+
///
160+
/// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0);
161+
/// assert!(Arc::ptr_eq(&lock, guard.rwlock()));
162+
/// # }
163+
/// ```
164+
pub fn rwlock(&self) -> &Arc<RwLock<T>> {
165+
&self.lock
166+
}
141167
}
142168

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

tokio/src/sync/rwlock/owned_write_guard.rs

+20
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;
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, guard.rwlock()));
408+
/// # }
409+
/// ```
410+
pub fn rwlock(&self) -> &Arc<RwLock<T>> {
411+
&self.lock
412+
}
393413
}
394414

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

tokio/src/sync/rwlock/owned_write_guard_mapped.rs

+21
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,27 @@ 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::{RwLock, OwnedRwLockMappedWriteGuard};
166+
///
167+
/// # #[tokio::main]
168+
/// # async fn main() {
169+
/// let lock = Arc::new(RwLock::new(1));
170+
///
171+
/// let guard = lock.clone().write_owned().await;
172+
/// let guard = OwnedRwLockMappedWriteGuard::map(guard, |x| x);
173+
/// assert!(Arc::ptr_eq(&lock, guard.rwlock()));
174+
/// # }
175+
/// ```
176+
pub fn rwlock(&self) -> &Arc<RwLock<T>> {
177+
&self.lock
178+
}
158179
}
159180

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

0 commit comments

Comments
 (0)