1use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
5
6pub trait LockExt {
8 type Guard<'a>
10 where
11 Self: 'a;
12
13 fn lock_or_poisoned(&self) -> Self::Guard<'_>;
15}
16
17impl<T: ?Sized> LockExt for Mutex<T> {
18 type Guard<'a>
19 = MutexGuard<'a, T>
20 where
21 Self: 'a;
22
23 #[allow(clippy::expect_used, clippy::unwrap_used)]
24 fn lock_or_poisoned(&self) -> Self::Guard<'_> {
25 self.lock()
26 .expect("invariant: lock not poisoned (a holder panicked)")
27 }
28}
29
30pub trait RwLockExt {
32 type ReadGuard<'a>
34 where
35 Self: 'a;
36
37 type WriteGuard<'a>
39 where
40 Self: 'a;
41
42 fn read_or_poisoned(&self) -> Self::ReadGuard<'_>;
44
45 fn write_or_poisoned(&self) -> Self::WriteGuard<'_>;
47}
48
49impl<T: ?Sized> RwLockExt for RwLock<T> {
50 type ReadGuard<'a>
51 = RwLockReadGuard<'a, T>
52 where
53 Self: 'a;
54
55 type WriteGuard<'a>
56 = RwLockWriteGuard<'a, T>
57 where
58 Self: 'a;
59
60 #[allow(clippy::expect_used, clippy::unwrap_used)]
61 fn read_or_poisoned(&self) -> Self::ReadGuard<'_> {
62 self.read()
63 .expect("invariant: lock not poisoned (a holder panicked)")
64 }
65
66 #[allow(clippy::expect_used, clippy::unwrap_used)]
67 fn write_or_poisoned(&self) -> Self::WriteGuard<'_> {
68 self.write()
69 .expect("invariant: lock not poisoned (a holder panicked)")
70 }
71}