pub trait Plugin: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn start<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn stop<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided method
fn depends_on(&self) -> &[&str] { ... }
}Expand description
插件生命周期:start/stop 对称
极简设计——只定义 start / stop 两个生命周期方法。 Rust 版增强:异步化 + 依赖声明 + 拓扑排序启动。
§示例
ⓘ
use alun_core::plugin::Plugin;
struct DbPlugin { pool: PgPool }
#[async_trait]
impl Plugin for DbPlugin {
fn name(&self) -> &str { "pg" }
async fn start(&self) -> Result<()> {
sqlx::query("SELECT 1").fetch_one(&self.pool).await?;
Ok(())
}
async fn stop(&self) -> Result<()> {
self.pool.close().await;
Ok(())
}
}Required Methods§
Provided Methods§
Sourcefn depends_on(&self) -> &[&str]
fn depends_on(&self) -> &[&str]
依赖的其他插件名称(用于拓扑排序,保证启动顺序)
默认返回空数组(无依赖)。