mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-25 12:00:35 +00:00
sync: add a rwlock()
method to owned RwLock
guards (#6418)
This commit is contained in:
parent
3ce4720a45
commit
4565b81097
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user