1use crate::event::Event;
2use std::fmt::Debug;
3use std::rc::Rc as Shared;
4
5pub trait Action<S, I>: Debug {
7 type Err: std::error::Error;
8 fn call(&self, e: &Event<S, I>) -> Result<(), Self::Err>;
9}
10
11type WrapFn<'a, S, I, E> = Shared<dyn Fn(&Event<S, I>) -> Result<(), E> + 'a>;
12
13pub struct Closure<'a, S, I, E>(pub(crate) WrapFn<'a, S, I, E>);
16
17impl<'a, S, I, E> Closure<'a, S, I, E> {
18 pub fn new<F>(f: F) -> Self
19 where
20 F: Fn(&Event<S, I>) -> Result<(), E> + 'a,
21 {
22 Self(Shared::new(f))
23 }
24}
25
26impl<'a, S, I, E: std::error::Error> Action<S, I> for Closure<'a, S, I, E> {
27 type Err = E;
28 fn call(&self, e: &Event<S, I>) -> Result<(), Self::Err> {
29 (self.0)(e)
30 }
31}
32
33impl<'a, S, I, E> Debug for Closure<'a, S, I, E> {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 write!(f, "<Closure>")
36 }
37}
38
39impl<'a, S, I, E> Clone for Closure<'a, S, I, E> {
40 fn clone(&self) -> Self {
41 Self(self.0.clone())
42 }
43}