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(PartialEq, Eq, Clone, Copy)]
8pub struct SendError<T>(pub T);
9
10impl<T> fmt::Debug for SendError<T> {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        f.debug_struct("SendError").finish_non_exhaustive()
13    }
14}
15
16impl<T> fmt::Display for SendError<T> {
17    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
18        write!(fmt, "channel closed")
19    }
20}
21
22impl<T> Error for SendError<T> {}
23
24// ===== TrySendError =====
25
26/// This enumeration is the list of the possible error outcomes for the
27/// [`try_send`](super::Sender::try_send) method.
28#[derive(PartialEq, Eq, Clone, Copy)]
29pub enum TrySendError<T> {
30    /// The data could not be sent on the channel because the channel is
31    /// currently full and sending would require blocking.
32    Full(T),
33
34    /// The receive half of the channel was explicitly closed or has been
35    /// dropped.
36    Closed(T),
37}
38
39impl<T> TrySendError<T> {
40    /// Consume the `TrySendError`, returning the unsent value.
41    pub fn into_inner(self) -> T {
42        match self {
43            TrySendError::Full(val) => val,
44            TrySendError::Closed(val) => val,
45        }
46    }
47}
48
49impl<T> fmt::Debug for TrySendError<T> {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match *self {
52            TrySendError::Full(..) => "Full(..)".fmt(f),
53            TrySendError::Closed(..) => "Closed(..)".fmt(f),
54        }
55    }
56}
57
58impl<T> fmt::Display for TrySendError<T> {
59    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(
61            fmt,
62            "{}",
63            match self {
64                TrySendError::Full(..) => "no available capacity",
65                TrySendError::Closed(..) => "channel closed",
66            }
67        )
68    }
69}
70
71impl<T> Error for TrySendError<T> {}
72
73impl<T> From<SendError<T>> for TrySendError<T> {
74    fn from(src: SendError<T>) -> TrySendError<T> {
75        TrySendError::Closed(src.0)
76    }
77}
78
79// ===== TryRecvError =====
80
81/// Error returned by `try_recv`.
82#[derive(PartialEq, Eq, Clone, Copy, Debug)]
83pub enum TryRecvError {
84    /// This **channel** is currently empty, but the **Sender**(s) have not yet
85    /// disconnected, so data may yet become available.
86    Empty,
87    /// The **channel**'s sending half has become disconnected, and there will
88    /// never be any more data received on it.
89    Disconnected,
90}
91
92impl fmt::Display for TryRecvError {
93    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
94        match *self {
95            TryRecvError::Empty => "receiving on an empty channel".fmt(fmt),
96            TryRecvError::Disconnected => "receiving on a closed channel".fmt(fmt),
97        }
98    }
99}
100
101impl Error for TryRecvError {}
102
103// ===== RecvError =====
104
105/// Error returned by `Receiver`.
106#[derive(Debug, Clone)]
107#[doc(hidden)]
108#[deprecated(note = "This type is unused because recv returns an Option.")]
109pub struct RecvError(());
110
111#[allow(deprecated)]
112impl fmt::Display for RecvError {
113    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
114        write!(fmt, "channel closed")
115    }
116}
117
118#[allow(deprecated)]
119impl Error for RecvError {}
120
121cfg_time! {
122    // ===== SendTimeoutError =====
123
124    #[derive(PartialEq, Eq, Clone, Copy)]
125    /// Error returned by [`Sender::send_timeout`](super::Sender::send_timeout)].
126    pub enum SendTimeoutError<T> {
127        /// The data could not be sent on the channel because the channel is
128        /// full, and the timeout to send has elapsed.
129        Timeout(T),
130
131        /// The receive half of the channel was explicitly closed or has been
132        /// dropped.
133        Closed(T),
134    }
135
136    impl<T> SendTimeoutError<T> {
137        /// Consume the `SendTimeoutError`, returning the unsent value.
138        pub fn into_inner(self) -> T {
139            match self {
140                SendTimeoutError::Timeout(val) => val,
141                SendTimeoutError::Closed(val) => val,
142            }
143        }
144    }
145
146    impl<T> fmt::Debug for SendTimeoutError<T> {
147        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148            match *self {
149                SendTimeoutError::Timeout(..) => "Timeout(..)".fmt(f),
150                SendTimeoutError::Closed(..) => "Closed(..)".fmt(f),
151            }
152        }
153    }
154
155    impl<T> fmt::Display for SendTimeoutError<T> {
156        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
157            write!(
158                fmt,
159                "{}",
160                match self {
161                    SendTimeoutError::Timeout(..) => "timed out waiting on send operation",
162                    SendTimeoutError::Closed(..) => "channel closed",
163                }
164            )
165        }
166    }
167
168    impl<T> Error for SendTimeoutError<T> {}
169}