simple_channels/mpsc/
error.rs

1use core::fmt;
2use std::error::Error;
3
4/// An error returned from the `send` method.
5///
6/// The error contains the value that could not be sent. This occurs when the
7/// channel is disconnected, meaning all receivers have been dropped.
8#[derive(PartialEq, Eq, Clone, Copy)]
9pub struct SendError<T>(pub T);
10
11impl<T> fmt::Debug for SendError<T> {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        f.debug_tuple("SendError").finish()
14    }
15}
16
17impl<T> fmt::Display for SendError<T> {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        "sending on a disconnected channel".fmt(f)
20    }
21}
22
23impl<T: Send> Error for SendError<T> {}
24
25/// An error returned from the `recv` and `try_recv` methods.
26///
27/// This error indicates that the channel is empty and disconnected, meaning
28/// all senders have been dropped and no more messages will be sent.
29#[derive(PartialEq, Eq, Clone, Copy, Debug)]
30pub enum RecvError {
31    /// The channel is empty and disconnected.
32    Disconnected,
33}
34
35impl fmt::Display for RecvError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        "receiving on an empty and disconnected channel".fmt(f)
38    }
39}
40
41impl Error for RecvError {}
42
43/// An error returned from the `try_recv` method.
44#[derive(PartialEq, Eq, Clone, Copy, Debug)]
45pub enum TryRecvError {
46    /// The channel is currently empty, but is still connected.
47    Empty,
48    /// The channel is empty and disconnected.
49    Disconnected,
50}
51
52impl fmt::Display for TryRecvError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match *self {
55            TryRecvError::Empty => "receiving on an empty channel".fmt(f),
56            TryRecvError::Disconnected => "receiving on an empty and disconnected channel".fmt(f),
57        }
58    }
59}
60
61impl Error for TryRecvError {}