Skip to main content

lensbench_core/
sync.rs

1//! 毒(中で panic した後)を無視して取る Mutex・RwLock
2//!
3//! serve・GUI は panic を拾って続けるので、毒で panic すると同じ錠を使う所が以後ずっと止まる。中身は
4//! 書き換えの途中の値でも、覚え書き・進み具合・登録の表など読み直せる値だけを置く前提
5use 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}