#events #event-loop #looper #thread #loops #single

mrogalski-looper

Clean abstraction for a single-threaded event loop. Built as a lightweight wrapper around the std::sync::mpsc package.

4 stable releases

Uses old Rust 2015

1.0.3 Jul 18, 2017

#920 in Concurrency

Apache-2.0

8KB
55 lines

looper

Clean abstraction for a single-threaded event loop. Built as a lightweight wrapper around the std::sync::mpsc package.


lib.rs:

Clean abstraction for a single-threaded event loop. Built as a lightweight wrapper around the std::sync::mpsc package.

Example usage:

use mrogalski_looper::{Handler, Sender, run};

struct ExampleHandler {
    data: Vec<i32>,
}

impl Handler<i32> for ExampleHandler {

    // Invoked right after the `run` function is called.
    fn start(&mut self, sender: Sender<i32>) {
        for elem in vec![1, 2, 3] {
            sender.send(elem.clone()).unwrap();
        }
    }

    // Called for every `event` sent to the `sender`.
    fn handle(&mut self, i: i32) -> bool {
        self.data.push(i);
        true
    }

    // Called after last event is processed or an error occurs.
    fn end(self) {
        assert_eq!(self.data, vec![1, 2, 3]);
    }
}

// Blocks the current thread until all events are processed.
run(ExampleHandler { data: vec![] });

No runtime deps