mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
69 lines
1.9 KiB
Rust
69 lines
1.9 KiB
Rust
use crate::*;
|
|
use std::sync::{Arc, Mutex, RwLock};
|
|
|
|
/// Handle input
|
|
pub trait Handle<E: Engine>: Send + Sync {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled>;
|
|
}
|
|
|
|
/// Current input state
|
|
pub trait Input<E: Engine> {
|
|
/// 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<E: Engine, H: Handle<E>> Handle<E> for &mut H {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
|
(*self).handle(context)
|
|
}
|
|
}
|
|
|
|
impl<E: Engine, H: Handle<E>> Handle<E> for Option<H> {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
|
if let Some(ref mut handle) = self {
|
|
handle.handle(context)
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<H, E: Engine> Handle<E> for Mutex<H> where H: Handle<E> {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
|
self.get_mut().unwrap().handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, E: Engine> Handle<E> for Arc<Mutex<H>> where H: Handle<E> {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
|
self.lock().unwrap().handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, E: Engine> Handle<E> for RwLock<H> where H: Handle<E> {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
|
self.write().unwrap().handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, E: Engine> Handle<E> for Arc<RwLock<H>> where H: Handle<E> {
|
|
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
|
self.write().unwrap().handle(context)
|
|
}
|
|
}
|