1 unstable release

Uses old Rust 2015

0.1.0 Aug 12, 2015

#31 in #supervisor

MIT license

14KB
263 lines

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.

Dependencies

~4MB
~61K SLoC