1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Access to the data of services.

use std::cell::{Cell, RefCell};
use std::ops::Deref;
use std::rc::Rc;
use std::sync::{Arc, Mutex, RwLock, TryLockError};

///////////////////////////////////////////////////////////////////////////////
// Poisoning Support
///////////////////////////////////////////////////////////////////////////////

/// Indicates whether an instance is poisoned or not.
///
/// More information about poisoning in the [Nomicon].
///
/// How to use this:
/// * For pointer types that don't support poisoning, use [`assert_healthy`].
/// * When it's a hard bug if the value is poisoned, use [`assert_healthy`].
/// * When poisoning status doesn't matter, use [`unpoison`].
/// * When you need different logic for poisoned or not, use a match statement.
///
/// [Nomicon]: https://doc.rust-lang.org/nomicon/poisoning.html
/// [`assert_healthy`]: Poisoning::assert_healthy
/// [`unpoison`]: Poisoning::unpoison
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Poisoning<S> {
    /// The instance is not poisoned, program flow can continue as usual.
    Healthy(S),
    /// The instance is poisoned, extra care should be taken when handling the
    /// value.
    Poisoned(S),
}

impl<S> Poisoning<S> {
    /// Returns the instance if it is not poisoned, panics if it is poisoned.
    #[track_caller]
    pub fn assert_healthy(self) -> S {
        match self {
            Self::Healthy(value) => value,
            Self::Poisoned(..) => panic!("Shared instance is poisoned"),
        }
    }

    /// Returns the instance if it is poisoned, panics if it is not poisoned.
    #[track_caller]
    pub fn assert_poisoned(self) -> S {
        match self {
            Self::Poisoned(value) => value,
            Self::Healthy(..) => panic!("Shared instance is not poisoned"),
        }
    }

    /// Always returns the instance, whether it's poisoned or not.
    ///
    /// For pointer types that don't support poisoning, prefer 
    /// [`assert_healthy`], as this won't introduce hidden bugs when the 
    /// pointer type is changed at a later time.
    ///
    /// Only use this if you're certain that it doesn't matter if the value
    /// is poisoned.
    ///
    /// [`assert_healthy`]: Poisoning::assert_healthy
    pub fn unpoison(self) -> S {
        match self {
            Self::Healthy(value) => value,
            Self::Poisoned(value) => value,
        }
    }

    /// Returns `true` if the instance is [`Healthy`].
    ///
    /// [`Healthy`]: Poisoning::Healthy
    pub const fn is_healthy(&self) -> bool {
        matches!(self, Self::Healthy(..))
    }

    /// Returns `true` if the instance is [`Poisoned`].
    ///
    /// [`Poisoned`]: Poisoning::Poisoned
    pub const fn is_poisoned(&self) -> bool {
        matches!(self, Self::Poisoned(..))
    }

    /// Returns `Some(&S)` if the value is not poisoned, returns `None` if it
    /// is poisoned.
    pub const fn as_healthy(&self) -> Option<&S> {
        match self {
            Self::Healthy(v) => Some(v),
            Self::Poisoned(..) => None
        }
    }

    /// Returns `Some(&S)` if the value is poisoned, returns `None` if it is 
    /// not poisoned.
    pub const fn as_poisoned(&self) -> Option<&S> {
        match self {
            Self::Poisoned(v) => Some(v),
            Self::Healthy(..) => None
        }
    }

    /// Returns `Some(&mut S)` if the value is not poisoned, returns `None` if
    /// it is poisoned.
    pub fn as_healthy_mut(&mut self) -> Option<&mut S> {
        match self {
            Self::Healthy(v) => Some(v),
            Self::Poisoned(..) => None
        }
    }

    /// Returns `Some(&mut S)` if the value is poisoned, returns `None` if it
    /// is not poisoned.
    pub fn as_poisoned_mut(&mut self) -> Option<&mut S> {
        match self {
            Self::Poisoned(v) => Some(v),
            Self::Healthy(..) => None
        }
    }

    /// Converts the value into `Some(S)` if it is not poisoned. Converterts
    /// the value into `None` if it is poisoned.
    pub fn into_healthy(self) -> Option<S> {
        match self {
            Self::Healthy(v) => Some(v),
            Self::Poisoned(..) => None
        }
    }

