Skip to main content

kestrel_timer/
error.rs

1use std::fmt;
2
3/// Timer Error Type
4///
5/// 定时器错误类型
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum TimerError {
8    /// Invalid slot count (must be a power of 2 and greater than 0)
9    ///
10    /// 无效的槽位数 (必须是 2 的幂且大于 0)
11    InvalidSlotCount {
12        slot_count: usize,
13        reason: &'static str,
14    },
15
16    /// Configuration validation failed
17    ///
18    /// 配置验证失败
19    InvalidConfiguration { field: String, reason: String },
20
21    /// Registration failed, internal channel is full or closed
22    ///
23    /// 注册失败,内部通道已满或已关闭
24    RegisterFailed,
25
26    /// The timing wheel has been shut down and cannot accept new tasks
27    ///
28    /// 时间轮已关闭,不能再接受新任务
29    Shutdown,
30
31    /// A task handle or task ID belongs to another timing wheel
32    ///
33    /// 任务句柄或任务 ID 属于另一个时间轮
34    WrongWheel,
35
36    /// Batch operation failed: handles and tasks length mismatch
37    ///
38    /// 批量操作失败:handles 和 tasks 长度不匹配
39    BatchLengthMismatch {
40        handles_len: usize,
41        tasks_len: usize,
42    },
43}
44
45impl fmt::Display for TimerError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            TimerError::InvalidSlotCount { slot_count, reason } => {
49                write!(
50                    f,
51                    "Invalid slot count {}: {} (无效的槽位数 {}: {})",
52                    slot_count, reason, slot_count, reason
53                )
54            }
55            TimerError::InvalidConfiguration { field, reason } => {
56                write!(
57                    f,
58                    "Configuration validation failed ({}): {} (配置验证失败 ({}): {})",
59                    field, reason, field, reason
60                )
61            }
62            TimerError::RegisterFailed => {
63                write!(
64                    f,
65                    "Registration failed: internal channel is full or closed (注册失败: 内部通道已满或已关闭)"
66                )
67            }
68            TimerError::Shutdown => {
69                write!(f, "Timer wheel has been shut down (时间轮已关闭)")
70            }
71            TimerError::WrongWheel => {
72                write!(
73                    f,
74                    "Task belongs to a different timing wheel (任务属于另一个时间轮)"
75                )
76            }
77            TimerError::BatchLengthMismatch {
78                handles_len,
79                tasks_len,
80            } => {
81                write!(
82                    f,
83                    "Batch operation failed: handles length ({}) does not match tasks length ({}) (批量操作失败: handles 长度 ({}) 与 tasks 长度 ({}) 不匹配)",
84                    handles_len, tasks_len, handles_len, tasks_len
85                )
86            }
87        }
88    }
89}
90
91impl std::error::Error for TimerError {}