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 45 46 47 48 49 50 51 52
use std::borrow::Borrow; use std::hash::Hash; pub trait DispatchFunction<P: ?Sized, R>: Fn(&P) -> R + 'static + Sync + Send where P: Send + Sync, { } impl<T, P: ?Sized, R> DispatchFunction<P, R> for T where T: Fn(&P) -> R + 'static + Sync + Send, P: Send + Sync, { } pub trait Dispatcher<K, P: ?Sized, R> where K: Eq + Hash, { fn into_vec(self) -> Vec<(K, Box<dyn DispatchFunction<P, R>>)>; fn remove<Q: ?Sized>(&mut self, key: &Q) where K: Borrow<Q>, Q: Eq + Hash; fn insert(&mut self, key: K, item: Box<dyn DispatchFunction<P, R>>); fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where K: Borrow<Q>, Q: Eq + Hash; fn get<Q: ?Sized>(&self, key: &Q) -> Option<&Box<dyn DispatchFunction<P, R>>> where K: Borrow<Q>, Q: Eq + Hash; fn len(&self) -> usize; fn call<Q: ?Sized>(&self, key: &Q, params: &P) -> Option<R> where K: Borrow<Q>, Q: Eq + Hash, { match self.get(key) { Some(func) => Some(func(params)), None => None, } } }