concurrent_event/
handler.rs

1//! Contains the definition of the event handler trait as well as some standard
2//! implementations for common use cases.
3
4/// A trait for event handlers which can be registered with an event. For
5/// comfort, an implementation for `Box<dyn EventHandler<A>>` is provided.
6///
7/// # Type Parameters
8///
9/// * `A`: The type of event arguments accepted by this handler.
10pub trait EventHandler<A> : Send {
11    fn on_event(&mut self, arg: A);
12}
13
14/// An event handler which manages a simple closure that receives no state
15/// except the event argument. The closure is executed every time an event is
16/// received.
17///
18/// # Type Parameters
19///
20/// * `A`: The type of event arguments accepted by this handler.
21///
22/// # Example
23///
24/// ```
25/// use concurrent_event::Event;
26/// use concurrent_event::handler::StatelessEventHandler;
27///
28/// let mut ev = Event::<&str, StatelessEventHandler<&str>>::new();
29/// let handler = StatelessEventHandler::new(|arg: &str| println!("{}", arg));
30/// ev.add_handler(handler);
31/// ev.emit("Hello World!");
32/// ```
33pub struct StatelessEventHandler<'a, A> {
34    func: Box<dyn Fn(A) + Send + 'a>
35}
36
37impl<'a, A> StatelessEventHandler<'a, A> {
38    /// Creates a new stateless event handler from a closure.
39    ///
40    /// # Parameters
41    ///
42    /// * `f`: A closure which is executed every time an event is received. It
43    /// consumes the event argument.
44    pub fn new(f: impl Fn(A) + Send + 'a) -> StatelessEventHandler<'a, A> {
45        StatelessEventHandler {
46            func: Box::new(f)
47        }
48    }
49}
50
51impl<'a, A> EventHandler<A> for StatelessEventHandler<'a, A> {
52    fn on_event(&mut self, arg: A) {
53        (self.func)(arg)
54    }
55}
56
57/// An event handler which manages a closure together with some state which can
58/// track information over multiple events. The closure is executed with a
59/// mutable reference of the state every time an event is received.
60///
61/// # Type Parameters
62///
63/// * `A`: The type of event arguments accepted by this handler.
64/// * `S`: The type of the state maintained by this handler.
65///
66/// # Example
67///
68/// ```
69/// use concurrent_event::Event;
70/// use concurrent_event::handler::StatefulEventHandler;
71///
72/// let mut ev = Event::<i32, StatefulEventHandler<i32, i32>>::new();
73/// let handler = StatefulEventHandler::new(|arg: i32, state: &mut i32| *state += arg, 0);
74/// let id = ev.add_handler(handler);
75/// ev.emit(2);
76/// ev.emit(3);
77/// let state = *ev.get_handler(id).unwrap().state();
78/// 
79/// assert_eq!(5, state);
80/// ```
81pub struct StatefulEventHandler<'a, A, S: Send> {
82    func: Box<dyn Fn(A, &mut S) + Send + 'a>,
83    state: S
84}
85
86impl<'a, A, S: Send> StatefulEventHandler<'a, A, S> {
87
88    /// Creates a new stateful event handler from a closure and the initial
89    /// state.
90    ///
91    /// # Parameters
92    ///
93    /// * `f`: A closure which is executed every time an event is received. It
94    /// consumes the event argument and gets a mutable reference to the current
95    /// state.
96    /// * `initial_state`: The initial state given to the closure in the first
97    /// received event.
98    pub fn new<F>(f: F, initial_state: S) -> StatefulEventHandler<'a, A, S>
99    where
100        F : Fn(A, &mut S) + Send + 'a
101    {
102        StatefulEventHandler {
103            func: Box::new(f),
104            state: initial_state
105        }
106    }
107
108    /// Gets the current state.
109    pub fn state(&self) -> &S {
110        &self.state
111    }
112}
113
114impl<'a, A, S: Send> EventHandler<A> for StatefulEventHandler<'a, A, S> {
115    fn on_event(&mut self, arg: A) {
116        (self.func)(arg, &mut self.state)
117    }
118}
119
120impl<'a, A> EventHandler<A> for Box<dyn EventHandler<A> + 'a> {
121    fn on_event(&mut self, arg: A) {
122        self.as_mut().on_event(arg)
123    }
124}