Path: blob/main/crates/bevy_platform/src/sync/poison.rs
6849 views
//! Provides `LockResult`, `PoisonError`, `TryLockError`, `TryLockResult`12pub use implementation::{LockResult, PoisonError, TryLockError, TryLockResult};34#[cfg(feature = "std")]5use std::sync as implementation;67#[cfg(not(feature = "std"))]8mod implementation {9use core::{error::Error, fmt};1011/// Fallback implementation of `PoisonError` from the standard library.12pub struct PoisonError<T> {13guard: T,14}1516impl<T> fmt::Debug for PoisonError<T> {17fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {18f.debug_struct("PoisonError").finish_non_exhaustive()19}20}2122impl<T> fmt::Display for PoisonError<T> {23fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {24"poisoned lock: another task failed inside".fmt(f)25}26}2728impl<T> Error for PoisonError<T> {}2930impl<T> PoisonError<T> {31/// Creates a `PoisonError`.32///33/// See the standard library for further details.34#[cfg(panic = "unwind")]35pub fn new(guard: T) -> PoisonError<T> {36PoisonError { guard }37}3839/// Consumes this error indicating that a lock is poisoned, returning the40/// underlying guard to allow access regardless.41///42/// See the standard library for further details.43pub fn into_inner(self) -> T {44self.guard45}4647/// Reaches into this error indicating that a lock is poisoned, returning a48/// reference to the underlying guard to allow access regardless.49///50/// See the standard library for further details.51pub fn get_ref(&self) -> &T {52&self.guard53}5455/// Reaches into this error indicating that a lock is poisoned, returning a56/// mutable reference to the underlying guard to allow access regardless.57///58/// See the standard library for further details.59pub fn get_mut(&mut self) -> &mut T {60&mut self.guard61}62}6364/// Fallback implementation of `TryLockError` from the standard library.65pub enum TryLockError<T> {66/// The lock could not be acquired because another thread failed while holding67/// the lock.68Poisoned(PoisonError<T>),69/// The lock could not be acquired at this time because the operation would70/// otherwise block.71WouldBlock,72}7374impl<T> From<PoisonError<T>> for TryLockError<T> {75fn from(err: PoisonError<T>) -> TryLockError<T> {76TryLockError::Poisoned(err)77}78}7980impl<T> fmt::Debug for TryLockError<T> {81fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {82match *self {83TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f),84TryLockError::WouldBlock => "WouldBlock".fmt(f),85}86}87}8889impl<T> fmt::Display for TryLockError<T> {90fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {91match *self {92TryLockError::Poisoned(..) => "poisoned lock: another task failed inside",93TryLockError::WouldBlock => "try_lock failed because the operation would block",94}95.fmt(f)96}97}9899impl<T> Error for TryLockError<T> {}100101/// Fallback implementation of `LockResult` from the standard library.102pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;103104/// Fallback implementation of `TryLockResult` from the standard library.105pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;106}107108109