use std::{
ops::Deref,
sync::Arc,
};
use tokio::sync::Mutex as AsyncMutex;
use crate::lock::{
AsyncLock,
TryLockError,
};
pub struct ArcAsyncMutex<T> {
inner: Arc<AsyncMutex<T>>,
}
impl<T> ArcAsyncMutex<T> {
#[inline]
pub fn new(data: T) -> Self {
Self {
inner: Arc::new(AsyncMutex::new(data)),
}
}
}
impl<T> AsRef<AsyncMutex<T>> for ArcAsyncMutex<T> {
#[inline]
fn as_ref(&self) -> &AsyncMutex<T> {
self.inner.as_ref()
}
}
impl<T> Deref for ArcAsyncMutex<T> {
type Target = AsyncMutex<T>;
#[inline]
fn deref(&self) -> &Self::Target {
self.inner.as_ref()
}
}
impl<T> AsyncLock<T> for ArcAsyncMutex<T>
where
T: Send,
{
#[inline]
async fn read<R, F>(&self, f: F) -> R
where
F: FnOnce(&T) -> R + Send,
R: Send,
{
let guard = self.inner.lock().await;
f(&*guard)
}
#[inline]
async fn write<R, F>(&self, f: F) -> R
where
F: FnOnce(&mut T) -> R + Send,
R: Send,
{
let mut guard = self.inner.lock().await;
f(&mut *guard)
}
#[inline]
fn try_read<R, F>(&self, f: F) -> Result<R, TryLockError>
where
F: FnOnce(&T) -> R,
{
self.inner
.try_lock()
.map(|guard| f(&*guard))
.map_err(|_| TryLockError::WouldBlock)
}
#[inline]
fn try_write<R, F>(&self, f: F) -> Result<R, TryLockError>
where
F: FnOnce(&mut T) -> R,
{
self.inner
.try_lock()
.map(|mut guard| f(&mut *guard))
.map_err(|_| TryLockError::WouldBlock)
}
}
impl<T> Clone for ArcAsyncMutex<T> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}