pub trait Task: Send {
// Required methods
fn name(&self) -> &str;
fn run(
self: Box<Self>,
shutdown_rx: Receiver<()>,
) -> Pin<Box<dyn Future<Output = TaskResult> + Send>>;
// Provided methods
fn dependencies(&self) -> Vec<String> { ... }
fn ready_check(
&self,
) -> Pin<Box<dyn Future<Output = TaskResult> + Send + '_>> { ... }
fn priority(&self) -> i32 { ... }
fn is_critical(&self) -> bool { ... }
fn initial_state(&self) -> TaskState { ... }
}Expand description
任务抽象 (核心扩展点)
所有需要在运行时中管理的任务都必须实现此 trait 支持依赖声明、生命周期钩子、就绪检查
§实现说明
- 使用 Rust 2024 原生 async fn in traits(不使用 async-trait 宏)
- 所有方法提供默认实现,降低实现难度
- 支持依赖管理、优先级、关键任务标记
§示例
use flare_core_runtime::task::{Task, TaskResult};
use std::pin::Pin;
use std::future::Future;
struct MyTask {
name: String,
}
impl Task for MyTask {
fn name(&self) -> &str {
&self.name
}
fn run(
self: Box<Self>,
shutdown_rx: tokio::sync::oneshot::Receiver<()>,
) -> Pin<Box<dyn Future<Output = TaskResult> + Send>> {
Box::pin(async move {
// 任务逻辑
Ok(())
})
}
}Required Methods§
Provided Methods§
Sourcefn dependencies(&self) -> Vec<String>
fn dependencies(&self) -> Vec<String>
Sourcefn ready_check(&self) -> Pin<Box<dyn Future<Output = TaskResult> + Send + '_>>
fn ready_check(&self) -> Pin<Box<dyn Future<Output = TaskResult> + Send + '_>>
Sourcefn is_critical(&self) -> bool
fn is_critical(&self) -> bool
Sourcefn initial_state(&self) -> TaskState
fn initial_state(&self) -> TaskState
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".