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