sync: add a rwlock() method to owned RwLock guards (#6418)

This commit is contained in:
Matthieu Le brazidec (r3v2d0g) 2024-03-24 16:37:05 +01:00 committed by GitHub
parent 3ce4720a45
commit 4565b81097
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 71 additions and 0 deletions

View File

@ -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> {

View File

@ -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> {

View File

@ -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> {