[][src]Trait riker::actor::Receive

pub trait Receive<Msg: Message> {
    type Msg: Message;
    pub fn receive(
        &mut self,
        ctx: &Context<Self::Msg>,
        msg: Msg,
        sender: Sender
    ); }

Receive and handle a specific message type

This trait is typically used in conjuction with the #actor attribute macro and implemented for each message type to receive.

Examples


#[derive(Clone, Debug)]
pub struct Foo;
#[derive(Clone, Debug)]
pub struct Bar;
#[actor(Foo, Bar)] // <-- set our actor to receive Foo and Bar types
#[derive(Default)]
struct MyActor;

impl Actor for MyActor {
    type Msg = MyActorMsg; // <-- MyActorMsg is provided for us

    fn recv(&mut self,
                ctx: &Context<Self::Msg>,
                msg: Self::Msg,
                sender: Sender) {
        self.receive(ctx, msg, sender); // <-- call the respective implementation
    }
}

impl Receive<Foo> for MyActor {
    type Msg = MyActorMsg;

    fn receive(&mut self,
                ctx: &Context<Self::Msg>,
                msg: Foo, // <-- receive Foo
                sender: Sender) {
        println!("Received a Foo");
    }
}

impl Receive<Bar> for MyActor {
    type Msg = MyActorMsg;

    fn receive(&mut self,
                ctx: &Context<Self::Msg>,
                msg: Bar, // <-- receive Bar
                sender: Sender) {
        println!("Received a Bar");
    }
}

// main
let sys = ActorSystem::new().unwrap();
let actor = sys.actor_of::<MyActor>("my-actor").unwrap();

actor.tell(Foo, None);
actor.tell(Bar, None);

Associated Types

Loading content...

Required methods

pub fn receive(&mut self, ctx: &Context<Self::Msg>, msg: Msg, sender: Sender)[src]

Invoked when an actor receives a message

It is guaranteed that only one message in the actor's mailbox is processed at any one time, including receive, other_receive and system_receive.

Loading content...

Implementors

impl Receive<ChannelMsg<SystemEvent>> for EventsChannel[src]

type Msg = ChannelMsg<SystemEvent>

impl Receive<Publish<SystemEvent>> for EventsChannel[src]

type Msg = ChannelMsg<SystemEvent>

impl<Msg> Receive<ChannelMsg<Msg>> for Channel<Msg> where
    Msg: Message
[src]

type Msg = ChannelMsg<Msg>

impl<Msg> Receive<Publish<Msg>> for Channel<Msg> where
    Msg: Message
[src]

type Msg = ChannelMsg<Msg>

impl<Msg> Receive<Subscribe<Msg>> for Channel<Msg> where
    Msg: Message
[src]

type Msg = ChannelMsg<Msg>

impl<Msg> Receive<Unsubscribe<Msg>> for Channel<Msg> where
    Msg: Message
[src]

type Msg = ChannelMsg<Msg>

impl<Msg> Receive<UnsubscribeAll<Msg>> for Channel<Msg> where
    Msg: Message
[src]

type Msg = ChannelMsg<Msg>

Loading content...