From ddf7f76e449af384619f3d1b259d8476219d2e7e Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Wed, 20 Nov 2024 11:41:00 +0100 Subject: [PATCH] [#390] Fix clippy warnings and write example readme --- .../rust/_examples_common/pubsub_event.rs | 12 +++---- .../rust/event_based_communication/README.md | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/examples/rust/_examples_common/pubsub_event.rs b/examples/rust/_examples_common/pubsub_event.rs index 62e37aedd..cb3adcc45 100644 --- a/examples/rust/_examples_common/pubsub_event.rs +++ b/examples/rust/_examples_common/pubsub_event.rs @@ -23,15 +23,15 @@ pub enum PubSubEvent { Undefined, } -impl Into for PubSubEvent { - fn into(self) -> EventId { - EventId::new(self as usize) +impl From for EventId { + fn from(value: PubSubEvent) -> Self { + EventId::new(value as usize) } } -impl Into for EventId { - fn into(self) -> PubSubEvent { - match self.as_value() { +impl From for PubSubEvent { + fn from(value: EventId) -> Self { + match value.as_value() { 0 => PubSubEvent::PublisherConnected, 1 => PubSubEvent::PublisherDisconnected, 2 => PubSubEvent::SubscriberConnected, diff --git a/examples/rust/event_based_communication/README.md b/examples/rust/event_based_communication/README.md index 6c8418cc5..ff5613ccf 100644 --- a/examples/rust/event_based_communication/README.md +++ b/examples/rust/event_based_communication/README.md @@ -1,3 +1,35 @@ # Event-Based Communication ## Running The Example + +This example demonstrates iceoryx2's event multiplexing mechanism in a more +complex setup. The iceoryx2 `Publisher` and `Subscriber` are integrated into +custom `ExamplePublisher` and `ExampleSubscriber` classes, which also +incorporate an additional iceoryx2 `Notifier` and `Listener`. This setup +enables automatic event emission whenever an `ExamplePublisher` or +`ExampleSubscriber` is created or dropped. Additionally, events are emitted +whenever a new `Sample` is sent or received. + +When a `struct` implements `SynchronousMultiplexing`, it can be attached to a +`WaitSet`. Both `ExamplePublisher` and `ExampleSubscriber` implement this +interface by forwarding calls to their underlying `Listener`, which already +provides an implementation of `SynchronousMultiplexing`. + +The `WaitSet` notifies the user of the origin of an event notification. The +user can then acquire the `EventId` from the `Listener`. Based on the value of +the `EventId`, the user can identify the specific event that occurred and take +appropriate action. + +### Terminal 1 + +```sh +cargo run --example event_based_comm_publisher +``` + +### Terminal 2 + +```sh +cargo run --example event_based_comm_subscriber +``` + +Feel free to run multiple publishers or subscribers in parallel.