Wactor
WASM actor system based on lunatic.
Actors run on isolated green threads. They cannot share memory, and communicate only through input
and output messages. Consequently messages must be serialized to travel between threads.
Example
use serde::{Deserialize, Serialize};
use wactor::*;
struct Counter {
count: u32,
}
#[derive(Serialize, Deserialize)]
enum Input {
AddOne,
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Output {
Count(u32),
}
impl Actor for Counter {
type Input = Input;
type Output = Output;
fn create() -> Self {
Self { count: 0 }
}
fn handle(&mut self, msg: Self::Input, link: &Link<Self>) {
match msg {
Input::AddOne => {
self.count += 1;
link.respond(Output::Count(self.count)).ok();
}
}
}
}
fn main() {
let bridge = wactor::spawn::<Counter>();
bridge.send(Input::AddOne).expect("Dead actor");
let result = bridge.receive();
assert_eq!(result, Ok(Output::Count(1)));
}
How to run
Install lunatic then build and run:
cargo build --release --target=wasm32-wasi --example basic
lunatic target/wasm32-wasi/release/examples/basic.wasm