Skip to content

Commit

Permalink
Add a rwlock() method to owned RwLock guards
Browse files Browse the repository at this point in the history
Adds a `rwlock()` method returning a reference to the original `Arc<RwLock>` to the `Owned*` guards.
  • Loading branch information
r3v2d0g committed Mar 20, 2024
1 parent d51f168 commit 5eb4835
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tokio/src/sync/rwlock/owned_read_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,32 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
resource_span: this.resource_span,
})
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// # 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<RwLock<T>> {
&self.lock
}
}

impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockReadGuard<T, U> {
Expand Down
20 changes: 20 additions & 0 deletions tokio/src/sync/rwlock/owned_write_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,26 @@ impl<T: ?Sized> OwnedRwLockWriteGuard<T> {

guard
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// # 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<RwLock<T>> {
&self.lock
}
}

impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> {
Expand Down
21 changes: 21 additions & 0 deletions tokio/src/sync/rwlock/owned_write_guard_mapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U> {
resource_span: this.resource_span,
})
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// # 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<RwLock<T>> {
&self.lock
}
}

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

0 comments on commit 5eb4835

Please sign in to comment.