//! Provides `LockResult`, `PoisonError`, `TryLockError`, `TryLockResult` pub use implementation::{LockResult, PoisonError, TryLockError, TryLockResult}; #[cfg(feature = "std")] use std::sync as implementation; #[cfg(not(feature = "std"))] mod implementation { use core::{error::Error, fmt}; /// Fallback implementation of `PoisonError` from the standard library. pub struct PoisonError { guard: T, } impl fmt::Debug for PoisonError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("PoisonError").finish_non_exhaustive() } } impl fmt::Display for PoisonError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { "poisoned lock: another task failed inside".fmt(f) } } impl Error for PoisonError {} impl PoisonError { /// Creates a `PoisonError`. /// /// See the standard library for further details. #[cfg(panic = "unwind")] pub fn new(guard: T) -> PoisonError { PoisonError { guard } } /// Consumes this error indicating that a lock is poisoned, returning the /// underlying guard to allow access regardless. /// /// See the standard library for further details. pub fn into_inner(self) -> T { self.guard } /// Reaches into this error indicating that a lock is poisoned, returning a /// reference to the underlying guard to allow access regardless. /// /// See the standard library for further details. pub fn get_ref(&self) -> &T { &self.guard } /// Reaches into this error indicating that a lock is poisoned, returning a /// mutable reference to the underlying guard to allow access regardless. /// /// See the standard library for further details. pub fn get_mut(&mut self) -> &mut T { &mut self.guard } } /// Fallback implementation of `TryLockError` from the standard library. pub enum TryLockError { /// The lock could not be acquired because another thread failed while holding /// the lock. Poisoned(PoisonError), /// The lock could not be acquired at this time because the operation would /// otherwise block. WouldBlock, } impl From> for TryLockError { fn from(err: PoisonError) -> TryLockError { TryLockError::Poisoned(err) } } impl fmt::Debug for TryLockError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f), TryLockError::WouldBlock => "WouldBlock".fmt(f), } } } impl fmt::Display for TryLockError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { TryLockError::Poisoned(..) => "poisoned lock: another task failed inside", TryLockError::WouldBlock => "try_lock failed because the operation would block", } .fmt(f) } } impl Error for TryLockError {} /// Fallback implementation of `LockResult` from the standard library. pub type LockResult = Result>; /// Fallback implementation of `TryLockResult` from the standard library. pub type TryLockResult = Result>; }