use crate::AnyLock;
use std::future::Future;
impl<T> AnyLock<T> for tokio::sync::Mutex<T> {
type ReadGuard<'a> = tokio::sync::MutexGuard<'a, T>
where
T: 'a,
Self: 'a;
type WriteGuard<'a> = tokio::sync::MutexGuard<'a, T>
where
T: 'a,
Self: 'a;
fn read<'a>(&'a self) -> Self::ReadGuard<'a> {
self.blocking_lock()
}
fn write<'a>(&'a self) -> Self::WriteGuard<'a> {
self.blocking_lock()
}
fn async_read<'a>(&'a self) -> Box<dyn Future<Output = Self::ReadGuard<'a>> + 'a> {
Box::new(async move { self.lock().await })
}
fn async_write<'a>(&'a self) -> Box<dyn Future<Output = Self::WriteGuard<'a>> + 'a> {
Box::new(async move { self.lock().await })
}
fn new(inner: T) -> Self {
tokio::sync::Mutex::new(inner)
}
}
pub struct TokioRwLock<T>(tokio::sync::RwLock<T>);
impl<T> AnyLock<T> for TokioRwLock<T> {
type ReadGuard<'a> = tokio::sync::RwLockReadGuard<'a, T>
where
T: 'a,
Self: 'a;
type WriteGuard<'a> = tokio::sync::RwLockWriteGuard<'a, T>
where
T: 'a,
Self: 'a;
fn read<'a>(&'a self) -> Self::ReadGuard<'a> {
self.0.blocking_read()
}
fn write<'a>(&'a self) -> Self::WriteGuard<'a> {
self.0.blocking_write()
}
fn async_read<'a>(&'a self) -> Box<dyn Future<Output = Self::ReadGuard<'a>> + 'a> {
Box::new(async move { self.0.read().await })
}
fn async_write<'a>(&'a self) -> Box<dyn Future<Output = Self::WriteGuard<'a>> + 'a> {
Box::new(async move { self.0.write().await })
}
fn new(inner: T) -> Self {
TokioRwLock(tokio::sync::RwLock::new(inner))
}
}