Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::hash::Hash;
- trait Listener<T,C> {
- fn handle(&mut self, evt: T, ctx: &mut C);
- }
- struct Dispatcher<T,C> where T: Hash + Eq {
- events: HashMap<T, Vec<Box<dyn Listener<T,C>>>>,
- }
- impl<T,C> Default for Dispatcher<T,C> where T: Hash + Eq {
- fn default() -> Self {
- Self{events: HashMap::default()}
- }
- }
- impl<T,C> Dispatcher<T,C> where T: Hash + Eq {
- fn dispatch(&mut self, _event: T, context: &mut C) {
- }
- }
- #[derive(Hash, PartialEq, Eq)]
- enum Event {
- A,B,C,
- }
- struct Context {
- some: bool,
- }
- fn main() {
- let mut dispatcher = Dispatcher::default();
- let mut ctx = Context{some: true};
- let event = Event::A;
- dispatcher.dispatch(event, &mut ctx);
- let event = Event::B;
- dispatcher.dispatch(event, &mut ctx);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement