use std::collections::HashMap; use std::sync::Mutex; pub struct Robo { speed: Mutex, } //type InsKey = usize; type InsKey = &'static str; type InsMap<'a> = HashMap>; // const KEY1: InsKey = 0; // const KEY2: InsKey = 0; const KEY1: InsKey = "mv1"; const KEY2: InsKey = "mv2"; pub struct Co<'a> { ins: InsMap<'a>, } impl<'a> Co<'a> { pub fn new(ins: InsMap<'a>) -> Self { Self { ins } } pub fn run(&mut self) { (self.ins.get_mut(&KEY1).expect("should have move word"))(); (self.ins.get_mut(&KEY2).expect("should have move2 word"))(); } } impl<'a> Robo { pub fn new() -> Self { Self { speed: Mutex::new(0), } } pub fn make_ins(&'a mut self) -> InsMap<'a> { let mut map = HashMap::new(); let s = &mut self.speed; let op_speed = || { let mut x = s.lock().expect("couldn't get lock on speed"); *x = 1; }; let op_speed2 = || { let mut x = s.lock().expect("couldn't get lock on speed"); *x = 2; }; map.insert(KEY1, Box::new(op_speed) as Box); map.insert(KEY2, Box::new(op_speed2) as Box); map } } #[cfg(test)] mod tests { use super::*; #[test] fn can_run() { let mut r = Robo::new(); let ins: InsMap = r.make_ins(); let mut c: Co = Co::new(ins); c.run(); } }