I'm trying to find a way to allow users to plug in custom coding (closures) when certain events are processed.
One possible solution is to implement an event dispatcher. Each event type has a corresponding handler function with an own signatures (input and return type).
I'm unsure how to store handler functions because their signatures differ. It would be great if Rust's type system can make sure that only certain input/return type pairs are allowed (InputType1+ReturnType1 and InputType2+ReturnType2 in the example).
Is there maybe a better approach to tackle this problem? The handler doesn't need to be a closure, a struct implementing a trait is also fine.
It's not feasible for the interface to be register(some_closure) because the type system, in principle (though not in practice on current stable), allows a single type to have more than one Fn signature. So, if you want that implicit event-type selection, it will have to be via a custom trait (with associated types for input and output).
Then, for the storage, you can make fn register itself a method of another trait, which is implemented once for each supported event type, and each implementation knows which (appropriately typed) list of handlers to store the handler in. The type of that list will be like Vec<dyn Handler<Input = InputType1, Return = ReturnType1>>.
You could probably hack something together using poly_fn in frunk - Rust, but I wouldn't really recommend it. A better solution might be an enum of all events with their input and output types.
@David-Kunz Or using HList, if you want to have to prove at compile-time that a certain type of function exists: Rust Explorer (Please don't use it, very overengineered and POC, but it was fun)