diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-25 20:17:53 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-25 20:18:27 -0400 |
| commit | ee00bc1108b51b41915b0ba748d4484cf47d6481 (patch) | |
| tree | 8c81e71d6811f559dce798733d87c71b1399e1ea /src | |
| parent | f31de5a8dfdcdfc60048240daa9ab9c152afb806 (diff) | |
| download | automathon-ee00bc1108b51b41915b0ba748d4484cf47d6481.tar.gz automathon-ee00bc1108b51b41915b0ba748d4484cf47d6481.zip | |
wip: another attempt at extensibility
Diffstat (limited to 'src')
| -rw-r--r-- | src/robo.rs | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/src/robo.rs b/src/robo.rs index 5da2e18..f508879 100644 --- a/src/robo.rs +++ b/src/robo.rs @@ -1,35 +1,55 @@ use std::collections::HashMap; use std::sync::Mutex; -// vector describing speed in x,y dimensions -pub type Speed = (usize, usize); - pub struct Robo { - speed: Mutex<Speed>, + speed: Mutex<usize>, +} + +//type InsKey = usize; +type InsKey = &'static str; +type InsMap<'a> = HashMap<InsKey, Box<dyn FnMut() + 'a>>; +// 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 Robo { +impl<'a> Robo { pub fn new() -> Self { Self { - speed: Mutex::new((0, 0)), + speed: Mutex::new(0), } } - pub fn make_ins(&mut self) -> HashMap<&'static str, Box<dyn FnMut() + '_>> { + 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, 1); + *x = 1; }; let op_speed2 = || { let mut x = s.lock().expect("couldn't get lock on speed"); - *x = (2, 2); + *x = 2; }; - map.insert("move", Box::new(op_speed) as Box<dyn FnMut()>); - map.insert("move2", Box::new(op_speed2) as Box<dyn FnMut()>); + map.insert(KEY1, Box::new(op_speed) as Box<dyn FnMut()>); + map.insert(KEY2, Box::new(op_speed2) as Box<dyn FnMut()>); map } } @@ -39,24 +59,10 @@ mod tests { use super::*; #[test] - fn can_run_two() { + fn can_run() { let mut r = Robo::new(); - { - let hm = &mut r.make_ins(); - let fun = hm.get_mut("move").expect("move should exist"); - fun(); - } - let s = r.speed.lock().expect("couldn't get lock on speed"); - assert_eq!(*s, (1, 1)); - drop(s); - - { - let hm = &mut r.make_ins(); - let fun = hm.get_mut("move2").expect("move2 should exist"); - fun(); - } - let s = r.speed.lock().expect("couldn't get lock on speed"); - assert_eq!(*s, (2, 2)); - drop(s); + let ins: InsMap = r.make_ins(); + let mut c: Co = Co::new(ins); + c.run(); } } |
