#actors #tokio #async

theatre

A concise async actor model implementation

9 releases

0.1.8 Apr 6, 2021
0.1.7 Apr 6, 2021
0.1.5 Mar 18, 2021
0.1.2 Sep 17, 2020

#99 in Concurrency

Download history 9/week @ 2021-07-15 18/week @ 2021-07-29 9/week @ 2021-08-05 16/week @ 2021-08-12 1/week @ 2021-08-19 75/week @ 2021-09-16 182/week @ 2021-09-23 101/week @ 2021-09-30 268/week @ 2021-10-07 271/week @ 2021-10-14

72 downloads per month

MIT license

36KB
950 lines

Theatre

A concise async actor model implementation

See the examples folder to get a sense of how Theatre works.

Accumulator example

use theatre::prelude::*;

#[derive(Default)]
struct Accumulator {
    count: i32,
}

impl Actor for Accumulator {}

struct Increment;

impl Message for Increment {
    type Return = ();
}

struct GetCount;

impl Message for GetCount {
    type Return = i32;
}

#[async_trait]
impl Handler<Increment> for Accumulator {
    async fn handle(
        &mut self,
        _msg: Increment,
        _context: &mut Context<Self>,
    ) -> <Increment as Message>::Return {
        println!("Adding 1");
        self.count += 1
    }
}

#[async_trait]
impl Handler<GetCount> for Accumulator {
    async fn handle(
        &mut self,
        _msg: GetCount,
        _context: &mut Context<Self>,
    ) -> <GetCount as Message>::Return {
        self.count
    }
}

#[tokio::main]
async fn main() {
    let mut acc = Accumulator::default().start().await.0;

    assert_eq!(0, acc.send(GetCount).await.unwrap());

    acc.send(Increment).await.unwrap();
    acc.send(Increment).await.unwrap();
    acc.send(Increment).await.unwrap();
    acc.send(Increment).await.unwrap();

    println!("Value: {}", acc.send(GetCount).await.unwrap());
    assert_eq!(4, acc.send(GetCount).await.unwrap());
}

Dependencies

~4MB
~74K SLoC