Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_platform/src/sync/poison.rs
6849 views
1
//! Provides `LockResult`, `PoisonError`, `TryLockError`, `TryLockResult`
2
3
pub use implementation::{LockResult, PoisonError, TryLockError, TryLockResult};
4
5
#[cfg(feature = "std")]
6
use std::sync as implementation;
7
8
#[cfg(not(feature = "std"))]
9
mod implementation {
10
use core::{error::Error, fmt};
11
12
/// Fallback implementation of `PoisonError` from the standard library.
13
pub struct PoisonError<T> {
14
guard: T,
15
}
16
17
impl<T> fmt::Debug for PoisonError<T> {
18
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19
f.debug_struct("PoisonError").finish_non_exhaustive()
20
}
21
}
22
23
impl<T> fmt::Display for PoisonError<T> {
24
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25
"poisoned lock: another task failed inside".fmt(f)
26
}
27
}
28
29
impl<T> Error for PoisonError<T> {}
30
31
impl<T> PoisonError<T> {
32
/// Creates a `PoisonError`.
33
///
34
/// See the standard library for further details.
35
#[cfg(panic = "unwind")]
36
pub fn new(guard: T) -> PoisonError<T> {
37
PoisonError { guard }
38
}
39
40
/// Consumes this error indicating that a lock is poisoned, returning the
41
/// underlying guard to allow access regardless.
42
///
43
/// See the standard library for further details.
44
pub fn into_inner(self) -> T {
45
self.guard
46
}
47
48
/// Reaches into this error indicating that a lock is poisoned, returning a
49
/// reference to the underlying guard to allow access regardless.
50
///
51
/// See the standard library for further details.
52
pub fn get_ref(&self) -> &T {
53
&self.guard
54
}
55
56
/// Reaches into this error indicating that a lock is poisoned, returning a
57
/// mutable reference to the underlying guard to allow access regardless.
58
///
59
/// See the standard library for further details.
60
pub fn get_mut(&mut self) -> &mut T {
61
&mut self.guard
62
}
63
}
64
65
/// Fallback implementation of `TryLockError` from the standard library.
66
pub enum TryLockError<T> {
67
/// The lock could not be acquired because another thread failed while holding
68
/// the lock.
69
Poisoned(PoisonError<T>),
70
/// The lock could not be acquired at this time because the operation would
71
/// otherwise block.
72
WouldBlock,
73
}
74
75
impl<T> From<PoisonError<T>> for TryLockError<T> {
76
fn from(err: PoisonError<T>) -> TryLockError<T> {
77
TryLockError::Poisoned(err)
78
}
79
}
80
81
impl<T> fmt::Debug for TryLockError<T> {
82
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83
match *self {
84
TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f),
85
TryLockError::WouldBlock => "WouldBlock".fmt(f),
86
}
87
}
88
}
89
90
impl<T> fmt::Display for TryLockError<T> {
91
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92
match *self {
93
TryLockError::Poisoned(..) => "poisoned lock: another task failed inside",
94
TryLockError::WouldBlock => "try_lock failed because the operation would block",
95
}
96
.fmt(f)
97
}
98
}
99
100
impl<T> Error for TryLockError<T> {}
101
102
/// Fallback implementation of `LockResult` from the standard library.
103
pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;
104
105
/// Fallback implementation of `TryLockResult` from the standard library.
106
pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
107
}
108
109