washed_up

Crates.iowashed_up
lib.rswashed_up
version0.1.0
sourcesrc
created_at2015-08-12 06:11:14.153512
updated_at2015-12-11 23:59:34.806356
descriptionA toy actor library
homepage
repositoryhttps://github.com/mnussbaum/washed_up
max_upload_size
id2821
size14,173
Michael Nussbaum (mnussbaum)

documentation

README

washed_up is a toy actor library for Rust.

Build Status

This actor library provides:

  • Light weight "green" threads
  • Message passing semantics

It hopes to provide in the future:

  • Integrated non-blocking IO (possibly via mioco)
  • Easier to use scheduling
  • Distributed actors and message passing

washed_up uses coroutines to provide light weight concurrency. To effectively use coroutines use non-blocking code. Since coroutines are cooperatively scheduled it's up to your code to relinquish control back to the scheduler when it has reached a good stopping point. Typically, this might mean having your actor code invoke the scheduler after processing each message.

Here's an example:

extern crate coroutine;
extern crate rustc_serialize;
extern crate uuid;
extern crate washed_up;

use coroutine::Coroutine;
use rustc_serialize::json::Json;
use uuid::Uuid;
use washed_up::Supervisor;

fn main() {
    // The supervisor coordinates our actors
    let supervisor = Supervisor::new("People");

    // Actors are identified with pids and have names primarily for debugging
    let pid: Uuid = supervisor.spawn(
        "Fred",
        |receiver| {
            loop {
                let message = receiver.recv().unwrap();
                if message.is_null() {
                    break;
                }
                println!("Received: {}", message);

                // This yields control to the scheduler so other actors can run.
                // Execution will resume right where it left off
                Coroutine::sched();
            }
            ()
        }
    ).unwrap();

    // Messages are sent with pids
    supervisor.send_message(pid, Json::from_str("{\"hello\": \"hi\"}").unwrap()).unwrap();

    // Our actor body is set to quit when it sees null
    supervisor.send_message(pid, Json::from_str("null").unwrap()).unwrap();

    // This will block until the actor body has finished
    supervisor.join(pid).unwrap()
}

Your actor body function must have the following signature:

Fn(std::sync::mpsc::Receiver<Json>) -> ()

Dependencies you will probably need to pull in:

The coroutine library requires Rust nightly, so this library does as well.

Commit count: 32

cargo fmt