sync_extra/
lib.rs

1use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
2
3pub trait MutexExtra {
4    type Value: ?Sized;
5
6    fn lock_unwrap(&self) -> MutexGuard<'_, Self::Value>;
7}
8
9impl<T: ?Sized> MutexExtra for Mutex<T> {
10    type Value = T;
11
12    fn lock_unwrap(&self) -> MutexGuard<'_, Self::Value> {
13        self.lock().expect("lock is poisioned")
14    }
15}
16
17pub trait RwLockExtra {
18    type Value: ?Sized;
19
20    fn read_unwrap(&self) -> RwLockReadGuard<'_, Self::Value>;
21    fn write_unwrap(&self) -> RwLockWriteGuard<'_, Self::Value>;
22}
23
24impl<T: ?Sized> RwLockExtra for RwLock<T> {
25    type Value = T;
26
27    fn read_unwrap(&self) -> RwLockReadGuard<'_, Self::Value> {
28        self.read().expect("lock is poisioned")
29    }
30
31    fn write_unwrap(&self) -> RwLockWriteGuard<'_, Self::Value> {
32        self.write().expect("lock is poisioned")
33    }
34}