ping/
ping.rs

1use xactor::*;
2
3/// Define `Ping` message
4#[message(result = "usize")]
5struct Ping(usize);
6
7/// Actor
8struct MyActor {
9    count: usize,
10}
11
12/// Declare actor and its context
13impl Actor for MyActor {}
14
15/// Handler for `Ping` message
16#[async_trait::async_trait]
17impl Handler<Ping> for MyActor {
18    async fn handle(&mut self, _ctx: &mut Context<Self>, msg: Ping) -> usize {
19        self.count += msg.0;
20        self.count
21    }
22}
23
24#[xactor::main]
25async fn main() -> Result<()> {
26    // start new actor
27    let addr = MyActor { count: 10 }.start().await?;
28
29    // send message and get future for result
30    let res = addr.call(Ping(10)).await?;
31    println!("RESULT: {}", res == 20);
32
33    Ok(())
34}