async_weighted_semaphore/
errors.rs

1use std::fmt;
2use std::fmt::Display;
3use std::error::Error;
4#[allow(unused_imports)] // for doc links
5use crate::Semaphore;
6
7/// An error returned by [`Semaphore::acquire`] and [`Semaphore::acquire_arc`] to indicate the
8/// Semaphore has been poisoned. See [`Semaphore::acquire_arc`] for an example usage.
9#[derive(Debug, Eq, Ord, PartialOrd, PartialEq, Clone, Copy, Hash, Default)]
10pub struct PoisonError;
11
12impl Error for PoisonError {}
13
14impl Display for PoisonError {
15    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> fmt::Result {
16        Display::fmt("the semaphore has been poisoned", f)
17    }
18}
19
20/// An error returned from [`Semaphore::try_acquire`] and [`Semaphore::try_acquire_arc`]. See
21/// [`Semaphore::try_acquire_arc`] for an example usage.
22#[derive(Debug, Eq, Ord, PartialOrd, PartialEq, Clone, Copy, Hash)]
23pub enum TryAcquireError {
24    /// [`Semaphore::try_acquire`] failed because [`Semaphore::acquire`] would have blocked. Either
25    /// there are insufficient available permits or there is another pending call to acquire.
26    WouldBlock,
27    /// [`Semaphore::try_acquire`] failed because the `Semaphore` is poisoned.
28    Poisoned,
29}
30
31impl Error for TryAcquireError {}
32
33impl Display for TryAcquireError {
34    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
35        match self {
36            TryAcquireError::WouldBlock => Display::fmt("the call to acquire would have blocked", f),
37            TryAcquireError::Poisoned => Display::fmt(&PoisonError, f),
38        }
39    }
40}
41
42impl From<PoisonError> for TryAcquireError {
43    fn from(_: PoisonError) -> Self {
44        TryAcquireError::Poisoned
45    }
46}