From b8328dcbb4759f8a1f395d26ef492e863257a2ce Mon Sep 17 00:00:00 2001 From: elenaf9 Date: Mon, 1 Feb 2021 12:59:40 +0100 Subject: [PATCH] implement From<#ty> for actor message enum --- riker-macros/src/lib.rs | 10 ++-- riker-macros/tests/macro.rs | 91 ++++++++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 11 deletions(-) diff --git a/riker-macros/src/lib.rs b/riker-macros/src/lib.rs index 90cdd5ab..f996fbf6 100644 --- a/riker-macros/src/lib.rs +++ b/riker-macros/src/lib.rs @@ -103,7 +103,7 @@ fn intos(name: &Ident, types: &MsgTypes) -> TokenStream { let intos = types .types .iter() - .map(|t| impl_into(&name, &t.name, &t.mtype)); + .map(|t| impl_from(&name, &t.name, &t.mtype)); quote! { #(#intos)* } @@ -135,11 +135,11 @@ fn receive(aname: &Ident, gen: &Generics, name: &Ident, types: &MsgTypes) -> Tok } } -fn impl_into(name: &Ident, vname: &Ident, ty: &TypePath) -> TokenStream { +fn impl_from(name: &Ident, vname: &Ident, ty: &TypePath) -> TokenStream { quote! { - impl Into<#name> for #ty { - fn into(self) -> #name { - #name::#vname(self) + impl From<#ty> for #name { + fn from(t: #ty) -> Self { + #name::#vname(t) } } } diff --git a/riker-macros/tests/macro.rs b/riker-macros/tests/macro.rs index 4f5b4966..8acd9b3f 100644 --- a/riker-macros/tests/macro.rs +++ b/riker-macros/tests/macro.rs @@ -1,4 +1,5 @@ use riker::actors::*; +use riker::Message; #[actor(String, u32)] #[derive(Clone, Default)] @@ -97,11 +98,11 @@ fn run_derived_generic_actor() { } #[derive(Clone, Debug)] -pub struct Message { +pub struct GenericMessage { inner: T, } -#[actor(Message)] +#[actor(GenericMessage)] #[derive(Clone, Default)] struct GenericMsgActor; @@ -118,13 +119,13 @@ impl Actor for GenericMsgActor { } } -impl Receive> for GenericMsgActor { +impl Receive> for GenericMsgActor { type Msg = GenericMsgActorMsg; fn receive( &mut self, _ctx: &Context, - msg: Message, + msg: GenericMessage, _sender: Option, ) { println!("{}", msg.inner); @@ -137,9 +138,9 @@ fn run_generic_message_actor() { let act = sys.actor_of::("act").unwrap(); - let msg = GenericMsgActorMsg::Message(Message { + let msg = GenericMessage { inner: "test".to_string(), - }); + }; act.tell(msg, None); // wait until all direct children of the user root are terminated @@ -222,3 +223,81 @@ fn run_path_message_actor() { std::thread::sleep(std::time::Duration::from_millis(50)); } } + +#[derive(Clone, Debug)] +pub struct KnownMessageType; + +#[derive(Clone, Debug)] +struct GenericStruct +where + T: Message + From, +{ + actor: ActorRef, +} + +impl GenericStruct +where + T: Message + From, +{ + fn msg_actor(&self, msg: KnownMessageType) { + self.actor.tell(msg, None) + } +} + +#[derive(Clone, Debug)] +pub struct OtherMessageType; + +#[actor(KnownMessageType, OtherMessageType)] +#[derive(Clone, Debug, Default)] +struct AnyActor; + +impl Actor for AnyActor { + type Msg = AnyActorMsg; + + fn supervisor_strategy(&self) -> Strategy { + Strategy::Stop + } + + fn recv(&mut self, ctx: &Context, msg: Self::Msg, sender: Sender) { + self.receive(ctx, msg, sender); + ctx.stop(&ctx.myself); + } +} + +impl Receive for AnyActor { + type Msg = AnyActorMsg; + + fn receive( + &mut self, + _ctx: &Context, + msg: KnownMessageType, + _sender: Option, + ) { + println!("Received {:?}", msg); + } +} + +impl Receive for AnyActor { + type Msg = AnyActorMsg; + + fn receive( + &mut self, + _ctx: &Context, + _msg: OtherMessageType, + _sender: Option, + ) { + } +} + +#[test] +fn tell_generic_actor_ref() { + let sys = ActorSystem::new().unwrap(); + let actor = sys.actor_of::("any").unwrap(); + let some_struct = GenericStruct { actor }; + some_struct.msg_actor(KnownMessageType); + // wait until all direct children of the user root are terminated + while sys.user_root().has_children() { + // in order to lower cpu usage, sleep here + std::thread::sleep(std::time::Duration::from_millis(50)); + } +}