Skip to main content

kanal_plus/
error.rs

1#![forbid(unsafe_code)]
2use core::{fmt, fmt::Debug};
3
4/// Error type for channel send operations without timeout
5#[derive(PartialEq, Eq, Clone, Copy)]
6pub struct SendError<T>(pub T);
7
8impl<T> Debug for SendError<T> {
9    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10        write!(f, "SendError(..)")
11    }
12}
13impl<T> fmt::Display for SendError<T> {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        fmt::Display::fmt("send to a closed channel", f)
16    }
17}
18
19impl<T> SendError<T> {
20    /// Consumes the error and returns the contained value.
21    #[inline]
22    pub fn into_inner(self) -> T {
23        self.0
24    }
25}
26
27impl<T> std::error::Error for SendError<T> {}
28
29/// Error type for channel send operations with timeout
30#[derive(PartialEq, Eq, Clone, Copy)]
31pub enum SendTimeoutError<T> {
32    /// Indicates that the channel is closed on both sides with a call to
33    /// `close()`
34    Closed(T),
35    /// Indicates that channel operation reached timeout and is canceled
36    Timeout(T),
37}
38
39impl<T> Debug for SendTimeoutError<T> {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        match self {
42            SendTimeoutError::Closed(..) => write!(f, "Closed(..)"),
43            SendTimeoutError::Timeout(..) => write!(f, "Timeout(..)"),
44        }
45    }
46}
47impl<T> fmt::Display for SendTimeoutError<T> {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        fmt::Display::fmt(
50            match *self {
51                SendTimeoutError::Closed(..) => "send to a closed channel",
52                SendTimeoutError::Timeout(..) => "send timeout",
53            },
54            f,
55        )
56    }
57}
58
59impl<T> SendTimeoutError<T> {
60    /// Consumes the error and returns the contained value.
61    #[inline]
62    pub fn into_inner(self) -> T {
63        match self {
64            SendTimeoutError::Closed(value) => value,
65            SendTimeoutError::Timeout(value) => value,
66        }
67    }
68}
69
70impl<T> std::error::Error for SendTimeoutError<T> {}
71
72/// Error type for channel receive operations without timeout
73#[derive(Debug, PartialEq, Eq)]
74pub struct ReceiveError();
75impl core::error::Error for ReceiveError {}
76impl fmt::Display for ReceiveError {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        fmt::Display::fmt("receive from a closed channel", f)
79    }
80}
81
82/// Error type for channel receive operations with timeout
83#[derive(Debug, PartialEq, Eq)]
84pub enum ReceiveErrorTimeout {
85    /// Indicates that the channel is closed on both sides with a call to
86    /// `close()`
87    Closed,
88    /// Indicates that channel operation reached timeout and is canceled
89    Timeout,
90}
91impl core::error::Error for ReceiveErrorTimeout {}
92impl fmt::Display for ReceiveErrorTimeout {
93    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94        fmt::Display::fmt(
95            match *self {
96                ReceiveErrorTimeout::Closed => "receive from a closed channel",
97                ReceiveErrorTimeout::Timeout => "receive timeout",
98            },
99            f,
100        )
101    }
102}
103
104/// Error type for closing a channel when channel is already closed
105#[derive(Debug, PartialEq, Eq)]
106pub struct CloseError();
107impl std::error::Error for CloseError {}
108impl fmt::Display for CloseError {
109    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110        fmt::Display::fmt("channel is already closed", f)
111    }
112}