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] new(t: T) -> Mutex<T>11 pub(crate) fn new(t: T) -> Mutex<T> { 12 Mutex(sync::Mutex::new(t)) 13 } 14 15 #[inline] lock(&self) -> MutexGuard<'_, T>16 pub(crate) fn lock(&self) -> MutexGuard<'_, T> { 17 match self.0.lock() { 18 Ok(guard) => guard, 19 Err(p_err) => p_err.into_inner(), 20 } 21 } 22 23 #[inline] try_lock(&self) -> Option<MutexGuard<'_, T>>24 pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { 25 match self.0.try_lock() { 26 Ok(guard) => Some(guard), 27 Err(TryLockError::Poisoned(p_err)) => Some(p_err.into_inner()), 28 Err(TryLockError::WouldBlock) => None, 29 } 30 } 31 } 32