Skip to main content

moirai_core/channel/
error.rs

1//! Error types, cache-alignment helper, unified `Channel` trait, and `Result` alias.
2
3use std::fmt;
4
5pub(super) use moirai_utils::cache::CacheAligned;
6
7use super::stats::ChannelStatistics;
8
9/// Error types for channel operations
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum ChannelError {
12    /// Channel is full and cannot accept more messages
13    Full,
14    /// Channel is empty and has no messages
15    Empty,
16    /// Channel has been closed
17    Closed,
18    /// Operation would block but non-blocking was requested
19    WouldBlock,
20    /// Invalid channel configuration
21    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
38/// Result type for channel operations
39pub type Result<T> = std::result::Result<T, ChannelError>;
40
41/// Trait for unified channel behavior following Interface Segregation Principle
42pub trait Channel<T>: Send + Sync {
43    /// Send a value, blocking if necessary
44    fn send(&self, value: T) -> Result<()>;
45
46    /// Try to send without blocking
47    fn try_send(&self, value: T) -> Result<()>;
48
49    /// Receive a value, blocking if necessary
50    fn recv(&self) -> Result<T>;
51
52    /// Try to receive without blocking
53    fn try_recv(&self) -> Result<T>;
54
55    /// Check if channel is empty
56    fn is_empty(&self) -> bool;
57
58    /// Check if channel is full
59    fn is_full(&self) -> bool;
60
61    /// Get the capacity of the channel
62    fn capacity(&self) -> Option<usize>;
63
64    /// Send multiple values in batch. Default sends each individually.
65    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    /// Receive up to `max_count` values in batch. Default receives individually.
75    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    /// Close the channel. Default: no-op (channels that support close override).
87    fn close(&self) {}
88
89    /// Check if channel is closed. Default: false.
90    fn is_closed(&self) -> bool {
91        false
92    }
93
94    /// Current number of buffered items. Default: 0.
95    fn len(&self) -> usize {
96        0
97    }
98
99    /// Return statistics if the channel tracks them.
100    fn stats(&self) -> Option<ChannelStatistics> {
101        None
102    }
103}