pub trait MutexLock {
type Error<'a>
where
Self: 'a;
type Guard<'a>
where
Self: 'a;
fn lock(&self) -> Result<Self::Guard<'_>, Self::Error<'_>>;
}
#[cfg(feature = "std")]
mod std {
use std::sync::{Mutex, MutexGuard, PoisonError};
impl<T: ?Sized> super::MutexLock for Mutex<T> {
type Guard<'a> = MutexGuard<'a, T> where Self: 'a;
type Error<'a> = PoisonError<MutexGuard<'a, T>> where Self: 'a;
fn lock(&self) -> Result<Self::Guard<'_>, Self::Error<'_>> {
Mutex::lock(self)
}
}
}
#[cfg(feature = "parking_lot")]
mod parking_lot {
use core::convert::Infallible;
use parking_lot::{Mutex, MutexGuard};
impl<T: ?Sized> super::MutexLock for Mutex<T> {
type Guard<'a> = MutexGuard<'a, T> where Self: 'a;
type Error<'a> = Infallible where Self: 'a;
fn lock(&self) -> Result<Self::Guard<'_>, Self::Error<'_>> {
Ok(Mutex::lock(self))
}
}
}
#[cfg(feature = "async")]
pub trait AsyncMutexLock {
type Guard<'a>
where
Self: 'a;
fn lock(&self) -> impl core::future::Future<Output = Self::Guard<'_>>;
}
#[cfg(feature = "futures")]
mod futures {
use futures::lock::{Mutex, MutexGuard};
impl<T: ?Sized> super::AsyncMutexLock for Mutex<T> {
type Guard<'a> = MutexGuard<'a, T>
where
Self: 'a;
async fn lock(&self) -> Self::Guard<'_> {
Mutex::lock(self).await
}
}
}
#[cfg(feature = "tokio")]
mod tokio {
use tokio::sync::{Mutex, MutexGuard};
impl<T: ?Sized> super::AsyncMutexLock for Mutex<T> {
type Guard<'a> = MutexGuard<'a, T>
where
Self: 'a;
async fn lock(&self) -> Self::Guard<'_> {
Mutex::lock(self).await
}
}
}