Expand description
§libplugin — 轻量插件框架 / Minimal plugin framework
静态注册 + 运行时动态加载,一套生命周期,两种来源: Static registration + runtime loading, one lifecycle, two sources:
- 静态插件:编译时注册,零开销 / Static plugins: registered at compile time, zero overhead
- 动态插件:运行时通过
libloading加载 .so/.dll/.dylib / Dynamic plugins: .so/.dll/.dylib loaded at runtime vialibloading - 插件发现:扫描插件目录下的 manifest.toml / Discovery: scan plugin dirs for manifest.toml
- 生命周期:on_load → on_message/on_tick → on_unload/on_shutdown / Lifecycle: on_load → on_message/on_tick → on_unload/on_shutdown
- per-plugin 配置:manifest.toml 中的
[plugin.config]序列化为 JSON 传给 on_load / Per-plugin config:[plugin.config]in the manifest reaches on_load as JSON
§快速开始 / Quick start
ⓘ
use libplugin::{Plugin, PluginManager};
let mut mgr = PluginManager::new("/path/to/plugins");
// 注册静态插件 / Register a static plugin
mgr.register(Box::new(MyPlugin));
// 发现并加载动态插件 / Discover and load dynamic plugins
mgr.discover_and_load()?;
// 调用生命周期 / Drive the lifecycle
mgr.load_all()?; // 初始化所有已注册插件 / initialize all registered plugins
mgr.on_message("hello"); // 广播消息给所有插件 / broadcast to all plugins
mgr.on_tick()?; // 周期 tick / periodic tick
mgr.unload_all()?; // 卸载所有插件 / unload everything§动态插件 ABI
动态插件必须导出 C 函数 libplugin_entry() -> PluginVTable。
详见 vtable::PluginVTable。
Dynamic plugins must export the C function
libplugin_entry() -> PluginVTable. See vtable::PluginVTable.
Re-exports§
pub use error::Result;
Modules§
- dynamic
- 动态库插件 — 通过 libloading 加载 .so/.dll/.dylib Dynamic library plugin — loads .so/.dll/.dylib via libloading.
- error
- 插件系统错误类型 Plugin system error types.
- manifest
- 插件清单 — 描述动态插件的元信息与配置 Plugin manifest — metadata and configuration for dynamic plugins.
- vtable
- C 兼容插件 VTable — 动态库插件的 ABI 契约 C-compatible plugin VTable — ABI contract for dynamic library plugins.
Structs§
- Plugin
Manager - 插件管理器 — 负责注册、发现、加载、卸载插件 Plugin manager — registration, discovery, loading, unloading.
Enums§
- Plugin
Source - 插件来源 / Plugin origin.
- Plugin
State - 插件运行状态 / Plugin runtime state.
Traits§
- Plugin
- 插件特质 — 所有插件(静态或动态)需实现此 trait Plugin trait — all plugins (static or dynamic) must implement this.