summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples/heartbeat.rs
diff options
context:
space:
mode:
authorMarcel Müller <m.mueller@ifm.com>2022-04-11 10:09:06 +0200
committerMarcel Müller <m.mueller@ifm.com>2022-04-11 11:09:45 +0200
commit94c6e4359e0ca441a68400bea6938dd79aa481f4 (patch)
tree21b5f280973ce55696d31d2ae47cab3247a8d4de /crates/core/tedge_api/examples/heartbeat.rs
parent1d5d93b1084f225f801b1509c3367d83aec62f8e (diff)
Add PluginDeclaration trait
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 <m.mueller@ifm.com>
Diffstat (limited to 'crates/core/tedge_api/examples/heartbeat.rs')
-rw-r--r--crates/core/tedge_api/examples/heartbeat.rs19
1 files changed, 14 insertions, 5 deletions
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<PD: PluginDirectory> PluginBuilder<PD> 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<PD: PluginDirectory> PluginBuilder<PD> for CriticalServiceBuilder {
where
Self: Sized,
{
- HandleTypes::declare_handlers_for::<(Heartbeat,), CriticalService>()
+ CriticalService::get_handled_types()
}
async fn verify_configuration(
@@ -225,7 +230,7 @@ impl<PD: PluginDirectory> PluginBuilder<PD> for CriticalServiceBuilder {
Ok(CriticalService {
status: tokio::sync::Mutex::new(true),
}
- .into_untyped::<(Heartbeat,)>())
+ .into_untyped())
}
}
@@ -259,6 +264,10 @@ impl Handle<Heartbeat> 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 {