Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement From<#ty> for actor message enum #1

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions riker-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
91 changes: 85 additions & 6 deletions riker-macros/tests/macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use riker::actors::*;
use riker::Message;

#[actor(String, u32)]
#[derive(Clone, Default)]
Expand Down Expand Up @@ -97,11 +98,11 @@ fn run_derived_generic_actor() {
}

#[derive(Clone, Debug)]
pub struct Message<T> {
pub struct GenericMessage<T> {
inner: T,
}

#[actor(Message<String>)]
#[actor(GenericMessage<String>)]
#[derive(Clone, Default)]
struct GenericMsgActor;

Expand All @@ -118,13 +119,13 @@ impl Actor for GenericMsgActor {
}
}

impl Receive<Message<String>> for GenericMsgActor {
impl Receive<GenericMessage<String>> for GenericMsgActor {
type Msg = GenericMsgActorMsg;

fn receive(
&mut self,
_ctx: &Context<Self::Msg>,
msg: Message<String>,
msg: GenericMessage<String>,
_sender: Option<BasicActorRef>,
) {
println!("{}", msg.inner);
Expand All @@ -137,9 +138,9 @@ fn run_generic_message_actor() {

let act = sys.actor_of::<GenericMsgActor>("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
Expand Down Expand Up @@ -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<T>
where
T: Message + From<KnownMessageType>,
{
actor: ActorRef<T>,
}

impl<T> GenericStruct<T>
where
T: Message + From<KnownMessageType>,
{
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<Self::Msg>, msg: Self::Msg, sender: Sender) {
self.receive(ctx, msg, sender);
ctx.stop(&ctx.myself);
}
}

impl Receive<KnownMessageType> for AnyActor {
type Msg = AnyActorMsg;

fn receive(
&mut self,
_ctx: &Context<Self::Msg>,
msg: KnownMessageType,
_sender: Option<BasicActorRef>,
) {
println!("Received {:?}", msg);
}
}

impl Receive<OtherMessageType> for AnyActor {
type Msg = AnyActorMsg;

fn receive(
&mut self,
_ctx: &Context<Self::Msg>,
_msg: OtherMessageType,
_sender: Option<BasicActorRef>,
) {
}
}

#[test]
fn tell_generic_actor_ref() {
let sys = ActorSystem::new().unwrap();
let actor = sys.actor_of::<AnyActor>("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));
}
}