flag_bearer_queue/
closeable.rs

1use crate::acquire::AcquireError;
2
3pub(crate) mod private {
4    pub trait Sealed {
5        const CLOSE: bool;
6        type Closed<P>;
7    }
8}
9
10/// Whether the semaphore is closeable
11pub trait IsCloseable: private::Sealed {
12    /// The error returned by [`acquire`](crate::acquire::Acquire).
13    /// It will be a variant of [`AcquireError`].
14    ///
15    /// * [`Closeable`] semaphores return a [`AcquireError<P>`].
16    /// * [`Uncloseable`] semaphores return [`AcquireError<Uncloseable>`].
17    type AcquireError<P>;
18
19    #[doc(hidden)]
20    fn new_err<P>(params: P) -> Self::AcquireError<P>;
21    #[doc(hidden)]
22    fn map_err<P, R>(p: Self::Closed<P>, f: impl FnOnce(P) -> R) -> Self::AcquireError<R>;
23}
24
25/// Controls whether the [`SemaphoreQueue`](crate::SemaphoreQueue) is closeable.
26pub enum Closeable {}
27
28/// Controls whether the [`SemaphoreQueue`](crate::SemaphoreQueue) is not closeable.
29pub enum Uncloseable {}
30
31impl private::Sealed for Closeable {
32    const CLOSE: bool = true;
33    type Closed<P> = P;
34}
35
36impl IsCloseable for Closeable {
37    type AcquireError<P> = AcquireError<P>;
38
39    #[doc(hidden)]
40    fn new_err<P>(params: P) -> Self::AcquireError<P> {
41        AcquireError { params }
42    }
43    #[doc(hidden)]
44    fn map_err<P, R>(p: Self::Closed<P>, f: impl FnOnce(P) -> R) -> Self::AcquireError<R> {
45        AcquireError { params: f(p) }
46    }
47}
48
49impl private::Sealed for Uncloseable {
50    const CLOSE: bool = false;
51    type Closed<P> = Self;
52}
53
54impl IsCloseable for Uncloseable {
55    type AcquireError<P> = AcquireError<Uncloseable>;
56
57    #[doc(hidden)]
58    fn new_err<P>(_params: P) -> Self::AcquireError<P> {
59        unreachable!()
60    }
61    #[doc(hidden)]
62    fn map_err<P, R>(p: Self::Closed<P>, _f: impl FnOnce(P) -> R) -> Self::AcquireError<R> {
63        match p {}
64    }
65}
66
67impl core::fmt::Display for Uncloseable {
68    fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        match *self {}
70    }
71}
72
73impl core::fmt::Debug for Uncloseable {
74    fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75        match *self {}
76    }
77}
78
79impl core::error::Error for Uncloseable {}