moirai_core/channel/
error.rs1use std::fmt;
4
5pub(super) use moirai_utils::cache::CacheAligned;
6
7use super::stats::ChannelStatistics;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum ChannelError {
12 Full,
14 Empty,
16 Closed,
18 WouldBlock,
20 InvalidConfig,
22}
23
24impl fmt::Display for ChannelError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 Self::Full => write!(f, "channel is full"),
28 Self::Empty => write!(f, "channel is empty"),
29 Self::Closed => write!(f, "channel is closed"),
30 Self::WouldBlock => write!(f, "operation would block"),
31 Self::InvalidConfig => write!(f, "invalid channel configuration"),
32 }
33 }
34}
35
36impl std::error::Error for ChannelError {}
37
38pub type Result<T> = std::result::Result<T, ChannelError>;
40
41pub trait Channel<T>: Send + Sync {
43 fn send(&self, value: T) -> Result<()>;
45
46 fn try_send(&self, value: T) -> Result<()>;
48
49 fn recv(&self) -> Result<T>;
51
52 fn try_recv(&self) -> Result<T>;
54
55 fn is_empty(&self) -> bool;
57
58 fn is_full(&self) -> bool;
60
61 fn capacity(&self) -> Option<usize>;
63
64 fn send_batch(&self, values: Vec<T>) -> Result<usize> {
66 let mut count = 0;
67 for value in values {
68 self.send(value)?;
69 count += 1;
70 }
71 Ok(count)
72 }
73
74 fn recv_batch(&self, max_count: usize) -> Vec<T> {
76 let mut results = Vec::with_capacity(max_count);
77 for _ in 0..max_count {
78 match self.recv() {
79 Ok(value) => results.push(value),
80 Err(_) => break,
81 }
82 }
83 results
84 }
85
86 fn close(&self) {}
88
89 fn is_closed(&self) -> bool {
91 false
92 }
93
94 fn len(&self) -> usize {
96 0
97 }
98
99 fn stats(&self) -> Option<ChannelStatistics> {
101 None
102 }
103}