ranked_semaphore/
error.rs

1//! Error types for ranked semaphore operations.
2
3use std::fmt;
4
5/// Error returned from acquire operations when the semaphore has been closed.
6#[derive(Debug)]
7pub struct AcquireError(());
8
9/// Error returned from try_acquire operations.
10#[derive(Debug, PartialEq, Eq)]
11pub enum TryAcquireError {
12    /// The semaphore has been closed and cannot issue new permits.
13    Closed,
14    /// The semaphore has no available permits.
15    NoPermits,
16}
17
18impl AcquireError {
19    pub(crate) fn closed() -> AcquireError {
20        AcquireError(())
21    }
22}
23
24impl fmt::Display for AcquireError {
25    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(fmt, "semaphore closed")
27    }
28}
29
30impl std::error::Error for AcquireError {}
31
32impl TryAcquireError {
33    /// Returns `true` if the error was caused by a closed semaphore.
34    pub fn is_closed(&self) -> bool {
35        matches!(self, TryAcquireError::Closed)
36    }
37
38    /// Returns `true` if the error was caused by insufficient permits.
39    pub fn is_no_permits(&self) -> bool {
40        matches!(self, TryAcquireError::NoPermits)
41    }
42}
43
44impl fmt::Display for TryAcquireError {
45    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            TryAcquireError::Closed => write!(fmt, "semaphore closed"),
48            TryAcquireError::NoPermits => write!(fmt, "no permits available"),
49        }
50    }
51}
52
53impl std::error::Error for TryAcquireError {}