use crate::*; use std::sync::{Mutex, Arc, RwLock}; /// Implement the [Handle] trait. #[macro_export] macro_rules! handle { (|$self:ident:$Struct:ty,$input:ident|$handler:expr) => { impl Handle for $Struct { fn handle (&mut $self, $input: &E) -> Perhaps { $handler } } }; ($E:ty: |$self:ident:$Struct:ty,$input:ident|$handler:expr) => { impl Handle<$E> for $Struct { fn handle (&mut $self, $input: &$E) -> Perhaps<<$E as Input>::Handled> { $handler } } } } /// Handle input pub trait Handle: Send + Sync { fn handle (&mut self, _input: &E) -> Perhaps { Ok(None) } } impl> Handle for &mut H { fn handle (&mut self, context: &E) -> Perhaps { (*self).handle(context) } } impl> Handle for Option { fn handle (&mut self, context: &E) -> 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) -> Perhaps { self.get_mut().unwrap().handle(context) } } impl Handle for Arc> where H: Handle { fn handle (&mut self, context: &E) -> Perhaps { self.lock().unwrap().handle(context) } } impl Handle for RwLock where H: Handle { fn handle (&mut self, context: &E) -> Perhaps { self.write().unwrap().handle(context) } } impl Handle for Arc> where H: Handle { fn handle (&mut self, context: &E) -> Perhaps { self.write().unwrap().handle(context) } }