| 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 | pub(crate) const fn const_new(t: T) -> Mutex<T> { |
| 17 | Mutex(sync::Mutex::new(t)) |
| 18 | } |
| 19 | |
| 20 | #[inline ] |
| 21 | pub(crate) fn lock(&self) -> MutexGuard<'_, T> { |
| 22 | match self.0.lock() { |
| 23 | Ok(guard) => guard, |
| 24 | Err(p_err) => p_err.into_inner(), |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #[inline ] |
| 29 | pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { |
| 30 | match self.0.try_lock() { |
| 31 | Ok(guard) => Some(guard), |
| 32 | Err(TryLockError::Poisoned(p_err)) => Some(p_err.into_inner()), |
| 33 | Err(TryLockError::WouldBlock) => None, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[inline ] |
| 38 | pub(crate) fn get_mut(&mut self) -> &mut T { |
| 39 | match self.0.get_mut() { |
| 40 | Ok(val) => val, |
| 41 | Err(p_err) => p_err.into_inner(), |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |