1pub mod async_bridge;
12pub mod broadcast;
13pub mod mpsc;
14pub mod spsc;
15
16pub use mpsc::{MpscChannel, MpscReceiver, MpscSender};
17pub use spsc::{SpscChannel, SpscReceiver, SpscSender};
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21pub enum ChannelError<T> {
22 Full(T),
24 Disconnected(T),
26 Empty,
28 Timeout,
30}
31
32impl<T> std::fmt::Display for ChannelError<T> {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 match self {
35 Self::Full(_) => write!(f, "channel full"),
36 Self::Disconnected(_) => write!(f, "channel disconnected"),
37 Self::Empty => write!(f, "channel empty"),
38 Self::Timeout => write!(f, "operation timed out"),
39 }
40 }
41}
42
43impl<T: std::fmt::Debug> std::error::Error for ChannelError<T> {}
44
45pub trait ChannelSender<T>: Clone + Send + Sync {
47 fn try_send(&self, item: T) -> Result<(), ChannelError<T>>;
52
53 fn send_timeout(&self, item: T, timeout: std::time::Duration) -> Result<(), ChannelError<T>>;
58}
59
60pub trait ChannelReceiver<T>: Send {
62 fn try_recv(&self) -> Option<T>;
64
65 fn recv_timeout(&self, timeout: std::time::Duration) -> Option<T>;
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_channel_error_display_full() {
75 let err: ChannelError<u32> = ChannelError::Full(42);
76 assert_eq!(err.to_string(), "channel full");
77 }
78
79 #[test]
80 fn test_channel_error_display_disconnected() {
81 let err: ChannelError<u32> = ChannelError::Disconnected(42);
82 assert_eq!(err.to_string(), "channel disconnected");
83 }
84
85 #[test]
86 fn test_channel_error_display_empty() {
87 let err: ChannelError<u32> = ChannelError::Empty;
88 assert_eq!(err.to_string(), "channel empty");
89 }
90
91 #[test]
92 fn test_channel_error_display_timeout() {
93 let err: ChannelError<u32> = ChannelError::Timeout;
94 assert_eq!(err.to_string(), "operation timed out");
95 }
96
97 #[test]
98 fn test_channel_error_equality() {
99 let err1: ChannelError<u32> = ChannelError::Empty;
100 let err2: ChannelError<u32> = ChannelError::Empty;
101 assert_eq!(err1, err2);
102
103 let err3: ChannelError<u32> = ChannelError::Timeout;
104 assert_ne!(err1, err3);
105 }
106
107 #[test]
108 fn test_channel_error_clone() {
109 let err: ChannelError<u32> = ChannelError::Full(42);
110 let cloned = err.clone();
111 assert_eq!(err, cloned);
112 }
113
114 #[test]
115 fn test_channel_error_debug() {
116 let err: ChannelError<u32> = ChannelError::Full(42);
117 let debug_str = format!("{:?}", err);
118 assert!(debug_str.contains("Full"));
119 assert!(debug_str.contains("42"));
120 }
121}