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