    /// Converts the value into `Some(S)` if it is poisoned. Converterts the
    /// value into `None` if it is not poisoned.
    pub fn into_poisoned(self) -> Option<S> {
        match self {
            Self::Poisoned(v) => Some(v),
            Self::Healthy(..) => None
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// Traits
///////////////////////////////////////////////////////////////////////////////

/// Provides access to a shared instance.
pub trait IAccess {
    /// The actual type of the instance.
    type Target: ?Sized;

    /// Tries to get access to the shared instance through a closure.
    ///
    /// Returns `None` if the access failed, for example if the shared instance 
    /// is already locked or mutably borrowed.
    ///
    /// The parameter of the closure contains the poisoning status of the
    /// instance.
    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U>;

    /// Get access to the shared instance through a closure.
    ///
    /// The parameter of the closure contains the poisoning status of the
    /// instance.
    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U;
}

/// Provides mutable access to a shared instance.
pub trait IAccessMut: IAccess {
    /// Tries to get mutable access to the shared instance through a closure.
    ///
    /// Returns `None` if the access failed, for example if the shared instance is
    /// already locked or mutably borrowed.
    ///
    /// The parameter of the closure contains the poisoning status of the
    /// instance.
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U>;

    /// Get mutable access to the shared instance through a closure.
    ///
    /// The parameter of the closure contains the poisoning status of the
    /// instance.
    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U;
}

///////////////////////////////////////////////////////////////////////////////
// Helper Types
///////////////////////////////////////////////////////////////////////////////

/// Wrapper to make a type accessable through the `IAccess` trait.
///
/// Note: this makes the type read-only.
#[repr(transparent)]
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Access<T: ?Sized>(T);

impl<T> Access<T> {
    /// Creates a new `Access` wrapper around some value.
    pub const fn new(inner: T) -> Self {
        Self(inner)
    }

    /// Removes the `Access` wrapper and returns the original value.
    pub fn into_inner(self) -> T {
        self.0
    }

    /// Returns a reference to the inner value.
    pub const fn inner(&self) -> &T {
        &self.0
    }
}

impl<T> Deref for Access<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

///////////////////////////////////////////////////////////////////////////////
// IAccess Implementations
///////////////////////////////////////////////////////////////////////////////

impl<T> IAccess for Access<T> {
    type Target = T;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        Some(f(Poisoning::Healthy(self.inner())))
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        f(Poisoning::Healthy(self.inner()))
    }
}

impl<T: ?Sized> IAccess for RefCell<T> {
    type Target = T;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        match self.try_borrow() {
            Ok(bor) => Some(f(Poisoning::Healthy(&bor))),
            Err(..) => None,
        }
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        f(Poisoning::Healthy(&self.borrow()))
    }
}

impl<T: ?Sized + Copy> IAccess for Cell<T> {
    type Target = T;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        Some(self.access(f))
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        f(Poisoning::Healthy(&self.get()))
    }
}

impl<T: ?Sized> IAccess for Mutex<T> {
    type Target = T;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        match self.try_lock() {
            Ok(lock) => Some(f(Poisoning::Healthy(&lock))),
            Err(TryLockError::Poisoned(lock)) => Some(f(Poisoning::Poisoned(&lock.into_inner()))),
            Err(..) => None,
        }
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        match self.lock() {
            Ok(lock) => f(Poisoning::Healthy(&lock)),
            Err(poison) => f(Poisoning::Poisoned(&poison.into_inner())),
        }
    }
}

impl<T: ?Sized> IAccess for RwLock<T> {
    type Target = T;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        match self.try_read() {
            Ok(read) => Some(f(Poisoning::Healthy(&read))),
            Err(TryLockError::Poisoned(lock)) => Some(f(Poisoning::Poisoned(&lock.into_inner()))),
            Err(..) => None,
        }
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        match self.read() {
            Ok(read) => f(Poisoning::Healthy(&read)),
            Err(poison) => f(Poisoning::Poisoned(&poison.into_inner())),
        }
    }
}

impl<T: ?Sized + IAccess> IAccess for Rc<T> {
    type Target = T::Target;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        self.deref().try_access(f)
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        self.deref().access(f)
    }
}

impl<T: ?Sized + IAccess> IAccess for Arc<T> {
    type Target = T::Target;

