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> fmt::Debug for TrySendError<T> {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match *self {
42            TrySendError::Full(..) => "Full(..)".fmt(f),
43            TrySendError::Closed(..) => "Closed(..)".fmt(f),
44        }
45    }
46}
47
48impl<T> fmt::Display for TrySendError<T> {
49    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(
51            fmt,
52            "{}",
53            match self {
54                TrySendError::Full(..) => "no available capacity",
55                TrySendError::Closed(..) => "channel closed",
56            }
57        )
58    }
59}
60
61impl<T> Error for TrySendError<T> {}
62
63impl<T> From<SendError<T>> for TrySendError<T> {
64    fn from(src: SendError<T>) -> TrySendError<T> {
65        TrySendError::Closed(src.0)
66    }
67}
68
69// ===== TryRecvError =====
70
71/// Error returned by `try_recv`.
72#[derive(PartialEq, Eq, Clone, Copy, Debug)]
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 become disconnected, and there will
78    /// never be any more data received on it.
79    Disconnected,
80}
81
82impl fmt::Display for TryRecvError {
83    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match *self {
85            TryRecvError::Empty => "receiving on an empty channel".fmt(fmt),
86            TryRecvError::Disconnected => "receiving on a closed channel".fmt(fmt),
87        }
88    }
89}
90
91impl Error for TryRecvError {}
92
93// ===== RecvError =====
94
95/// Error returned by `Receiver`.
96#[derive(Debug, Clone)]
97#[doc(hidden)]
98#[deprecated(note = "This type is unused because recv returns an Option.")]
99pub struct RecvError(());
100
101#[allow(deprecated)]
102impl fmt::Display for RecvError {
103    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
104        write!(fmt, "channel closed")
105    }
106}
107
108#[allow(deprecated)]
109impl Error for RecvError {}
110
111cfg_time! {
112    // ===== SendTimeoutError =====
113
114    #[derive(PartialEq, Eq, Clone, Copy)]
115    /// Error returned by [`Sender::send_timeout`](super::Sender::send_timeout)].
116    pub enum SendTimeoutError<T> {
117        /// The data could not be sent on the channel because the channel is
118        /// full, and the timeout to send has elapsed.
119        Timeout(T),
120
121        /// The receive half of the channel was explicitly closed or has been
122        /// dropped.
123        Closed(T),
124    }
125
126    impl<T> fmt::Debug for SendTimeoutError<T> {
127        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128            match *self {
129                SendTimeoutError::Timeout(..) => "Timeout(..)".fmt(f),
130                SendTimeoutError::Closed(..) => "Closed(..)".fmt(f),
131            }
132        }
133    }
134
135    impl<T> fmt::Display for SendTimeoutError<T> {
136        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
137            write!(
138                fmt,
139                "{}",
140                match self {
141                    SendTimeoutError::Timeout(..) => "timed out waiting on send operation",
142                    SendTimeoutError::Closed(..) => "channel closed",
143                }
144            )
145        }
146    }
147
148    impl<T> Error for SendTimeoutError<T> {}
149}