swap_buffer_queue/
error.rs

1//! Queue error types.
2
3use core::fmt;
4
5/// Error returned by [`Queue::try_enqueue`](crate::Queue::try_enqueue).
6///
7/// The value whose enqueuing has failed is embedded within the error.
8#[derive(Copy, Clone, Eq, PartialEq)]
9pub enum TryEnqueueError<T> {
10    /// The queue doesn't have sufficient capacity to enqueue the give value.
11    InsufficientCapacity(T),
12    /// The queue is closed.
13    Closed(T),
14}
15
16impl<T> TryEnqueueError<T> {
17    /// Returns the value whose enqueuing has failed
18    pub fn into_inner(self) -> T {
19        match self {
20            Self::InsufficientCapacity(v) | Self::Closed(v) => v,
21        }
22    }
23}
24
25impl<T> fmt::Debug for TryEnqueueError<T> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::InsufficientCapacity(_) => write!(f, "TryEnqueueError::InsufficientCapacity(_)"),
29            Self::Closed(_) => write!(f, "TryEnqueueError::Closed(_)"),
30        }
31    }
32}
33
34impl<T> fmt::Display for TryEnqueueError<T> {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        let error = match self {
37            Self::InsufficientCapacity(_) => "queue has insufficient capacity",
38            Self::Closed(_) => "queue is closed",
39        };
40        write!(f, "{error}")
41    }
42}
43
44#[cfg(feature = "std")]
45impl<T> std::error::Error for TryEnqueueError<T> {}
46
47/// Error returned by [`SynchronizedQueue::enqueue`](crate::SynchronizedQueue::enqueue)/[`SynchronizedQueue::enqueue_async`](crate::SynchronizedQueue::enqueue_async)
48pub type EnqueueError<T> = TryEnqueueError<T>;
49
50/// Error returned by [`Queue::try_dequeue`](crate::Queue::try_dequeue).
51#[derive(Debug, Copy, Clone, Eq, PartialEq)]
52pub enum TryDequeueError {
53    /// The queue is empty.
54    Empty,
55    /// There is a concurrent enqueuing that need to end before dequeuing.
56    Pending,
57    /// The queue is closed.
58    Closed,
59    /// The queue is concurrently dequeued.
60    Conflict,
61}
62
63impl fmt::Display for TryDequeueError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        let error = match self {
66            Self::Empty => "queue is empty",
67            Self::Pending => "waiting for concurrent enqueuing end",
68            Self::Closed => "queue is closed",
69            Self::Conflict => "queue is concurrently dequeued",
70        };
71        write!(f, "{error}")
72    }
73}
74
75#[cfg(feature = "std")]
76impl std::error::Error for TryDequeueError {}
77
78/// Error returned by [`SynchronizedQueue::dequeue`](crate::SynchronizedQueue::dequeue)/
79/// [`SynchronizedQueue::dequeue_async`](crate::SynchronizedQueue::dequeue_async).
80#[derive(Debug, Copy, Clone, Eq, PartialEq)]
81pub enum DequeueError {
82    /// The queue is closed.
83    Closed,
84    /// The queue is concurrently dequeued.
85    Conflict,
86}
87
88impl fmt::Display for DequeueError {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        let error = match self {
91            Self::Closed => "queue is closed",
92            Self::Conflict => "queue is concurrently dequeued",
93        };
94        write!(f, "{error}")
95    }
96}
97
98#[cfg(feature = "std")]
99impl std::error::Error for DequeueError {}