From 94c6e4359e0ca441a68400bea6938dd79aa481f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Mon, 11 Apr 2022 10:09:06 +0200 Subject: Add PluginDeclaration trait MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This trait simplifies a lot of the message calls in the PluginBuilder. Advantages: You only declare a list of handled messages _once_. This is necessary since Rust does not allow 'queries' for types (aka "give me a tuple of supported types" does not exist), and so the necessary crutch is to declare them manually. Disadvantages: You cannot pick at Runtime anymore what messages you decide to support. This is considered to be a very niche use-case and might actually be an anti-pattern. Signed-off-by: Marcel Müller --- crates/core/tedge_api/examples/heartbeat.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'crates/core/tedge_api/examples/heartbeat.rs') diff --git a/crates/core/tedge_api/examples/heartbeat.rs b/crates/core/tedge_api/examples/heartbeat.rs index 15793bfc..7fae395e 100644 --- a/crates/core/tedge_api/examples/heartbeat.rs +++ b/crates/core/tedge_api/examples/heartbeat.rs @@ -9,8 +9,9 @@ use futures::FutureExt; use tedge_api::{ address::ReplySender, message::NoReply, - plugin::{BuiltPlugin, Handle, HandleTypes, Message, PluginExt}, - Address, Plugin, PluginBuilder, PluginConfiguration, PluginDirectory, PluginError, CancellationToken, + plugin::{BuiltPlugin, Handle, HandleTypes, Message, PluginDeclaration, PluginExt}, + Address, CancellationToken, Plugin, PluginBuilder, PluginConfiguration, PluginDirectory, + PluginError, }; /// A message that represents a heartbeat that gets sent to plugins @@ -78,7 +79,7 @@ impl PluginBuilder for HeartbeatServiceBuilder { monitored_services, cancellation_token, ) - .into_untyped::<()>()) + .into_untyped()) } } @@ -96,6 +97,10 @@ struct HeartbeatService { cancel_token: CancellationToken, } +impl PluginDeclaration for HeartbeatService { + type HandledMessages = (); +} + #[async_trait] impl Plugin for HeartbeatService { /// The setup function of the HeartbeatService can be used by the plugin author to setup for @@ -203,7 +208,7 @@ impl PluginBuilder for CriticalServiceBuilder { where Self: Sized, { - HandleTypes::declare_handlers_for::<(Heartbeat,), CriticalService>() + CriticalService::get_handled_types() } async fn verify_configuration( @@ -225,7 +230,7 @@ impl PluginBuilder for CriticalServiceBuilder { Ok(CriticalService { status: tokio::sync::Mutex::new(true), } - .into_untyped::<(Heartbeat,)>()) + .into_untyped()) } } @@ -259,6 +264,10 @@ impl Handle for CriticalService { } } +impl PluginDeclaration for CriticalService { + type HandledMessages = (Heartbeat,); +} + /// Because the CriticalService is of course a Plugin, it needs an implementation for that as well. #[async_trait] impl Plugin for CriticalService { -- cgit v1.2.3