use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub trait LockExt {
type Guard<'a>
where
Self: 'a;
fn lock_or_poisoned(&self) -> Self::Guard<'_>;
}
impl<T: ?Sized> LockExt for Mutex<T> {
type Guard<'a>
= MutexGuard<'a, T>
where
Self: 'a;
#[allow(clippy::expect_used, clippy::unwrap_used)]
fn lock_or_poisoned(&self) -> Self::Guard<'_> {
self.lock()
.expect("invariant: lock not poisoned (a holder panicked)")
}
}
pub trait RwLockExt {
type ReadGuard<'a>
where
Self: 'a;
type WriteGuard<'a>
where
Self: 'a;
fn read_or_poisoned(&self) -> Self::ReadGuard<'_>;
fn write_or_poisoned(&self) -> Self::WriteGuard<'_>;
}
impl<T: ?Sized> RwLockExt for RwLock<T> {
type ReadGuard<'a>
= RwLockReadGuard<'a, T>
where
Self: 'a;
type WriteGuard<'a>
= RwLockWriteGuard<'a, T>
where
Self: 'a;
#[allow(clippy::expect_used, clippy::unwrap_used)]
fn read_or_poisoned(&self) -> Self::ReadGuard<'_> {
self.read()
.expect("invariant: lock not poisoned (a holder panicked)")
}
#[allow(clippy::expect_used, clippy::unwrap_used)]
fn write_or_poisoned(&self) -> Self::WriteGuard<'_> {
self.write()
.expect("invariant: lock not poisoned (a holder panicked)")
}
}