Commit 8fb5e5cd authored by Ralf's avatar Ralf
Browse files

varagrs-macro for handle_events

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