Skip to main content

framework_timer/
types.rs

1use anyhow::Error;
2use std::future::Future;
3use std::pin::Pin;
4
5/// 定时任务执行后的调度结果。
6pub struct TimerResult {
7    /// 任务执行业务错误;存在时会记录警告日志。
8    pub error: Option<Error>,
9    /// 下次执行的毫秒时间戳;未指定时使用宏声明的间隔。
10    pub next: Option<i64>,
11}
12
13impl TimerResult {
14    /// 表示任务成功完成并使用默认执行间隔。
15    pub const fn success() -> Self {
16        Self {
17            error: None,
18            next: None,
19        }
20    }
21
22    /// 表示任务完成并在指定毫秒时间戳再次执行。
23    pub const fn with_next(next: i64) -> Self {
24        Self {
25            error: None,
26            next: Some(next),
27        }
28    }
29
30    /// 表示任务失败并使用默认执行间隔。
31    pub fn failure(error: Error) -> Self {
32        Self {
33            error: Some(error),
34            next: None,
35        }
36    }
37}
38
39/// 已装箱的定时任务 Future。
40pub type TimerFuture = Pin<Box<dyn Future<Output = TimerResult> + Send>>;
41
42/// 可被定时器调度的无参异步任务。
43pub type TimerTarget = fn() -> TimerFuture;
44
45/// 为任务建立外部异步上下文的执行包装器。
46pub type TimerWrapper = std::sync::Arc<dyn Fn(TimerTarget) -> TimerFuture + Send + Sync>;