1 unstable release
Uses old Rust 2015
0.1.0 | Sep 8, 2018 |
---|
#22 in #usize
10KB
188 lines
A system for managing typed events
Example
use std::thread;
use std::sync::atomic::{AtomicBool, Ordering};
use anymsg::{Pump, EventLoopMapping};
let mut pump = Pump::new();
// Define various messages
struct UpdateMsg { what: usize }
struct AddMsg { by: usize }
struct VerifyMsg { equals: usize }
// Define our Client, with its own state
struct ReceiverType {
counter: usize
}
// Create our initial state
let receiver = ReceiverType { counter: 1 };
#
let sender = pump.sender();
let mut mapping = EventLoopMapping::new();
mapping.add_client(receiver)
.add_handler(|this: &mut ReceiverType, msg: &UpdateMsg| {
this.counter = msg.what;
})
.add_handler(|this: &mut ReceiverType, msg: &AddMsg| {
this.counter += msg.by;
})
.add_handler(|this, msg: &VerifyMsg| {
assert!(this.counter == msg.equals);
println!("We passed the test!");
});
let event_loop = pump.event_loop(mapping);
let pthread = pump.start();
let client_thread = thread::spawn(move || {
let mut event_loop = event_loop;
while event_loop.poll_once().is_ok() {}
});
// Send our messages and terminate
sender.send(UpdateMsg {what: 7});
sender.send(AddMsg {by: 1}).unwrap();
sender.send(VerifyMsg {equals: 8}).unwrap();
sender.hang_up();
client_thread.join().unwrap();
pthread.join();