tokio/sync/mpsc/
error.rs

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