Skip to main content

imago/sync_primitives/
async_impl.rs

1//! Wrappers around `tokio::sync` lock types, where necessary.
2
3use std::sync::Arc;
4use tokio::sync::{OwnedRwLockReadGuard, OwnedRwLockWriteGuard};
5
6/// Read-write lock whose guards own the lock, so a guard can outlive its owner.
7#[derive(Debug, Default)]
8pub(crate) struct OwnedRwLock<T>(Arc<tokio::sync::RwLock<T>>);
9
10impl<T> Clone for OwnedRwLock<T> {
11    fn clone(&self) -> Self {
12        OwnedRwLock(Arc::clone(&self.0))
13    }
14}
15
16impl<T> OwnedRwLock<T> {
17    /// Acquire an owned read lock.
18    pub async fn read(&self) -> OwnedRwLockReadGuard<T> {
19        Arc::clone(&self.0).read_owned().await
20    }
21
22    /// Acquire an owned write lock.
23    pub async fn write(&self) -> OwnedRwLockWriteGuard<T> {
24        Arc::clone(&self.0).write_owned().await
25    }
26}