truba

Crates.iotruba
lib.rstruba
version0.1.6
sourcesrc
created_at2023-08-16 07:05:32.983092
updated_at2023-12-05 17:43:26.439009
descriptionThe minimal tokio runtime based actors for Rust
homepage
repositoryhttps://github.com/noogen-projects/truba
max_upload_size
id945588
size43,747
Alexander Mescheryakov (XX)

documentation

README

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.

Commit count: 25

cargo fmt