pub trait Plugin: Send + Sync {
// Required method
fn name(&self) -> &str;
// Provided methods
fn on_startup<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn on_task_start<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn on_task_stop<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn on_shutdown<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn on_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
error: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
}Expand description
插件抽象
支持在关键生命周期事件注入自定义逻辑
§实现说明
- 使用 async-trait 宏支持 trait objects
- 所有插件必须实现此 trait
- 插件可以在运行时启动、任务启动/停止、停机等事件时执行
§示例
ⓘ
use flare_core_runtime::plugin::{Plugin, PluginContext};
use flare_core_runtime::error::PluginError;
struct LoggingPlugin;
impl Plugin for LoggingPlugin {
fn name(&self) -> &str {
"logging"
}
async fn on_startup(&self, ctx: &PluginContext) -> Result<(), PluginError> {
tracing::info!("Runtime starting: {}", ctx.runtime_name);
Ok(())
}
async fn on_shutdown(&self, ctx: &PluginContext) -> Result<(), PluginError> {
tracing::info!("Runtime shutting down: {}", ctx.runtime_name);
Ok(())
}
}Required Methods§
Provided Methods§
Sourcefn on_startup<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_startup<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sourcefn on_task_start<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_task_start<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sourcefn on_task_stop<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_task_stop<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sourcefn on_shutdown<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_shutdown<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 PluginContext,
) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".