Commit 36241923 authored by Ralf's avatar Ralf
Browse files

turn EventHandler into a mere type alias

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