Commit 36241923 authored by Ralf's avatar Ralf
Browse files

turn EventHandler into a mere type alias

parent 0e927ff8
...@@ -14,49 +14,20 @@ pub enum HandlerResult { ...@@ -14,49 +14,20 @@ pub enum HandlerResult {
QuitLoop, QuitLoop,
} }
// TODO: Consider making this a trait, or a type alias for a raw FnMut pub type EventHandler<'a> = Box<FnMut(Event) -> HandlerResult + 'a>;
pub struct EventHandler<'a> {
run: Box<FnMut(Event) -> HandlerResult + 'a>,
}
impl<'a> EventHandler<'a> {
pub fn new<F>(f: F) -> Self
where F: FnMut(Event) -> HandlerResult + 'a
{
EventHandler { run: Box::new(f) }
}
pub fn new_boxed(f: Box<FnMut(Event) -> HandlerResult + 'a>) -> Self {
EventHandler { run: f }
}
pub fn new_safe<F>(mut f: F) -> Self
where F: FnMut(Event) + 'a
{
EventHandler {
run: Box::new(move |ev| {
f(ev);
HandlerResult::Continue
}),
}
}
pub fn new_safe_boxed(mut f: Box<FnMut(Event) + 'a>) -> Self { pub fn mk_quitting_handler<'a, F>(f: F) -> EventHandler<'a>
EventHandler { where F: FnMut(Event) -> HandlerResult + 'a
run: Box::new(move |ev| { {
f(ev); Box::new(f)
HandlerResult::Continue
}),
}
}
#[inline]
pub fn run(&mut self, ev: Event) -> HandlerResult {
(self.run)(ev)
}
} }
pub fn mk_handler<'a, F>(mut f: F) -> EventHandler<'a>
where F: FnMut(Event) + 'a
{
Box::new(move |ev| { f(ev); HandlerResult::Continue })
}
#[must_use] #[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)] #[derive(Copy,Clone,Debug,PartialEq,Eq)]
...@@ -149,7 +120,7 @@ impl Environment { ...@@ -149,7 +120,7 @@ impl Environment {
self.sensors.record_state(&ev); self.sensors.record_state(&ev);
// forward to all handlers // forward to all handlers
for handler in handlers.iter_mut() { for handler in handlers.iter_mut() {
match handler.run(ev) { match handler(ev) {
HandlerResult::Continue => {} HandlerResult::Continue => {}
HandlerResult::QuitLoop => return EventsResult::Quit, HandlerResult::QuitLoop => return EventsResult::Quit,
} }
......
...@@ -39,8 +39,8 @@ impl Situation for UnLocking { ...@@ -39,8 +39,8 @@ impl Situation for UnLocking {
// for max. 1 second, handle whatever events we see // for max. 1 second, handle whatever events we see
let r = handle_events_timeout!(env, let r = handle_events_timeout!(env,
Duration::from_secs(1), Duration::from_secs(1),
EventHandler::new_safe(handlers::user_command_reject_handler), mk_handler(handlers::user_command_reject_handler),
EventHandler::new(|ev| { mk_quitting_handler(|ev| {
match ev { match ev {
Event::DoorLocked(b) if b == locking => { Event::DoorLocked(b) if b == locking => {
HandlerResult::QuitLoop HandlerResult::QuitLoop
...@@ -88,7 +88,7 @@ impl Situation for SpaceOpen { ...@@ -88,7 +88,7 @@ impl Situation for SpaceOpen {
buzz_actor buzz_actor
}); });
let r = handle_events!(env, let r = handle_events!(env,
EventHandler::new_safe(|ev| { mk_handler(|ev| {
if let Event::Bell(true) = ev { if let Event::Bell(true) = ev {
buzzer.respawn(Duration::from_secs(2)); buzzer.respawn(Duration::from_secs(2));
} }
......
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