1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use derivative::Derivative;
use std::{error, fmt};
#[cfg(test)]
use proptest_derive::Arbitrary;
#[derive(Derivative)]
#[derivative(Debug)]
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum SendError<T> {
Disconnected(#[derivative(Debug = "ignore")] T),
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"sending on a disconnected channel".fmt(f)
}
}
impl<T: Send> error::Error for SendError<T> {}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum RecvError {
Disconnected,
}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use RecvError::*;
match self {
Disconnected => "receiving on an empty and disconnected channel".fmt(f),
}
}
}
impl error::Error for RecvError {}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub enum TryRecvError {
Empty,
Disconnected,
}
impl fmt::Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use TryRecvError::*;
match self {
Empty => "receiving on an empty channel".fmt(f),
Disconnected => "receiving on an empty and disconnected channel".fmt(f),
}
}
}
impl error::Error for TryRecvError {}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn send_error_implements_error_trait(err: SendError<()>) {
assert_eq!(
format!("{}", err),
format!("{}", Box::<dyn error::Error>::from(err))
);
}
#[test]
fn recv_error_implements_error_trait(err: RecvError) {
assert_eq!(
format!("{}", err),
format!("{}", Box::<dyn error::Error>::from(err))
);
}
#[test]
fn try_recv_error_implements_error_trait(err: TryRecvError) {
assert_eq!(
format!("{}", err),
format!("{}", Box::<dyn error::Error>::from(err))
);
}
}
}