Skip to main content

crossfire/crossbeam/
err.rs

1//! The MIT License (MIT)
2//!
3//! Copyright (c) 2019 The Crossbeam Project Developers
4//!
5//! Permission is hereby granted, free of charge, to any
6//! person obtaining a copy of this software and associated
7//! documentation files (the "Software"), to deal in the
8//! Software without restriction, including without
9//! limitation the rights to use, copy, modify, merge,
10//! publish, distribute, sublicense, and/or sell copies of
11//! the Software, and to permit persons to whom the Software
12//! is furnished to do so, subject to the following
13//! conditions:
14//!
15//! The above copyright notice and this permission notice
16//! shall be included in all copies or substantial portions
17//! of the Software.
18//!
19//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
20//! ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
21//! TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
22//! PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
23//! SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24//! CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25//! OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
26//! IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27//! DEALINGS IN THE SOFTWARE.
28
29use std::error;
30use std::fmt;
31
32/// An error returned from the `send` method.
33///
34/// The message could not be sent because the channel is disconnected.
35///
36/// The error contains the message so it can be recovered.
37#[derive(PartialEq, Eq, Clone, Copy)]
38pub struct SendError<T>(pub T);
39
40/// An error returned from the `try_send` method.
41///
42/// The error contains the message being sent so it can be recovered.
43#[derive(PartialEq, Eq, Clone, Copy)]
44pub enum TrySendError<T> {
45    /// The message could not be sent because the channel is full.
46    ///
47    /// If this is a zero-capacity channel, then the error indicates that there was no receiver
48    /// available to receive the message at the time.
49    Full(T),
50
51    /// The message could not be sent because the channel is disconnected.
52    Disconnected(T),
53}
54
55/// An error returned from the `send_timeout` method.
56///
57/// The error contains the message being sent so it can be recovered.
58#[derive(PartialEq, Eq, Clone, Copy)]
59pub enum SendTimeoutError<T> {
60    /// The message could not be sent because the channel is full and the operation timed out.
61    ///
62    /// If this is a zero-capacity channel, then the error indicates that there was no receiver
63    /// available to receive the message and the operation timed out.
64    Timeout(T),
65
66    /// The message could not be sent because the channel is disconnected.
67    Disconnected(T),
68}
69
70/// An error returned from the `recv` method.
71///
72/// A message could not be received because the channel is empty and disconnected.
73#[derive(PartialEq, Eq, Clone, Copy, Debug)]
74pub struct RecvError;
75
76/// An error returned from the `try_recv` method.
77#[derive(PartialEq, Eq, Clone, Copy, Debug)]
78pub enum TryRecvError {
79    /// A message could not be received because the channel is empty.
80    ///
81    /// If this is a zero-capacity channel, then the error indicates that there was no sender
82    /// available to send a message at the time.
83    Empty,
84
85    /// The message could not be received because the channel is empty and disconnected.
86    Disconnected,
87}
88
89/// An error returned from the `recv_timeout` method.
90#[derive(PartialEq, Eq, Clone, Copy, Debug)]
91pub enum RecvTimeoutError {
92    /// A message could not be received because the channel is empty and the operation timed out.
93    ///
94    /// If this is a zero-capacity channel, then the error indicates that there was no sender
95    /// available to send a message and the operation timed out.
96    Timeout,
97
98    /// The message could not be received because the channel is empty and disconnected.
99    Disconnected,
100}
101
102impl<T> fmt::Debug for SendError<T> {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        "SendError(..)".fmt(f)
105    }
106}
107
108impl<T> fmt::Display for SendError<T> {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        "sending on a disconnected channel".fmt(f)
111    }
112}
113
114impl<T: Send> error::Error for SendError<T> {}
115
116impl<T> SendError<T> {
117    /// Unwraps the message.
118    ///
119    /// # Examples
120    ///
121    /// ```
122    /// use crossfire::mpmc;
123    ///
124    /// let (s, r) = mpmc::bounded_blocking::<&str>(10);
125    /// drop(r);
126    ///
127    /// if let Err(err) = s.send("foo") {
128    ///     assert_eq!(err.into_inner(), "foo");
129    /// }
130    /// ```
131    pub fn into_inner(self) -> T {
132        self.0
133    }
134}
135
136impl<T> fmt::Debug for TrySendError<T> {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        match *self {
139            Self::Full(..) => "Full(..)".fmt(f),
140            Self::Disconnected(..) => "Disconnected(..)".fmt(f),
141        }
142    }
143}
144
145impl<T> fmt::Display for TrySendError<T> {
146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147        match *self {
148            Self::Full(..) => "sending on a full channel".fmt(f),
149            Self::Disconnected(..) => "sending on a disconnected channel".fmt(f),
150        }
151    }
152}
153
154impl<T: Send> error::Error for TrySendError<T> {}
155
156impl<T> From<SendError<T>> for TrySendError<T> {
157    fn from(err: SendError<T>) -> Self {
158        match err {
159            SendError(t) => Self::Disconnected(t),
160        }
161    }
162}
163
164impl<T> TrySendError<T> {
165    /// Unwraps the message.
166    ///
167    /// # Examples
168    ///
169    /// ```
170    /// use crossfire::mpmc;
171    ///
172    /// let (s, r) = mpmc::bounded_blocking::<&str>(0);
173    ///
174    /// if let Err(err) = s.try_send("foo") {
175    ///     assert_eq!(err.into_inner(), "foo");
176    /// }
177    /// ```
178    pub fn into_inner(self) -> T {
179        match self {
180            Self::Full(v) => v,
181            Self::Disconnected(v) => v,
182        }
183    }
184
185    /// Returns `true` if the send operation failed because the channel is full.
186    pub fn is_full(&self) -> bool {
187        matches!(self, Self::Full(_))
188    }
189
190    /// Returns `true` if the send operation failed because the channel is disconnected.
191    pub fn is_disconnected(&self) -> bool {
192        matches!(self, Self::Disconnected(_))
193    }
194}
195
196impl<T> fmt::Debug for SendTimeoutError<T> {
197    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198        "SendTimeoutError(..)".fmt(f)
199    }
200}
201
202impl<T> fmt::Display for SendTimeoutError<T> {
203    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204        match *self {
205            Self::Timeout(..) => "timed out waiting on send operation".fmt(f),
206            Self::Disconnected(..) => "sending on a disconnected channel".fmt(f),
207        }
208    }
209}
210
211impl<T: Send> error::Error for SendTimeoutError<T> {}
212
213impl<T> From<SendError<T>> for SendTimeoutError<T> {
214    fn from(err: SendError<T>) -> Self {
215        match err {
216            SendError(e) => Self::Disconnected(e),
217        }
218    }
219}
220
221impl<T> SendTimeoutError<T> {
222    /// Unwraps the message.
223    ///
224    /// # Examples
225    ///
226    /// ```
227    /// use std::time::Duration;
228    /// use crossfire::mpmc;
229    ///
230    /// let (s, r) = mpmc::bounded_blocking::<&str>(10);
231    /// drop(r);
232    ///
233    /// if let Err(err) = s.send_timeout("foo", Duration::from_secs(1)) {
234    ///     assert_eq!(err.into_inner(), "foo");
235    /// }
236    /// ```
237    pub fn into_inner(self) -> T {
238        match self {
239            Self::Timeout(v) => v,
240            Self::Disconnected(v) => v,
241        }
242    }
243
244    /// Returns `true` if the send operation timed out.
245    pub fn is_timeout(&self) -> bool {
246        matches!(self, Self::Timeout(_))
247    }
248
249    /// Returns `true` if the send operation failed because the channel is disconnected.
250    pub fn is_disconnected(&self) -> bool {
251        matches!(self, Self::Disconnected(_))
252    }
253}
254
255impl fmt::Display for RecvError {
256    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257        "receiving on an empty and disconnected channel".fmt(f)
258    }
259}
260
261impl error::Error for RecvError {}
262
263impl fmt::Display for TryRecvError {
264    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265        match *self {
266            Self::Empty => "receiving on an empty channel".fmt(f),
267            Self::Disconnected => "receiving on an empty and disconnected channel".fmt(f),
268        }
269    }
270}
271
272impl error::Error for TryRecvError {}
273
274impl From<RecvError> for TryRecvError {
275    fn from(err: RecvError) -> Self {
276        match err {
277            RecvError => Self::Disconnected,
278        }
279    }
280}
281
282impl TryRecvError {
283    /// Returns `true` if the receive operation failed because the channel is empty.
284    pub fn is_empty(&self) -> bool {
285        matches!(self, Self::Empty)
286    }
287
288    /// Returns `true` if the receive operation failed because the channel is disconnected.
289    pub fn is_disconnected(&self) -> bool {
290        matches!(self, Self::Disconnected)
291    }
292}
293
294impl fmt::Display for RecvTimeoutError {
295    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296        match *self {
297            Self::Timeout => "timed out waiting on receive operation".fmt(f),
298            Self::Disconnected => "channel is empty and disconnected".fmt(f),
299        }
300    }
301}
302
303impl error::Error for RecvTimeoutError {}
304
305impl From<RecvError> for RecvTimeoutError {
306    fn from(err: RecvError) -> Self {
307        match err {
308            RecvError => Self::Disconnected,
309        }
310    }
311}
312
313impl RecvTimeoutError {
314    /// Returns `true` if the receive operation timed out.
315    pub fn is_timeout(&self) -> bool {
316        matches!(self, Self::Timeout)
317    }
318
319    /// Returns `true` if the receive operation failed because the channel is disconnected.
320    pub fn is_disconnected(&self) -> bool {
321        matches!(self, Self::Disconnected)
322    }
323}