use std::any::Any;
use std::sync::Arc;
pub type EventData = Arc<dyn Any + Send + Sync>;
pub struct Event {
pub event_type: String,
pub data: EventData,
}
impl Event {
pub fn new(event_type: impl Into<String>) -> Self {
Self::with_data(event_type, Arc::new(()))
}
pub fn with_data(event_type: impl Into<String>, data: EventData) -> Self {
Self {
event_type: event_type.into(),
data,
}
}
}
impl std::fmt::Debug for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Event")
.field("event_type", &self.event_type)
.field("data", &"<Arc<dyn Any>>")
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn downcast_works() {
let e = Event::with_data("eTick.", Arc::new(42u32));
let v = e.data.downcast_ref::<u32>().unwrap();
assert_eq!(*v, 42);
}
}