Commit 8fb5e5cd authored by Ralf's avatar Ralf

varagrs-macro for handle_events

parent d65eb353
......@@ -20,6 +20,12 @@ pub struct Environment {
door_locked: bool,
}
macro_rules! handle_events {
( $env:expr, $d:expr, $( $x:expr ),* ) => {
{ $env.handle_events($d, vec![$( $x, )*] ) }
};
}
impl Environment {
pub fn handle_events(&mut self,
d: Duration,
......
......@@ -8,6 +8,7 @@ pub enum HandlerResult {
QuitLoop,
}
// TODO: Consider making this a trait, or a type alias for a raw FnMut
pub struct EventHandler<'a> {
run: Box<FnMut(Event) -> HandlerResult + 'a>,
}
......
struct SphinxHead;
#[macro_use]
mod environment;
mod event_handler;
use std::time::Duration;
......@@ -11,7 +13,7 @@ use actors::Actor;
use input::Event;
use util::{Wakeable, Thread};
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
enum LockAction {
PressLock,
PressUnlock,
......@@ -29,6 +31,7 @@ trait State {
fn user_command_reject_handler(ev: Event) {}
#[derive(Debug)]
struct UnLockingState {
locking: bool,
}
......@@ -45,16 +48,14 @@ impl State for UnLockingState {
lock_actor.act(LockAction::Release);
lock_actor
});
let r = env.handle_events(Duration::from_secs(1),
vec![
let r = handle_events!(env, Duration::from_secs(1),
EventHandler::new_safe(user_command_reject_handler),
EventHandler::new(|ev| {
match ev {
Event::DoorLocked(b) if b == locking => HandlerResult::QuitLoop,
_ => HandlerResult::Continue,
}
}),
]);
}));
// take back lock_actor to feel complete again
lock_actor = locker.terminate();
if r != EventsResult::Timeout {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment