Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- trait CCmdT {
- fn call(&self, Vec<&str>);
- }
- struct CCmd<T>
- {
- func: Box<Fn(T, Vec<&str>) + 'static>
- }
- impl<T: From<i32>> CCmdT for CCmd<T> {
- fn call(&self, args: Vec<&str>) {
- (self.func)(T::from(8), args);
- }
- }
- struct Console {
- ccmds: HashMap<String, Box<CCmdT>>,
- }
- impl Console {
- pub fn register<T: From<i32> + 'static>(&mut self, name: &str, func: Box<Fn(T, Vec<&str>) + 'static>) {
- let ccmd = CCmd { func: func };
- self.ccmds.insert(String::from(name), Box::new(ccmd));
- }
- pub fn call(&self, name: &str, args: Vec<&str>) {
- if let Some(ccmd) = self.ccmds.get(name) {
- ccmd.call(args);
- }
- }
- }
- fn main() {
- let mut console = Console {ccmds: HashMap::new()};
- console.register("foo", Box::new(|n: i32, args: Vec<&str>| println!("{} {}", n, args[1])));
- console.call("foo", vec!["foo", "bar", "baz"]);
- //register(Box::new(|n: &mut f32| println!("{}", n)));
- }
Add Comment
Please, Sign In to add comment