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
53
54
55
56
57
58
59
60
61
62
63
64
65
//! dispatchtable is a runtime key based collection that contains references to functions. 
//!
//! # Quick Start
//! ```
//! use dispatchtable::{Dispatch, DispatchTable};
//! 
//! fn add(p: &(u8, u8)) -> u8 { p.0 + p.1 }
//! fn sub(p: &(u8, u8)) -> u8 { p.0 - p.1 }
//! 
//! let dispatchtable = DispatchTable::new();
//! 
//! dispatchtable.insert("add", add)
//! dispatchtable.insert("sub", sub)
//! 
//! assert_eq!(dispatchtable.call("sub", &(10, 5)), Some(5))
//! ```



#[cfg(test)]
mod test;
mod core;

use std::collections::HashMap;
pub use crate::core::{DispatchFunction, Dispatcher as Dispatch};

pub struct DispatchTable<K, P, R> {
    inner: HashMap<K, Box<dyn DispatchFunction<P, R>>>
}

impl<K, P, R> DispatchTable<K, P, R> {
    pub fn new() -> Self {
        Self {
            inner: HashMap::new()
        }
    }
}

impl<K, P, R> Dispatch<K, P, R> for DispatchTable<K, P, R>
where
    K: std::hash::Hash + std::cmp::Eq
{   
    fn len(&self) -> usize {
        self.inner.len()
    }

    fn contains_key(&self, key: &K) -> bool {
        self.inner.contains_key(key)
    }

    fn remove(&mut self, key: &K) {
        self.inner.remove(key);
    }
    fn insert(&mut self, key: K, item: Box<dyn DispatchFunction<P, R>>) {
        self.inner.insert(key, item);
    }

    fn get(&self, key: &K) -> Option<&Box<dyn DispatchFunction<P, R>>> {
        self.inner.get(key)
    }

    fn into_vec(self) -> Vec<(K, Box<dyn DispatchFunction<P, R>> )> {
        self.inner.into_iter().collect()
    }
}