1use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
6
7pub trait LockExt<T: ?Sized> {
8 fn lock_ok(&self) -> MutexGuard<'_, T>;
9}
10
11impl<T: ?Sized> LockExt<T> for Mutex<T> {
12 fn lock_ok(&self) -> MutexGuard<'_, T> {
13 self.lock().unwrap_or_else(|e| e.into_inner())
14 }
15}
16
17pub trait RwLockExt<T: ?Sized> {
18 fn read_ok(&self) -> RwLockReadGuard<'_, T>;
19 fn write_ok(&self) -> RwLockWriteGuard<'_, T>;
20}
21
22impl<T: ?Sized> RwLockExt<T> for RwLock<T> {
23 fn read_ok(&self) -> RwLockReadGuard<'_, T> {
24 self.read().unwrap_or_else(|e| e.into_inner())
25 }
26 fn write_ok(&self) -> RwLockWriteGuard<'_, T> {
27 self.write().unwrap_or_else(|e| e.into_inner())
28 }
29}