1 | use std::sync::{self, MutexGuard, TryLockError}; |
2 | |
3 | /// Adapter for `std::Mutex` that removes the poisoning aspects |
4 | /// from its api. |
5 | #[derive (Debug)] |
6 | pub(crate) struct Mutex<T: ?Sized>(sync::Mutex<T>); |
7 | |
8 | #[allow (dead_code)] |
9 | impl<T> Mutex<T> { |
10 | #[inline ] |
11 | pub(crate) fn new(t: T) -> Mutex<T> { |
12 | Mutex(sync::Mutex::new(t)) |
13 | } |
14 | |
15 | #[inline ] |
16 | #[cfg (not(tokio_no_const_mutex_new))] |
17 | pub(crate) const fn const_new(t: T) -> Mutex<T> { |
18 | Mutex(sync::Mutex::new(t)) |
19 | } |
20 | |
21 | #[inline ] |
22 | pub(crate) fn lock(&self) -> MutexGuard<'_, T> { |
23 | match self.0.lock() { |
24 | Ok(guard) => guard, |
25 | Err(p_err) => p_err.into_inner(), |
26 | } |
27 | } |
28 | |
29 | #[inline ] |
30 | pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { |
31 | match self.0.try_lock() { |
32 | Ok(guard) => Some(guard), |
33 | Err(TryLockError::Poisoned(p_err)) => Some(p_err.into_inner()), |
34 | Err(TryLockError::WouldBlock) => None, |
35 | } |
36 | } |
37 | } |
38 | |