From 4e63949b0a5456188965c2204f8957ee591787a1 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Tue, 7 Nov 2023 23:56:20 +0100 Subject: [PATCH] compilation errors fixed --- plugins/zenoh-plugin-trait/src/loading.rs | 128 +++++++++++++++------- 1 file changed, 86 insertions(+), 42 deletions(-) diff --git a/plugins/zenoh-plugin-trait/src/loading.rs b/plugins/zenoh-plugin-trait/src/loading.rs index 28e574c34..cfcd60a69 100644 --- a/plugins/zenoh-plugin-trait/src/loading.rs +++ b/plugins/zenoh-plugin-trait/src/loading.rs @@ -49,16 +49,6 @@ impl PluginCondition { pub fn add_warning>>(&mut self, warning: S) { self.warnings.push(warning.into()); } - pub fn catch_error ZResult>(&mut self, f: F) -> ZResult { - self.clear(); - match f() { - Ok(v) => Ok(v), - Err(e) => { - self.add_error(format!("{}", e)); - Err(e) - } - } - } pub fn errors(&self) -> &[Cow<'static, str>] { &self.errors } @@ -67,6 +57,19 @@ impl PluginCondition { } } +pub trait PluginConditionAddError { + fn add_error(self, condition: &mut PluginCondition) -> Self; +} + +impl PluginConditionAddError for core::result::Result { + fn add_error(self, condition: &mut PluginCondition) -> Self { + if let Err(e) = &self { + condition.add_error(e.to_string()); + } + self + } +} + #[derive(Clone, Debug, PartialEq, Eq)] pub struct PluginStatus { pub state: PluginState, @@ -136,6 +139,7 @@ where PluginStatus { state: self .instance + .as_ref() .map_or(PluginState::Loaded, |_| PluginState::Running), condition: PluginCondition::new(), // TODO: request runnnig plugin status } @@ -336,11 +340,9 @@ impl { fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin> { if self.starter.is_none() { - self.condition.catch_error(|| { - let (lib, path) = self.source.load()?; - self.starter = Some(DynamicPluginStarter::new(lib, path)?); - Ok(()) - })?; + let (lib, path) = self.source.load().add_error(&mut self.condition)?; + let starter = DynamicPluginStarter::new(lib, path).add_error(&mut self.condition)?; + self.starter = Some(starter); } Ok(self) } @@ -364,17 +366,18 @@ impl LoadedPlugin for DynamicPlugin { fn run(&mut self, args: &StartArgs) -> ZResult<&mut dyn RunningPlugin> { - self.condition.catch_error(|| { - let starter = self - .starter - .as_ref() - .ok_or_else(|| format!("Plugin `{}` not loaded", self.name))?; - let already_running = self.instance.is_some(); - if !already_running { - self.instance = Some(starter.start(self.name(), args)?); - } - Ok(()) - })?; + let starter = self + .starter + .as_ref() + .ok_or_else(|| format!("Plugin `{}` not loaded", self.name)) + .add_error(&mut self.condition)?; + let already_running = self.instance.is_some(); + if !already_running { + let instance = starter + .start(self.name(), args) + .add_error(&mut self.condition)?; + self.instance = Some(instance); + } Ok(self) } fn running(&self) -> Option<&dyn RunningPlugin> { @@ -407,12 +410,52 @@ impl } } +struct PluginRecord( + Box>, +); + +impl + PluginRecord +{ + fn new + 'static>(plugin: P) -> Self { + Self(Box::new(plugin)) + } +} + +impl PluginInfo + for PluginRecord +{ + fn name(&self) -> &str { + self.0.name() + } + fn path(&self) -> &str { + self.0.path() + } + fn status(&self) -> PluginStatus { + self.0.status() + } +} + +impl + DeclaredPlugin for PluginRecord +{ + fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin> { + self.0.load() + } + fn loaded(&self) -> Option<&dyn LoadedPlugin> { + self.0.loaded() + } + fn loaded_mut(&mut self) -> Option<&mut dyn LoadedPlugin> { + self.0.loaded_mut() + } +} + /// A plugins manager that handles starting and stopping plugins. /// Plugins can be loaded from shared libraries using [`Self::load_plugin_by_name`] or [`Self::load_plugin_by_paths`], or added directly from the binary if available using [`Self::add_static`]. pub struct PluginsManager { default_lib_prefix: String, loader: Option, - plugins: Vec>>, + plugins: Vec>, } impl @@ -442,7 +485,7 @@ impl Self { let plugin_loader: StaticPlugin = StaticPlugin::new(); - self.plugins.push(Box::new(plugin_loader)); + self.plugins.push(PluginRecord::new(plugin_loader)); self } @@ -459,13 +502,11 @@ impl; - Ok(plugin) + self.plugins.push(PluginRecord::new(loader)); + Ok(self.plugins.last_mut().unwrap()) } /// Add first available dynamic plugin from the list of paths to the plugin files @@ -477,10 +518,8 @@ impl; - Ok(plugin) + self.plugins.push(PluginRecord::new(loader)); + Ok(self.plugins.last_mut().unwrap()) } fn get_plugin_index(&self, name: &str) -> Option { @@ -494,14 +533,18 @@ impl impl Iterator> + '_ { - self.plugins.iter().map(|p| p.as_ref()) + self.plugins + .iter() + .map(|p| p as &dyn DeclaredPlugin) } /// Lists all plugins mutable - pub fn plugins_mut<'a>( - &'a mut self, - ) -> impl Iterator+'a )> + 'a { - self.plugins.iter_mut().map(move|p| p.as_mut()) + pub fn plugins_mut( + &mut self, + ) -> impl Iterator> + '_ { + self.plugins + .iter_mut() + .map(|p| p as &mut dyn DeclaredPlugin) } /// Lists the loaded plugins @@ -515,6 +558,7 @@ impl impl Iterator> + '_ { + // self.plugins_mut().filter_map(|p| p.loaded_mut()) self.plugins_mut().filter_map(|p| p.loaded_mut()) }