Skip to main content

moirai_core/channel/
error.rs

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