mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use crate::*;
|
|
|
|
/// Handle input
|
|
pub trait Handle<T, U>: Send + Sync {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U>;
|
|
}
|
|
|
|
impl<H, T, U> Handle<T, U> for &mut H where H: Handle<T, U> {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
|
(*self).handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, T, U> Handle<T, U> for Option<H> where H: Handle<T, U> {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
|
if let Some(ref mut handle) = self {
|
|
handle.handle(context)
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<H, T, U> Handle<T, U> for Mutex<H> where H: Handle<T, U> {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
|
self.lock().unwrap().handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, T, U> Handle<T, U> for Arc<Mutex<H>> where H: Handle<T, U> {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
|
self.lock().unwrap().handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, T, U> Handle<T, U> for RwLock<H> where H: Handle<T, U> {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
|
self.write().unwrap().handle(context)
|
|
}
|
|
}
|
|
|
|
impl<H, T, U> Handle<T, U> for Arc<RwLock<H>> where H: Handle<T, U> {
|
|
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
|
self.write().unwrap().handle(context)
|
|
}
|
|
}
|