use core::ErrorKind;
use core::Result;
use std::borrow::Cow;
use std::sync::Mutex;
use std::sync::MutexGuard;
use std::sync::PoisonError;
use std::sync::RwLock;
use std::sync::RwLockReadGuard;
use std::sync::RwLockWriteGuard;
pub(crate) trait RwLockExt<T> {
fn read_clean<Desc>(&self, description: Desc) -> Result<RwLockReadGuard<T>>
where
Desc: Into<Cow<'static, str>>;
fn write_clean<Desc>(&self, description: Desc) -> Result<RwLockWriteGuard<T>>
where
Desc: Into<Cow<'static, str>>;
}
impl<T> RwLockExt<T> for RwLock<T> {
fn read_clean<Desc>(&self, description: Desc) -> Result<RwLockReadGuard<T>>
where
Desc: Into<Cow<'static, str>>,
{
self.read()
.map_err(|PoisonError { .. }| ErrorKind::LockPoisoned(description.into().into()).into())
}
fn write_clean<Desc>(&self, description: Desc) -> Result<RwLockWriteGuard<T>>
where
Desc: Into<Cow<'static, str>>,
{
self.write()
.map_err(|PoisonError { .. }| ErrorKind::LockPoisoned(description.into().into()).into())
}
}
pub(crate) trait MutexExt<T> {
fn lock_clean<Desc>(&self, description: Desc) -> Result<MutexGuard<T>>
where
Desc: Into<Cow<'static, str>>;
}
impl<T> MutexExt<T> for Mutex<T> {
fn lock_clean<Desc>(&self, description: Desc) -> Result<MutexGuard<T>>
where
Desc: Into<Cow<'static, str>>,
{
self.lock()
.map_err(|PoisonError { .. }| ErrorKind::LockPoisoned(description.into().into()).into())
}
}