    fn try_access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> Option<U> {
        self.deref().try_access(f)
    }

    fn access<U, F: FnOnce(Poisoning<&Self::Target>) -> U>(&self, f: F) -> U {
        self.deref().access(f)
    }
}

///////////////////////////////////////////////////////////////////////////////
// IAccessMut Implementations
///////////////////////////////////////////////////////////////////////////////

impl<T: ?Sized> IAccessMut for RefCell<T> {
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U> {
        match self.try_borrow_mut() {
            Ok(mut bor) => Some(f(Poisoning::Healthy(&mut bor))),
            Err(..) => None,
        }
    }

    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U {
        f(Poisoning::Healthy(&mut self.borrow_mut()))
    }
}

impl<T: ?Sized + Copy> IAccessMut for Cell<T> {
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U> {
        let mut value = self.get();
        let output = f(Poisoning::Healthy(&mut value));
        self.set(value);
        Some(output)
    }

    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U {
        let mut value = self.get();
        let output = f(Poisoning::Healthy(&mut value));
        self.set(value);
        output
    }
}

impl<T: ?Sized> IAccessMut for Mutex<T> {
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U> {
        match self.try_lock() {
            Ok(mut lock) => Some(f(Poisoning::Healthy(&mut lock))),
            Err(TryLockError::Poisoned(lock)) => {
                Some(f(Poisoning::Poisoned(&mut lock.into_inner())))
            }
            Err(..) => None,
        }
    }

    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U {
        match self.lock() {
            Ok(mut lock) => f(Poisoning::Healthy(&mut lock)),
            Err(poison) => f(Poisoning::Poisoned(&mut poison.into_inner())),
        }
    }
}

impl<T: ?Sized> IAccessMut for RwLock<T> {
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U> {
        match self.try_write() {
            Ok(mut write) => Some(f(Poisoning::Healthy(&mut write))),
            Err(TryLockError::Poisoned(poison)) => {
                Some(f(Poisoning::Poisoned(&mut poison.into_inner())))
            }
            Err(..) => None,
        }
    }

    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U {
        match self.write() {
            Ok(mut write) => f(Poisoning::Healthy(&mut write)),
            Err(poison) => f(Poisoning::Poisoned(&mut poison.into_inner())),
        }
    }
}

impl<T: ?Sized + IAccessMut> IAccessMut for Rc<T> {
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U> {
        self.deref().try_access_mut(f)
    }

    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U {
        self.deref().access_mut(f)
    }
}

impl<T: ?Sized + IAccessMut> IAccessMut for Arc<T> {
    fn try_access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> Option<U> {
        self.deref().try_access_mut(f)
    }

    fn access_mut<U, F: FnOnce(Poisoning<&mut Self::Target>) -> U>(&self, f: F) -> U {
        self.deref().access_mut(f)
    }
}

///////////////////////////////////////////////////////////////////////////////
// Tests
///////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn poisoning_assert_healthy() {
        let poison = Poisoning::Healthy(321);
        let value = poison.assert_healthy();
        assert_eq!(value, 321);
    }


    #[test]
    #[should_panic]
    fn poisoning_assert_healthy_panic() {
        let poison = Poisoning::Poisoned(100);
        let _value = poison.assert_healthy();
    }

    #[test]
    fn poisoning_unpoison() {
        let poison = Poisoning::Healthy(321);
        let value = poison.unpoison();
        assert_eq!(value, 321);

        let poison = Poisoning::Poisoned(123);
        let value = poison.unpoison();
        assert_eq!(value, 123);
    }

    #[test]
    fn poisoning_is_poisoned() {
        let poison = Poisoning::Healthy(321);
        let is_poisoned = poison.is_poisoned();
        assert_eq!(is_poisoned, false);

        let poison = Poisoning::Poisoned(123);
        let is_poisoned = poison.is_poisoned();
        assert_eq!(is_poisoned, true);
    }

    #[test]
    fn poisoning_is_healthy() {
        let poison = Poisoning::Healthy(321);
        let is_poisoned = poison.is_healthy();
        assert_eq!(is_poisoned, true);

        let poison = Poisoning::Poisoned(123);
        let is_poisoned = poison.is_healthy();
        assert_eq!(is_poisoned, false);
    }
}