1use std::error;
2use std::fmt;
3use std::io;
4
5pub enum SendError<T> {
6 Io(io::Error),
7 Disconnected(T),
8}
9
10pub enum TrySendError<T> {
11 Io(io::Error),
12 Full(T),
13 Disconnected(T),
14}
15
16#[derive(Debug)]
17pub enum RecvError {
18 Io(io::Error),
19 Disconnected,
20}
21
22#[derive(Debug)]
23pub enum TryRecvError {
24 Io(io::Error),
25 Empty,
26 Disconnected,
27}
28
29impl<T> From<io::Error> for SendError<T> {
30 fn from(e: io::Error) -> Self {
31 SendError::Io(e)
32 }
33}
34
35impl<T> fmt::Debug for SendError<T> {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 match *self {
38 SendError::Io(ref e) => write!(f, "Io({:?})", e),
39 SendError::Disconnected(..) => write!(f, "Disconnected(..)"),
40 }
41 }
42}
43
44impl<T> fmt::Display for SendError<T> {
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 match *self {
47 SendError::Io(..) => write!(f, "io error on sending"),
48 SendError::Disconnected(..) => write!(f, "sending on a closed channel"),
49 }
50 }
51}
52
53impl<T: Send> error::Error for SendError<T> {
54 fn description(&self) -> &str {
55 match *self {
56 SendError::Io(..) => "io error on sending",
57 SendError::Disconnected(..) => "sending on a closed channel",
58 }
59 }
60}
61
62impl<T> From<io::Error> for TrySendError<T> {
63 fn from(e: io::Error) -> Self {
64 TrySendError::Io(e)
65 }
66}
67
68impl<T> fmt::Debug for TrySendError<T> {
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70 match *self {
71 TrySendError::Io(ref e) => write!(f, "Io({:?}", e),
72 TrySendError::Full(..) => write!(f, "Full(..)"),
73 TrySendError::Disconnected(..) => write!(f, "Disconnected(..)"),
74 }
75 }
76}
77
78impl<T> fmt::Display for TrySendError<T> {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match *self {
81 TrySendError::Io(..) => write!(f, "io error on sending"),
82 TrySendError::Full(..) => write!(f, "sending on a full channel"),
83 TrySendError::Disconnected(..) => write!(f, "sending on a closed channel"),
84 }
85 }
86}
87
88impl<T: Send> error::Error for TrySendError<T> {
89 fn description(&self) -> &str {
90 match *self {
91 TrySendError::Io(..) => "io error on sending",
92 TrySendError::Full(..) => "sending on a full channel",
93 TrySendError::Disconnected(..) => "sending on a closed channel",
94 }
95 }
96}
97
98impl From<io::Error> for RecvError {
99 fn from(e: io::Error) -> Self {
100 RecvError::Io(e)
101 }
102}
103
104impl fmt::Display for RecvError {
105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 write!(f, "receiving on a closed channel")
107 }
108}
109
110impl error::Error for RecvError {
111 fn description(&self) -> &str {
112 "receiving on a closed channel"
113 }
114}
115
116impl From<io::Error> for TryRecvError {
117 fn from(e: io::Error) -> Self {
118 TryRecvError::Io(e)
119 }
120}
121
122impl fmt::Display for TryRecvError {
123 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124 match *self {
125 TryRecvError::Io(..) => write!(f, "io error on receiving"),
126 TryRecvError::Empty => write!(f, "receiving on an empty channel"),
127 TryRecvError::Disconnected => write!(f, "receiving on a closed channel"),
128 }
129 }
130}
131
132impl error::Error for TryRecvError {
133 fn description(&self) -> &str {
134 match *self {
135 TryRecvError::Io(..) => "io error on receiving",
136 TryRecvError::Empty => "receiving on an empty channel",
137 TryRecvError::Disconnected => "receiving on a closed channel",
138 }
139 }
140}