kanal/
error.rs

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