• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Channel error types.
2 
3 use std::error::Error;
4 use std::fmt;
5 
6 /// Error returned by the `Sender`.
7 #[derive(PartialEq, Eq, Clone, Copy)]
8 pub struct SendError<T>(pub T);
9 
10 impl<T> fmt::Debug for SendError<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result11     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12         f.debug_struct("SendError").finish_non_exhaustive()
13     }
14 }
15 
16 impl<T> fmt::Display for SendError<T> {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result17     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
18         write!(fmt, "channel closed")
19     }
20 }
21 
22 impl<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)]
29 pub 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 
39 impl<T> TrySendError<T> {
40     /// Consume the `TrySendError`, returning the unsent value.
into_inner(self) -> T41     pub fn into_inner(self) -> T {
42         match self {
43             TrySendError::Full(val) => val,
44             TrySendError::Closed(val) => val,
45         }
46     }
47 }
48 
49 impl<T> fmt::Debug for TrySendError<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result50     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 
58 impl<T> fmt::Display for TrySendError<T> {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result59     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 
71 impl<T> Error for TrySendError<T> {}
72 
73 impl<T> From<SendError<T>> for TrySendError<T> {
from(src: SendError<T>) -> 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)]
83 pub 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 
92 impl fmt::Display for TryRecvError {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result93     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 
101 impl 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.")]
109 pub struct RecvError(());
110 
111 #[allow(deprecated)]
112 impl fmt::Display for RecvError {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result113     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
114         write!(fmt, "channel closed")
115     }
116 }
117 
118 #[allow(deprecated)]
119 impl Error for RecvError {}
120 
121 cfg_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 }
170