wrapper impls for Render and Handle

This commit is contained in:
🪞👃🪞 2024-08-30 20:19:36 +03:00
parent 3a7aa9e9a3
commit 777904cb35
4 changed files with 118 additions and 120 deletions

View file

@ -28,7 +28,7 @@ pub fn input_thread (
}
/// Trait for things that handle input events.
pub trait Handle {
pub trait Handle: Send + Sync {
/// Handle an input event.
/// Returns Ok(true) if the device handled the event.
/// This is the mechanism which allows nesting of components;.
@ -37,6 +37,45 @@ pub trait Handle {
}
}
impl<T: Handle> Handle for &mut T {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
(*self).handle(e)
}
}
impl<T: Handle> Handle for Option<T> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
match self {
Some(handle) => handle.handle(e),
None => Ok(false)
}
}
}
impl<T: Handle> Handle for Mutex<T> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
self.lock().unwrap().handle(e)
}
}
impl<T: Handle> Handle for Arc<Mutex<T>> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
self.lock().unwrap().handle(e)
}
}
impl<T: Handle> Handle for RwLock<T> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
self.write().unwrap().handle(e)
}
}
impl<T: Handle> Handle for Arc<RwLock<T>> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
self.write().unwrap().handle(e)
}
}
/// Implement the `Handle` trait.
#[macro_export] macro_rules! handle {
($T:ty) => {