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> fmt::Debug for TrySendError<T> {
40fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41match *self {
42 TrySendError::Full(..) => "Full(..)".fmt(f),
43 TrySendError::Closed(..) => "Closed(..)".fmt(f),
44 }
45 }
46}
4748impl<T> fmt::Display for TrySendError<T> {
49fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
50write!(
51 fmt,
52"{}",
53match self {
54 TrySendError::Full(..) => "no available capacity",
55 TrySendError::Closed(..) => "channel closed",
56 }
57 )
58 }
59}
6061impl<T> Error for TrySendError<T> {}
6263impl<T> From<SendError<T>> for TrySendError<T> {
64fn from(src: SendError<T>) -> TrySendError<T> {
65 TrySendError::Closed(src.0)
66 }
67}
6869// ===== TryRecvError =====
7071/// 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.
76Empty,
77/// The **channel**'s sending half has become disconnected, and there will
78 /// never be any more data received on it.
79Disconnected,
80}
8182impl fmt::Display for TryRecvError {
83fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
84match *self {
85 TryRecvError::Empty => "receiving on an empty channel".fmt(fmt),
86 TryRecvError::Disconnected => "receiving on a closed channel".fmt(fmt),
87 }
88 }
89}
9091impl Error for TryRecvError {}
9293// ===== RecvError =====
9495/// 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(());
100101#[allow(deprecated)]
102impl fmt::Display for RecvError {
103fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
104write!(fmt, "channel closed")
105 }
106}
107108#[allow(deprecated)]
109impl Error for RecvError {}
110111cfg_time! {
112// ===== SendTimeoutError =====
113114#[derive(PartialEq, Eq, Clone, Copy)]
115/// Error returned by [`Sender::send_timeout`](super::Sender::send_timeout)].
116pub 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.
119Timeout(T),
120121/// The receive half of the channel was explicitly closed or has been
122 /// dropped.
123Closed(T),
124 }
125126impl<T> fmt::Debug for SendTimeoutError<T> {
127fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128match *self {
129 SendTimeoutError::Timeout(..) => "Timeout(..)".fmt(f),
130 SendTimeoutError::Closed(..) => "Closed(..)".fmt(f),
131 }
132 }
133 }
134135impl<T> fmt::Display for SendTimeoutError<T> {
136fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
137write!(
138 fmt,
139"{}",
140match self {
141 SendTimeoutError::Timeout(..) => "timed out waiting on send operation",
142 SendTimeoutError::Closed(..) => "channel closed",
143 }
144 )
145 }
146 }
147148impl<T> Error for SendTimeoutError<T> {}
149}