5 releases

0.1.6 Dec 5, 2023
0.1.5 Nov 3, 2023
0.1.4 Oct 17, 2023
0.1.3 Aug 27, 2023
0.1.2 Aug 16, 2023

#517 in Asynchronous

29 downloads per month

MIT license

27KB
712 lines

truba

The minimal tokio runtime based actors for Rust.

[dependencies]
truba = "0.1"
use truba::{Context, Message, MpscChannel};

struct Value(u32);

impl Message for Value {
    type Channel = MpscChannel<Self>;
}

struct MyActor {
    value: u32,
}

impl MyActor {
    fn run(ctx: Context, value: u32) {
        let mut value_in = ctx.receiver::<Value>();
        let mut actor = MyActor { value };

        truba::spawn_event_loop!(ctx, {
            Some(msg) = value_in.recv() => {
                actor.handle_value(msg);
            },
        });
    }

    fn handle_value(&mut self, Value(value): Value) {
        self.value = value;
        println!("receive value {value}");
    }
}

#[tokio::main]
async fn main() {
    let ctx = Context::new();
    MyActor::run(ctx.clone(), 42);

    let sender = ctx.sender::<Value>();
    sender.send(Value(11)).await.ok();
    sender.send(Value(22)).await.ok();

    ctx.shutdown().await;
}

This and more examples you can find in the examples directory.

Dependencies

~3.5–9.5MB
~89K SLoC