~thatonelutenist/actm

2a78168eae68f6c662b84c08bfcf196b836154a7 — Nathan McCarty 1 year, 9 months ago 8c4be45
feat: wrapped_event!() macro

Add the wrapped_event macro, which automatically implements an `Event`
implementing type-saftey wrapper around an arbitrary type.
3 files changed, 53 insertions(+), 10 deletions(-)

M src/lib.rs
M src/testing_util.rs
M src/util/wrapped_event.rs
M src/lib.rs => src/lib.rs +1 -0
@@ 36,5 36,6 @@ pub mod prelude {
        traits::{Actor, Event, EventConsumer, EventProducer},
        types::CompletionToken,
        util::{AsyncActor, AsyncActorError, SyncActor, SyncActorError, WrappedEvent},
        wrapped_event,
    };
}

M src/testing_util.rs => src/testing_util.rs +11 -10
@@ 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<MathEventType>;
pub(crate) type OutputEvent = WrappedEvent<Output>;
wrapped_event!(MathEvent, MathEventType);
wrapped_event!(OutputEvent, Output);

// Async actor definition


M src/util/wrapped_event.rs => src/util/wrapped_event.rs +41 -0
@@ 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<CompletionToken> {
                self.0.token()
            }
            fn tokenize(&mut self) -> Option<CompletionToken> {
                self.0.tokenize()
            }
            fn stateless_clone(&self) -> Self {
                Self(self.0.stateless_clone())
            }
        }
    };
}