pub struct PluginManager { /* private fields */ }Expand description
插件管理器 - 初始化后不可变
§设计理念
- 初始化阶段: 使用
PluginManagerBuilder注册和配置插件 - 运行时阶段: 插件列表完全只读,零锁开销
- 性能优化: 预计算排序结果,避免运行时重复计算
§性能对比
旧实现 (使用 RwLock):
get_sorted_plugins(): 需要获取 2 个读锁 → ~500ns- 高并发场景存在锁竞争
新实现 (无锁):
get_sorted_plugins(): 直接返回 Arc → ~50ns (10x 提升)- 零锁竞争,完美并发扩展
§示例
use mf_state::plugin::{PluginManagerBuilder, Plugin};
// 初始化阶段
let mut builder = PluginManagerBuilder::new();
builder.register_plugin(plugin1)?;
builder.register_plugin(plugin2)?;
let manager = builder.build()?;
// 运行时阶段(零开销)
let plugins = manager.get_sorted_plugins_sync();Implementations§
Source§impl PluginManager
impl PluginManager
Sourcepub async fn get_sorted_plugins(&self) -> Vec<Arc<Plugin>>
pub async fn get_sorted_plugins(&self) -> Vec<Arc<Plugin>>
Sourcepub fn get_sorted_plugins_sync(&self) -> &[Arc<Plugin>]
pub fn get_sorted_plugins_sync(&self) -> &[Arc<Plugin>]
获取排序后的插件列表(同步接口,推荐使用)
返回切片引用,避免不必要的克隆。
§示例
for plugin in manager.get_sorted_plugins_sync() {
plugin.apply(...).await?;
}Sourcepub async fn is_initialized(&self) -> bool
pub async fn is_initialized(&self) -> bool
检查初始化状态(异步接口,兼容现有代码)
使用原子操作,无锁开销 (~5ns)。
Sourcepub fn is_initialized_sync(&self) -> bool
pub fn is_initialized_sync(&self) -> bool
检查初始化状态(同步接口,推荐使用)
Sourcepub fn plugin_count(&self) -> usize
pub fn plugin_count(&self) -> usize
获取插件总数
Sourcepub fn has_plugin(&self, name: &str) -> bool
pub fn has_plugin(&self, name: &str) -> bool
检查插件是否存在
Trait Implementations§
Source§impl Clone for PluginManager
impl Clone for PluginManager
Source§fn clone(&self) -> PluginManager
fn clone(&self) -> PluginManager
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for PluginManager
impl Debug for PluginManager
Auto Trait Implementations§
impl Freeze for PluginManager
impl !RefUnwindSafe for PluginManager
impl Send for PluginManager
impl Sync for PluginManager
impl Unpin for PluginManager
impl !UnwindSafe for PluginManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more