define_actor

Macro define_actor 

Source
macro_rules! define_actor {
    ($actor_name:literal, $actor_producer:path) => { ... };
    ($actor_addr:expr, $actor_producer:path) => { ... };
}
Expand description

This macro defines a new actor instance in the system. It takes a literal string as actor name and an implmentation of Producer that is called to return an Actor. The actor becomes active as soon as it is defined and receives a startup signal.

Example

use arrows::{Actor, Addr, Mail, Msg, Producer};
use serde::{Deserialize, Serialize};

pub struct NewActor;

impl Actor for NewActor {
    fn receive(&mut self, _incoming: Mail) -> Option<Mail> {
        Some(Msg::from_text("Reply from new actor").into())
    }
}

Next we implement the Producer trait to produce NewActor intances on demand.

#[derive(Debug, Serialize, Deserialize, Default)]
struct NewProducer;

impl Producer for NewProducer {
    fn produce(&mut self) -> Box<dyn Actor> {
        Box::new(NewActor)
    }
}

At this point - we have our Actor and Producer implementations ready. we can use the define_actor macro to register an actor instance in the system. The producer will be called to return an actor instance and the producer itself would would be persisted in the system, the actor will be activated and would receive a post start signal. Same prodcer instance would be called to create instances of the actor at system restart/actor activation points at different times in the actor’s life-cycle.

use arrows::define_actor;

let actor_producer = NewProducer::default();
define_actor!("new_actor", actor_producer);

At this stage, the actor would have received a start up signal and be ready to process messages.

use arrows::send;

let m1 = Msg::from_text("Message to new_actor");
let m2 = Msg::from_text("Message to new_actor");
let m3 = Msg::from_text("Message to new_actor");
send!("new_actor", (m1, m2, m3));

We can use same producer definition to create multiple instances of the actor.

let producer2 = NewProducer::default();
let producer3 = NewProducer::default();
define_actor!("another_actor", producer2);
define_actor!("yet_another_actor", producer3);