broker_tokio/sync/mpsc/
error.rs

1//! Channel error types
2
3use std::error::Error;
4use std::fmt;
5
6/// Error returned by the `Sender`.
7#[derive(Debug)]
8pub struct SendError<T>(pub T);
9
10impl<T> fmt::Display for SendError<T> {
11    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
12        write!(fmt, "channel closed")
13    }
14}
15
16impl<T: fmt::Debug> std::error::Error for SendError<T> {}
17
18// ===== TrySendError =====
19
20/// This enumeration is the list of the possible error outcomes for the
21/// [try_send](super::Sender::try_send) method.
22#[derive(Debug)]
23pub enum TrySendError<T> {
24    /// The data could not be sent on the channel because the channel is
25    /// currently full and sending would require blocking.
26    Full(T),
27
28    /// The receive half of the channel was explicitly closed or has been
29    /// dropped.
30    Closed(T),
31}
32
33impl<T: fmt::Debug> Error for TrySendError<T> {}
34
35impl<T> fmt::Display for TrySendError<T> {
36    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
37        write!(
38            fmt,
39            "{}",
40            match self {
41                TrySendError::Full(..) => "no available capacity",
42                TrySendError::Closed(..) => "channel closed",
43            }
44        )
45    }
46}
47
48impl<T> From<SendError<T>> for TrySendError<T> {
49    fn from(src: SendError<T>) -> TrySendError<T> {
50        TrySendError::Closed(src.0)
51    }
52}
53
54// ===== RecvError =====
55
56/// Error returned by `Receiver`.
57#[derive(Debug)]
58pub struct RecvError(());
59
60impl fmt::Display for RecvError {
61    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
62        write!(fmt, "channel closed")
63    }
64}
65
66impl Error for RecvError {}
67
68// ===== TryRecvError =====
69
70/// This enumeration is the list of the possible reasons that try_recv
71/// could not return data when called.
72#[derive(Debug, PartialEq)]
73pub enum TryRecvError {
74    /// This channel is currently empty, but the Sender(s) have not yet
75    /// disconnected, so data may yet become available.
76    Empty,
77    /// The channel's sending half has been closed, and there will
78    /// never be any more data received on it.
79    Closed,
80}
81
82impl fmt::Display for TryRecvError {
83    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(
85            fmt,
86            "{}",
87            match self {
88                TryRecvError::Empty => "channel empty",
89                TryRecvError::Closed => "channel closed",
90            }
91        )
92    }
93}
94
95impl Error for TryRecvError {}
96
97// ===== ClosedError =====
98
99/// Erorr returned by [`Sender::poll_ready`](super::Sender::poll_ready)].
100#[derive(Debug)]
101pub struct ClosedError(());
102
103impl ClosedError {
104    pub(crate) fn new() -> ClosedError {
105        ClosedError(())
106    }
107}
108
109impl fmt::Display for ClosedError {
110    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
111        write!(fmt, "channel closed")
112    }
113}
114
115impl Error for ClosedError {}