1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#![allow(dead_code)]
use std::any::Any;
#[derive(Debug)]
pub struct DispatchedEvent(Box<dyn Any + Send + Sync + 'static>);
impl DispatchedEvent {
pub(crate) fn new(inner: Box<dyn Any + Send + Sync + 'static>) -> Self {
Self(inner)
}
/// Returns the actual instance of the event
/// ```
/// # use async_trait::async_trait;
/// # use orsomafo::{Dispatchable, DispatchedEvent, EventDispatcherBuilder, EventHandler};
/// # use tokio::time::{sleep, Duration};
///
/// # #[tokio::main]
/// # async fn main() {
/// # _ = EventDispatcherBuilder::new().build().await;
///
/// #[derive(Clone, serde::Serialize, serde::Deserialize)]
/// struct MyEvent;
/// impl Dispatchable for MyEvent {}
///
/// struct MyEventHandler;
///
/// #[orsomafo::async_trait]
/// impl EventHandler for MyEventHandler {
/// async fn handle(&self, dispatched: &DispatchedEvent) {
/// let event: MyEvent = dispatched.the_event().unwrap();
/// // or
/// // let event = dispatched.the_event::<MyEvent>().unwrap()
/// //...
/// }
/// }
///
/// }
/// ```
pub fn the_event<T: Clone + 'static>(&self) -> Option<T> {
let result: Option<&T> = self.0.downcast_ref();
result.cloned()
}
}