From 2a78168eae68f6c662b84c08bfcf196b836154a7 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Fri, 18 Nov 2022 17:34:57 -0500 Subject: [PATCH] feat: wrapped_event!() macro Add the wrapped_event macro, which automatically implements an `Event` implementing type-saftey wrapper around an arbitrary type. --- src/lib.rs | 1 + src/testing_util.rs | 21 ++++++++++---------- src/util/wrapped_event.rs | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e26bc56..6bde47f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,5 +36,6 @@ pub mod prelude { traits::{Actor, Event, EventConsumer, EventProducer}, types::CompletionToken, util::{AsyncActor, AsyncActorError, SyncActor, SyncActorError, WrappedEvent}, + wrapped_event, }; } diff --git a/src/testing_util.rs b/src/testing_util.rs index 817ff03..b7d5858 100644 --- a/src/testing_util.rs +++ b/src/testing_util.rs @@ -10,19 +10,20 @@ use crate::{ traits::{Actor, Event, EventConsumer, EventProducer}, types::CompletionToken, util::{AsyncActor, AsyncActorError, SyncActor, SyncActorError, WrappedEvent}, + wrapped_event, }; // Define the Event types, here the context type is an i64 /// Dummy Event implementation for use in unit testing #[enum_dispatch] -pub(crate) trait Math { +pub trait Math { // Operate on a value fn operate(&self, value: i64) -> i64; } #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)] -pub(crate) struct Add(pub(crate) i64); +pub struct Add(pub(crate) i64); impl Math for Add { fn operate(&self, value: i64) -> i64 { value.overflowing_add(self.0).0 @@ -30,7 +31,7 @@ impl Math for Add { } #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)] -pub(crate) struct Subtract(pub(crate) i64); +pub struct Subtract(pub(crate) i64); impl Math for Subtract { fn operate(&self, value: i64) -> i64 { value.overflowing_sub(self.0).0 @@ -39,20 +40,20 @@ impl Math for Subtract { #[enum_dispatch(Math)] #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)] -pub(crate) enum MathEventType { +pub enum MathEventType { Add, Subtract, } #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)] -pub(crate) struct Output { - pub(crate) before: i64, - pub(crate) after: i64, - pub(crate) input: MathEventType, +pub struct Output { + pub before: i64, + pub after: i64, + pub input: MathEventType, } -pub(crate) type MathEvent = WrappedEvent; -pub(crate) type OutputEvent = WrappedEvent; +wrapped_event!(MathEvent, MathEventType); +wrapped_event!(OutputEvent, Output); // Async actor definition diff --git a/src/util/wrapped_event.rs b/src/util/wrapped_event.rs index 052f653..198c44c 100644 --- a/src/util/wrapped_event.rs +++ b/src/util/wrapped_event.rs @@ -57,3 +57,44 @@ where Self(self.0.clone(), None, self.2) } } + +/// Automatically generate an [`Event`] implementing wrapper for a type +#[macro_export] +macro_rules! wrapped_event { + ($wrapper_name: ident, $event_type: ident) => { + #[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Hash)] + pub struct $wrapper_name(WrappedEvent<$event_type>); + impl $wrapper_name { + pub fn into_inner(self) -> $event_type { + self.0.into_inner() + } + pub fn set_completion_token(&mut self, token: CompletionToken) { + self.0.set_completion_token(token); + } + pub fn set_priority(&mut self, priority: bool) { + self.0.set_priority(priority); + } + } + impl From<$event_type> for $wrapper_name { + fn from(item: $event_type) -> Self { + Self(WrappedEvent::from(item)) + } + } + impl Event for $wrapper_name { + type Flags = (); + fn flags(&self) -> Self::Flags {} + fn priority(&self) -> bool { + self.0.priority() + } + fn token(&mut self) -> Option { + self.0.token() + } + fn tokenize(&mut self) -> Option { + self.0.tokenize() + } + fn stateless_clone(&self) -> Self { + Self(self.0.stateless_clone()) + } + } + }; +} -- 2.45.2