1//! Channel error types.
23use std::error::Error;
4use std::fmt;
56/// Error returned by the `Sender`.
7#[derive(PartialEq, Eq, Clone, Copy)]
8pub struct SendError<T>(pub T);
910impl<T> fmt::Debug for SendError<T> {
11fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12 f.debug_struct("SendError").finish_non_exhaustive()
13 }
14}
1516impl<T> fmt::Display for SendError<T> {
17fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
18write!(fmt, "channel closed")
19 }
20}
2122impl<T> Error for SendError<T> {}
2324// ===== TrySendError =====
2526/// 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.
32Full(T),
3334/// The receive half of the channel was explicitly closed or has been
35 /// dropped.
36Closed(T),
37}
3839impl<T> TrySendError<T> {
40/// Consume the `TrySendError`, returning the unsent value.
41pub fn into_inner(self) -> T {
42match self {
43 TrySendError::Full(val) => val,
44 TrySendError::Closed(val) => val,
45 }
46 }
47}
4849impl<T> fmt::Debug for TrySendError<T> {
50fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51match *self {
52 TrySendError::Full(..) => "Full(..)".fmt(f),
53 TrySendError::Closed(..) => "Closed(..)".fmt(f),
54 }
55 }
56}
5758impl<T> fmt::Display for TrySendError<T> {
59fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
60write!(
61 fmt,
62"{}",
63match self {
64 TrySendError::Full(..) => "no available capacity",
65 TrySendError::Closed(..) => "channel closed",
66 }
67 )
68 }
69}
7071impl<T> Error for TrySendError<T> {}
7273impl<T> From<SendError<T>> for TrySendError<T> {
74fn from(src: SendError<T>) -> TrySendError<T> {
75 TrySendError::Closed(src.0)
76 }
77}
7879// ===== TryRecvError =====
8081/// 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.
86Empty,
87/// The **channel**'s sending half has become disconnected, and there will
88 /// never be any more data received on it.
89Disconnected,
90}
9192impl fmt::Display for TryRecvError {
93fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
94match *self {
95 TryRecvError::Empty => "receiving on an empty channel".fmt(fmt),
96 TryRecvError::Disconnected => "receiving on a closed channel".fmt(fmt),
97 }
98 }
99}
100101impl Error for TryRecvError {}
102103// ===== RecvError =====
104105/// 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(());
110111#[allow(deprecated)]
112impl fmt::Display for RecvError {
113fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
114write!(fmt, "channel closed")
115 }
116}
117118#[allow(deprecated)]
119impl Error for RecvError {}
120121cfg_time! {
122// ===== SendTimeoutError =====
123124#[derive(PartialEq, Eq, Clone, Copy)]
125/// Error returned by [`Sender::send_timeout`](super::Sender::send_timeout)].
126pub 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.
129Timeout(T),
130131/// The receive half of the channel was explicitly closed or has been
132 /// dropped.
133Closed(T),
134 }
135136impl<T> SendTimeoutError<T> {
137/// Consume the `SendTimeoutError`, returning the unsent value.
138pub fn into_inner(self) -> T {
139match self {
140 SendTimeoutError::Timeout(val) => val,
141 SendTimeoutError::Closed(val) => val,
142 }
143 }
144 }
145146impl<T> fmt::Debug for SendTimeoutError<T> {
147fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148match *self {
149 SendTimeoutError::Timeout(..) => "Timeout(..)".fmt(f),
150 SendTimeoutError::Closed(..) => "Closed(..)".fmt(f),
151 }
152 }
153 }
154155impl<T> fmt::Display for SendTimeoutError<T> {
156fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
157write!(
158 fmt,
159"{}",
160match self {
161 SendTimeoutError::Timeout(..) => "timed out waiting on send operation",
162 SendTimeoutError::Closed(..) => "channel closed",
163 }
164 )
165 }
166 }
167168impl<T> Error for SendTimeoutError<T> {}
169}