swactor/README.md
zacheryasc e2c4f55941 refactor: major library changes (#5)
Refactoring to logically separate component modules in order to make it easier to develop tests, metrics, tracing, etc.


Signed-off-by: Zachery Aaron Shores-Chmielewski <zacheryasc@gmail.com>
2026-02-06 11:25:37 +00:00

1.4 KiB

swactor

(S)mall (W)ASM-compatible (actor) library

Quick example

use swactor::{
    Ctx,
    actor::{ActorAddress, ActorInterface},
    runtime::{Runtime, RuntimeConfig},
};

#[derive(Debug, Default)]
struct Greeter { num_greeted: usize }

#[derive(Debug, Default, Clone)]
struct GreetMessage { who: String, return_addr: ActorAddress }

#[derive(Debug, Default, Clone)]
struct GreetResponse(String);

impl ActorInterface for Greeter {
    type Incoming = GreetMessage;
    type Response = GreetResponse;

    fn handle(&mut self, ctx: &Ctx, msg: GreetMessage) {
        let res = GreetResponse(format!("Hello, {}!", msg.who));
        self.num_greeted += 1;
        if let Err(_) = ctx.send(msg.return_addr, res) {
            self.num_greeted -= 1;
        }
    }
}

fn main() {
    let rt = Runtime::new(RuntimeConfig::default());
    let addr = rt.spawn(Greeter::default()).expect("failed to spawn");

    let inbox = rt.new_inbox::<GreetResponse>().unwrap();
    rt.send_to(addr, GreetMessage {
        who: "world".into(),
        return_addr: *inbox.addr(),
    }).unwrap();

    for _ in 0..3 { rt.tick(); }
    let resp = inbox.try_recv().expect("should have response");
    println!("{}", resp.0); // "Hello, world!"
}

Build & test

cargo build
cargo test
cargo test --features stress   # stress tests
cargo run --bin bench --release # benchmarks
cargo run --example hello