Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Mutex and RwLock types that do not poison themselves.
2 //!
3 //! These types expose identical APIs to the standard library `Mutex` and
4 //! `RwLock` except that they do not return `PoisonError`s.
5 #![doc(html_root_url="https://sfackler.github.io/rust-antidote/doc/v1.0.0")]
6 #![warn(missing_docs)]
7
8 use std::error::Error;
9 use std::fmt;
10 use std::ops::{Deref, DerefMut};
11 use std::sync;
12 use std::time::Duration;
13
14 #[doc(inline)]
15 pub use std::sync::WaitTimeoutResult;
16
17 /// Like `std::sync::Mutex` except that it does not poison itself.
18 pub struct Mutex<T: ?Sized>(sync::Mutex<T>);
19
20 impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
21 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
22 fmt::Debug::fmt(&self.0, fmt)
23 }
24 }
25
26 impl<T> Mutex<T> {
27 /// Like `std::sync::Mutex::new`.
28 #[inline]
29 pub fn new(t: T) -> Mutex<T> {
30 Mutex(sync::Mutex::new(t))
31 }
32
33 /// Like `std::sync::Mutex::into_inner`.
34 #[inline]
35 pub fn into_inner(self) -> T {
36 self.0.into_inner().unwrap_or_else(|e| e.into_inner())
37 }
38 }
39
40 impl<T: ?Sized> Mutex<T> {
41 /// Like `std::sync::Mutex::lock`.
42 #[inline]
43 pub fn lock<'a>(&'a self) -> MutexGuard<'a, T> {
44 MutexGuard(self.0.lock().unwrap_or_else(|e| e.into_inner()))
45 }
46
47 /// Like `std::sync::Mutex::try_lock`.
48 #[inline]
49 pub fn try_lock<'a>(&'a self) -> TryLockResult<MutexGuard<'a, T>> {
50 match self.0.try_lock() {
51 Ok(t) => Ok(MutexGuard(t)),
52 Err(sync::TryLockError::Poisoned(e)) => Ok(MutexGuard(e.into_inner())),
53 Err(sync::TryLockError::WouldBlock) => Err(TryLockError(())),
54 }
55 }
56
57 /// Like `std::sync::Mutex::get_mut`.
58 #[inline]
59 pub fn get_mut(&mut self) -> &mut T {
60 self.0.get_mut().unwrap_or_else(|e| e.into_inner())
61 }
62 }
63
64 /// Like `std::sync::MutexGuard`.
65 #[must_use]
66 pub struct MutexGuard<'a, T: ?Sized + 'a>(sync::MutexGuard<'a, T>);
67
68 impl<'a, T: ?Sized> Deref for MutexGuard<'a, T> {
69 type Target = T;
70
71 #[inline]
72 fn deref(&self) -> &T {
73 self.0.deref()
74 }
75 }
76
77 impl<'a, T: ?Sized> DerefMut for MutexGuard<'a, T> {
78 #[inline]
79 fn deref_mut(&mut self) -> &mut T {
80 self.0.deref_mut()
81 }
82 }
83
84 /// Like `std::sync::Condvar`.
85 pub struct Condvar(sync::Condvar);
86
87 impl Condvar {
88 /// Like `std::sync::Condvar::new`.
89 #[inline]
90 pub fn new() -> Condvar {
91 Condvar(sync::Condvar::new())
92 }
93
94 /// Like `std::sync::Condvar::wait`.
95 #[inline]
96 pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
97 MutexGuard(self.0.wait(guard.0).unwrap_or_else(|e| e.into_inner()))
98 }
99
100 /// Like `std::sync::Condvar::wait_timeout`.
101 #[inline]
102 pub fn wait_timeout<'a, T>(&self,
103 guard: MutexGuard<'a, T>,
104 dur: Duration)
105 -> (MutexGuard<'a, T>, WaitTimeoutResult) {
106 let (guard, result) = self.0.wait_timeout(guard.0, dur).unwrap_or_else(|e| e.into_inner());
107 (MutexGuard(guard), result)
108 }
109
110 /// Like `std::sync::Condvar::notify_one`.
111 #[inline]
112 pub fn notify_one(&self) {
113 self.0.notify_one()
114 }
115
116 /// Like `std::sync::Condvar::notify_all`.
117 #[inline]
118 pub fn notify_all(&self) {
119 self.0.notify_all()
120 }
121 }
122
123 /// Like `std::sync::TryLockResult`.
124 pub type TryLockResult<T> = Result<T, TryLockError>;
125
126 /// Like `std::sync::TryLockError`.
127 #[derive(Debug)]
128 pub struct TryLockError(());
129
130 impl fmt::Display for TryLockError {
131 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
132 fmt.write_str(self.description())
133 }
134 }
135
136 impl Error for TryLockError {
137 fn description(&self) -> &str {
138 "lock call failed because the operation would block"
139 }
140 }
141
142 /// Like `std::sync::RwLock` except that it does not poison itself.
143 pub struct RwLock<T: ?Sized>(sync::RwLock<T>);
144
145 impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
146 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
147 fmt::Debug::fmt(&self.0, fmt)
148 }
149 }
150
151 impl<T> RwLock<T> {
152 /// Like `std::sync::RwLock::new`.
153 #[inline]
154 pub fn new(t: T) -> RwLock<T> {
155 RwLock(sync::RwLock::new(t))
156 }
157
158 /// Like `std::sync::RwLock::into_inner`.
159 #[inline]
160 pub fn into_inner(self) -> T where T: Sized {
161 self.0.into_inner().unwrap_or_else(|e| e.into_inner())
162 }
163 }
164
165 impl<T: ?Sized> RwLock<T> {
166 /// Like `std::sync::RwLock::read`.
167 #[inline]
168 pub fn read<'a>(&'a self) -> RwLockReadGuard<'a, T> {
169 RwLockReadGuard(self.0.read().unwrap_or_else(|e| e.into_inner()))
170 }
171
172 /// Like `std::sync::RwLock::try_read`.
173 #[inline]
174 pub fn try_read<'a>(&'a self) -> TryLockResult<RwLockReadGuard<'a, T>> {
175 match self.0.try_read() {
176 Ok(t) => Ok(RwLockReadGuard(t)),
177 Err(sync::TryLockError::Poisoned(e)) => Ok(RwLockReadGuard(e.into_inner())),
178 Err(sync::TryLockError::WouldBlock) => Err(TryLockError(())),
179 }
180 }
181
182 /// Like `std::sync::RwLock::write`.
183 #[inline]
184 pub fn write<'a>(&'a self) -> RwLockWriteGuard<'a, T> {
185 RwLockWriteGuard(self.0.write().unwrap_or_else(|e| e.into_inner()))
186 }
187
188 /// Like `std::sync::RwLock::try_write`.
189 #[inline]
190 pub fn try_write<'a>(&'a self) -> TryLockResult<RwLockWriteGuard<'a, T>> {
191 match self.0.try_write() {
192 Ok(t) => Ok(RwLockWriteGuard(t)),
193 Err(sync::TryLockError::Poisoned(e)) => Ok(RwLockWriteGuard(e.into_inner())),
194 Err(sync::TryLockError::WouldBlock) => Err(TryLockError(())),
195 }
196 }
197
198 /// Like `std::sync::RwLock::get_mut`.
199 #[inline]
200 pub fn get_mut(&mut self) -> &mut T {
201 self.0.get_mut().unwrap_or_else(|e| e.into_inner())
202 }
203 }
204
205 /// Like `std::sync::RwLockReadGuard`.
206 #[must_use]
207 pub struct RwLockReadGuard<'a, T: ?Sized + 'a>(sync::RwLockReadGuard<'a, T>);
208
209 impl<'a, T: ?Sized> Deref for RwLockReadGuard<'a, T> {
210 type Target = T;
211
212 #[inline]
213 fn deref(&self) -> &T {
214 self.0.deref()
215 }
216 }
217
218 /// Like `std::sync::RwLockWriteGuard`.
219 #[must_use]
220 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a>(sync::RwLockWriteGuard<'a, T>);
221
222 impl<'a, T: ?Sized> Deref for RwLockWriteGuard<'a, T> {
223 type Target = T;
224
225 #[inline]
226 fn deref(&self) -> &T {
227 self.0.deref()
228 }
229 }
230
231 impl<'a, T: ?Sized> DerefMut for RwLockWriteGuard<'a, T> {
232 #[inline]
233 fn deref_mut(&mut self) -> &mut T {
234 self.0.deref_mut()
235 }
236 }