mediator/
event.rs

1/// Represents an application event.
2pub trait Event: Clone {}
3
4/// A handler for application events.
5pub trait EventHandler<E: Event> {
6    /// Handles an event.
7    fn handle(&mut self, event: E);
8}
9
10/// An async handler for application events.
11#[cfg(feature = "async")]
12#[cfg_attr(feature = "async", async_trait::async_trait)]
13pub trait AsyncEventHandler<E: Event> {
14    /// Handles an event.
15    async fn handle(&mut self, event: E);
16}
17
18///////////////////// Implementations /////////////////////
19
20impl<E, F> EventHandler<E> for F
21where
22    E: Event,
23    F: FnMut(E),
24{
25    fn handle(&mut self, event: E) {
26        self(event)
27    }
28}
29
30#[cfg(feature = "async")]
31#[cfg_attr(feature = "async", async_trait::async_trait)]
32impl<E, F> AsyncEventHandler<E> for F
33where
34    E: Event + Send + 'static,
35    F: FnMut(E) -> crate::futures::BoxFuture<'static, ()> + Send,
36{
37    async fn handle(&mut self, event: E) {
38        self(event).await
39    }
40}