Skip to content

Commit

Permalink
sync: add a rwlock() method to owned RwLock guards (#6418)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v2d0g authored Mar 24, 2024
1 parent 3ce4720 commit 4565b81
Show file tree
Hide file tree
Showing 3 changed files with 71 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, 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<RwLock<T>> {
&this.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, 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<RwLock<T>> {
&this.lock
}
}

impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> {
Expand Down
25 changes: 25 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,31 @@ 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,
/// 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<RwLock<T>> {
&this.lock
}
}

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

0 comments on commit 4565b81

Please sign in to comment.