use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::app::UserError;
pub(crate) trait LockExt<T> {
fn read_or_err(&self, ctx: &'static str) -> Result<RwLockReadGuard<'_, T>, UserError>;
fn write_or_err(&self, ctx: &'static str) -> Result<RwLockWriteGuard<'_, T>, UserError>;
}
impl<T> LockExt<T> for RwLock<T> {
fn read_or_err(&self, ctx: &'static str) -> Result<RwLockReadGuard<'_, T>, UserError> {
self.read().map_err(|_| UserError::LockPoisoned(ctx))
}
fn write_or_err(&self, ctx: &'static str) -> Result<RwLockWriteGuard<'_, T>, UserError> {
self.write().map_err(|_| UserError::LockPoisoned(ctx))
}
}