use crate::*; use std::sync::{Arc, Mutex, RwLock}; /// Handle input pub trait Handle: Send + Sync { fn handle (&mut self, context: &E::Input) -> Perhaps; } /// Current input state pub trait Input { /// Type of input event type Event; /// Currently handled event fn event (&self) -> &Self::Event; /// Whether component should exit fn is_done (&self) -> bool; /// Mark component as done fn done (&self); } #[macro_export] macro_rules! handle { (<$E:ty>|$self:ident:$Struct:ty,$input:ident|$handler:expr) => { impl Handle<$E> for $Struct { fn handle (&mut $self, $input: &<$E as Engine>::Input) -> Perhaps<<$E as Engine>::Handled> { $handler } } } } impl> Handle for &mut H { fn handle (&mut self, context: &E::Input) -> Perhaps { (*self).handle(context) } } impl> Handle for Option { fn handle (&mut self, context: &E::Input) -> Perhaps { if let Some(ref mut handle) = self { handle.handle(context) } else { Ok(None) } } } impl Handle for Mutex where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.get_mut().unwrap().handle(context) } } impl Handle for Arc> where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.lock().unwrap().handle(context) } } impl Handle for RwLock where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.write().unwrap().handle(context) } } impl Handle for Arc> where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.write().unwrap().handle(context) } }