Skip to main content

channel_sender/
error.rs

1//
2#[derive(Debug, Eq)]
3pub enum SendError<T> {
4    Full(T),
5    Closed(T),
6    Disconnected(T),
7}
8impl<T: core::fmt::Debug> core::fmt::Display for SendError<T> {
9    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
10        write!(f, "{self:?}")
11    }
12}
13impl<T: core::fmt::Debug> std::error::Error for SendError<T> {}
14impl<T: core::cmp::PartialEq> core::cmp::PartialEq for SendError<T> {
15    fn eq(&self, other: &Self) -> bool {
16        match (self, other) {
17            (Self::Full(v1), Self::Full(v2)) => v1 == v2,
18            (Self::Closed(v1), Self::Closed(v2)) | (Self::Closed(v1), Self::Disconnected(v2)) => {
19                v1 == v2
20            }
21            (Self::Disconnected(v1), Self::Disconnected(v2))
22            | (Self::Disconnected(v1), Self::Closed(v2)) => v1 == v2,
23            _ => false,
24        }
25    }
26}
27
28impl<T> SendError<T> {
29    pub fn is_full(&self) -> bool {
30        matches!(self, Self::Full(_))
31    }
32
33    pub fn is_closed_or_disconnected(&self) -> bool {
34        matches!(self, Self::Closed(_) | Self::Disconnected(_))
35    }
36
37    pub fn inner(&self) -> &T {
38        match &self {
39            Self::Full(v) => v,
40            Self::Closed(v) => v,
41            Self::Disconnected(v) => v,
42        }
43    }
44    pub fn into_inner(self) -> T {
45        match self {
46            Self::Full(v) => v,
47            Self::Closed(v) => v,
48            Self::Disconnected(v) => v,
49        }
50    }
51}
52
53//
54#[derive(Debug, Eq)]
55pub enum SendErrorWithoutFull<T> {
56    Closed(T),
57    Disconnected(T),
58    UnreachableFull(T),
59}
60impl<T: core::fmt::Debug> core::fmt::Display for SendErrorWithoutFull<T> {
61    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
62        write!(f, "{self:?}")
63    }
64}
65impl<T: core::fmt::Debug> std::error::Error for SendErrorWithoutFull<T> {}
66impl<T: core::cmp::PartialEq> core::cmp::PartialEq for SendErrorWithoutFull<T> {
67    fn eq(&self, other: &Self) -> bool {
68        match (self, other) {
69            (Self::Closed(v1), Self::Closed(v2)) | (Self::Closed(v1), Self::Disconnected(v2)) => {
70                v1 == v2
71            }
72            (Self::Disconnected(v1), Self::Disconnected(v2))
73            | (Self::Disconnected(v1), Self::Closed(v2)) => v1 == v2,
74            (Self::UnreachableFull(v1), Self::UnreachableFull(v2)) => v1 == v2,
75            _ => false,
76        }
77    }
78}
79
80impl<T> SendErrorWithoutFull<T> {
81    pub fn is_closed_or_disconnected(&self) -> bool {
82        matches!(self, Self::Closed(_) | Self::Disconnected(_))
83    }
84
85    pub fn is_unreachable_full(&self) -> bool {
86        matches!(self, Self::UnreachableFull(_))
87    }
88
89    pub fn inner(&self) -> &T {
90        match &self {
91            Self::Closed(v) => v,
92            Self::Disconnected(v) => v,
93            Self::UnreachableFull(v) => v,
94        }
95    }
96    pub fn into_inner(self) -> T {
97        match self {
98            Self::Closed(v) => v,
99            Self::Disconnected(v) => v,
100            Self::UnreachableFull(v) => v,
101        }
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn test_send_error_partial_eq() {
111        assert_eq!(SendError::Full(1), SendError::Full(1));
112        assert_eq!(SendError::Closed(1), SendError::Closed(1));
113        assert_eq!(SendError::Closed(1), SendError::Disconnected(1));
114        assert_eq!(SendError::Disconnected(1), SendError::Disconnected(1));
115        assert_ne!(SendError::Full(1), SendError::Closed(1));
116        assert_ne!(SendError::Full(1), SendError::Disconnected(1));
117    }
118
119    #[test]
120    fn test_send_error_without_full_partial_eq() {
121        assert_eq!(
122            SendErrorWithoutFull::UnreachableFull(1),
123            SendErrorWithoutFull::UnreachableFull(1)
124        );
125        assert_eq!(
126            SendErrorWithoutFull::Closed(1),
127            SendErrorWithoutFull::Closed(1)
128        );
129        assert_eq!(
130            SendErrorWithoutFull::Closed(1),
131            SendErrorWithoutFull::Disconnected(1)
132        );
133        assert_eq!(
134            SendErrorWithoutFull::Disconnected(1),
135            SendErrorWithoutFull::Disconnected(1)
136        );
137        assert_ne!(
138            SendErrorWithoutFull::UnreachableFull(1),
139            SendErrorWithoutFull::Closed(1)
140        );
141        assert_ne!(
142            SendErrorWithoutFull::UnreachableFull(1),
143            SendErrorWithoutFull::Disconnected(1)
144        );
145    }
146}