Hi!
I want to create an event dispatcher, to begin, I want to be able to register into it multiple listener for Any
events
the handlers should be implemented like this:
async fn (event: &SomeEvent) -> Result<(), ...> { todo!() }
To begin, I'm focusing on storing the handlers, for that, I'm trying to put them inside an HashMap
using the Any
TypeId
for keys, and a Vec<Handler<...>>
value. But I'm having issues with the lifetimes
here is my latest attempt : Rust Playground
The compiler is telling me that I could cast it to the correct type but I couldn't find how. I suppose it means I'll have to do some unsafe downcast later on to be able to call those handlers which is not great but I have the feeling I won't be able to avoid it.
I've read a lot of documentation & related questions here and tried many things. But I'm stuck now.
Can this approach work? If so, what do I need to change? Or should I implement this differently entirely?
For context, once this part is implemented, I'd like to wrap this into its own Handlers
struct, and then implement an EventDispatcher
using it, could be done like this:
async fn main() {
let handlers: Handlers = get_handlers_from_plugins(get_plugins_from_somewhere());
let event_dispatcher = EventDispatcher::new(handlers);
event_dispatcher.dispatch(&SomeEvent {}).await.unwrap();
event_dispatcher.dispatch(&AnotherEvent {}).await.unwrap();
}
thank you!