Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

intendednull/wactor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wactor

A dead simple actor api wrapper for 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 => {
                // Increment count by 1.
                self.count += 1;
                // Respond with new count. This fails if our recipient has been dropped.
                link.respond(Output::Count(self.count)).ok();
            }
        }
    }
}

fn main() {
    // Spawn our actor. We get a bridge for sending and receiving messages. Can be cloned for
    // multiple owners. Actor is dropped after all bridges have been dropped.
    let bridge = wactor::spawn::<Counter>();
    // Send our input message. This fails if our actor has panicked (unrecoverable error).
    bridge.send(Input::AddOne).expect("Dead actor");
    // Block until a response is received. This also fails if our actor has panicked.
    let result = bridge.receive();
    // Assert we received the correct value.
    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

About

Ultra minimal actor API wrapper for lunatic

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages