uppercut

Crates.iouppercut
lib.rsuppercut
version0.4.0
sourcesrc
created_at2020-05-12 08:59:17.950364
updated_at2022-06-06 19:22:41.906964
descriptionSmall and simple actor model implementation
homepagehttps://github.com/sergey-melnychuk/uppercut
repositoryhttps://github.com/sergey-melnychuk/uppercut
max_upload_size
id240596
size139,736
Sergey Melnychuk (sergey-melnychuk)

documentation

https://github.com/sergey-melnychuk/uppercut

README

Uppercut

Simple and small actor model implementation.

Install

Cargo.toml

[dependencies]
uppercut = "0.3"

Example

hello.rs

#[derive(Debug)]
struct Message(usize, Sender<usize>);

#[derive(Default)]
struct State;

impl AnyActor for State {
    fn receive(&mut self, envelope: Envelope, sender: &mut dyn AnySender) {
        if let Some(msg) = envelope.message.downcast_ref::<Message>() {
            sender.log(&format!("received: {:?}", msg));
            msg.1.send(msg.0).unwrap();
        }
    }
}

fn main() {
    // Total 6 threads:
    // = 1 scheduler thread (main event loop)
    // + 4 actor-worker threads (effective parallelism level)
    // + 1 background worker thread (logging, metrics, "housekeeping")
    let tp = ThreadPool::new(6);

    let cfg = Config::default();
    let sys = System::new("basic", "localhost", &cfg);
    let run = sys.run(&tp).unwrap();

    run.spawn_default::<State>("state");

    let (tx, rx) = channel();
    run.send("state", Envelope::of(Message(42, tx)));

    let timeout = Duration::from_secs(3);
    let result = rx.recv_timeout(timeout).unwrap();
    println!("result: {}", result);
    run.shutdown();
}
$ cargo run --example hello
[...]
result: 42

pi.rs

$ cargo run --release --example pi
[...]
Submitting 10000 workers making 100000 throws each.
Pi estimate: 3.141561988 (in 5 seconds)

More examples

Commit count: 120

cargo fmt