Crates.io | truba |
lib.rs | truba |
version | 0.1.6 |
source | src |
created_at | 2023-08-16 07:05:32.983092 |
updated_at | 2023-12-05 17:43:26.439009 |
description | The minimal tokio runtime based actors for Rust |
homepage | |
repository | https://github.com/noogen-projects/truba |
max_upload_size | |
id | 945588 |
size | 43,747 |
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.