actor

Macro actor 

Source
macro_rules! actor {
    ($spawner:ident, $name:ident, $ty:ty, $instance:expr) => { ... };
    ($spawner:ident, $name:ident, $ty:ty, $instance:expr, $mutex:ty) => { ... };
    ($spawner:ident, $name:ident, $ty:ty, $instance:expr, $queue_size:expr) => { ... };
    ($spawner:ident, $name:ident, $ty:ty, $instance:expr, $mutex:ty, $queue_size:expr) => { ... };
}
Expand description

Spawn an actor given a spawner and the actors name, type and instance.

actor! is a macro that simplifies the process of spawning an actor. It creates a new ActorContext and spawns the actor with the given spawner.

§Arguments

actor!(spawner, name, type, instance, [mutex type], [queue size]);

spawnerThe spawner to use to spawn the actor (i.e. embassy_executor::Spawner)
nameThe name of the actor, used to generate the task name
typeThe type of the actor, must implement the Actor trait
instanceThe instance of the actor to spawn
mutex typeThe type of mutex to use for the actor. (defaults to embassy_sync::blocking_mutex::raw::NoopRawMutex)
queue sizeThe size of the actor’s message queue. (defaults to 1)

§Example

use ector::*;

type Addr = DynamicAddress<Request<String, String>>;

struct Server;

impl Actor for Server {
    type Message = Request<String, String>;
    async fn on_mount<M>(&mut self, _: Addr, mut inbox: M) -> !
    where
       M: Inbox<Self::Message>,
   {
       println!("Server started!");

       loop {
           let motd = inbox.next().await;
           let m = motd.as_ref().clone();
           motd.reply(m).await;
       }
   }
}

#[embassy_executor::main]
async fn main(s: embassy_executor::Spawner) {
    let server_addr: Addr = actor!(s, server, Server, Server).into();
    loop {
         let r = server_addr.request("Hello".to_string()).await;
         println!("Server returned {}", r);
    }
}