pub struct PluginRegistry { /* private fields */ }Expand description
插件注册表(运行阶段使用,无锁)
由 PluginManagerBuilder::build() 生成,存储不可变的插件集合。
运行阶段通过 HashMap 直接查找,无需获取任何锁,实现 O(1) 无锁查找。
拓扑排序结果在构建时计算一次并缓存,多次调用 get_all() 无需重新计算。
§示例
ⓘ
use yang_base::plugin::{Plugin, PluginManagerBuilder};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut builder = PluginManagerBuilder::new();
builder.register(MyPlugin).await?;
let registry = builder.build();
// 无锁 O(1) 查找
if let Some(plugin) = registry.get("my_plugin") {
println!("找到插件: {}", plugin.name());
}
// 获取缓存的排序结果(无需重新计算)
let all_plugins = registry.get_all();
// 关闭所有插件
registry.shutdown().await?;
Ok(())
}Implementations§
Source§impl PluginRegistry
impl PluginRegistry
Sourcepub fn get(&self, name: &str) -> Option<&Arc<dyn Plugin>>
pub fn get(&self, name: &str) -> Option<&Arc<dyn Plugin>>
查找插件(无锁,O(1) HashMap 查找)
直接通过 HashMap 查找,无需获取任何锁。
§参数
name: 插件名称
§返回
Some(&Arc<dyn Plugin>): 找到插件None: 插件不存在
§示例
ⓘ
use yang_base::plugin::PluginManagerBuilder;
let registry = PluginManagerBuilder::new().build();
if let Some(plugin) = registry.get("my_plugin") {
println!("找到插件: {}", plugin.name());
}Sourcepub fn get_config(&self, name: &str) -> Option<&JsonValue>
pub fn get_config(&self, name: &str) -> Option<&JsonValue>
Auto Trait Implementations§
impl !RefUnwindSafe for PluginRegistry
impl !UnwindSafe for PluginRegistry
impl Forget for PluginRegistry
impl Freeze for PluginRegistry
impl Move for PluginRegistry
impl Send for PluginRegistry
impl Sync for PluginRegistry
impl Unpin for PluginRegistry
impl UnsafeUnpin for PluginRegistry
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> 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