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
use std::fmt;
use std::fmt::Display;
use std::error::Error;
#[allow(unused_imports)] // for doc links
use crate::Semaphore;

/// An error returned by [`Semaphore::acquire`] and [`Semaphore::acquire_arc`] to indicate the
/// Semaphore has been poisoned. See [`Semaphore::acquire_arc`] for an example usage.
#[derive(Debug, Eq, Ord, PartialOrd, PartialEq, Clone, Copy, Hash, Default)]
pub struct PoisonError;

impl Error for PoisonError {}

impl Display for PoisonError {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> fmt::Result {
        Display::fmt("the semaphore has been poisoned", f)
    }
}

/// An error returned from [`Semaphore::try_acquire`] and [`Semaphore::try_acquire_arc`]. See
/// [`Semaphore::try_acquire_arc`] for an example usage.
#[derive(Debug, Eq, Ord, PartialOrd, PartialEq, Clone, Copy, Hash)]
pub enum TryAcquireError {
    /// [`Semaphore::try_acquire`] failed because [`Semaphore::acquire`] would have blocked. Either
    /// there are insufficient available permits or there is another pending call to acquire.
    WouldBlock,
    /// [`Semaphore::try_acquire`] failed because the `Semaphore` is poisoned.
    Poisoned,
}

impl Error for TryAcquireError {}

impl Display for TryAcquireError {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            TryAcquireError::WouldBlock => Display::fmt("the call to acquire would have blocked", f),
            TryAcquireError::Poisoned => Display::fmt(&PoisonError, f),
        }
    }
}

impl From<PoisonError> for TryAcquireError {
    fn from(_: PoisonError) -> Self {
        TryAcquireError::Poisoned
    }
}