From ee00bc1108b51b41915b0ba748d4484cf47d6481 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 25 Aug 2025 20:17:53 -0400 Subject: wip: another attempt at extensibility --- src/robo.rs | 64 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) (limited to 'src/robo.rs') 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: 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 Robo { +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, 0)), + speed: Mutex::new(0), } } - pub fn make_ins(&mut self) -> HashMap<&'static str, Box> { + 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); - map.insert("move2", Box::new(op_speed2) as Box); + map.insert(KEY1, Box::new(op_speed) as Box); + map.insert(KEY2, Box::new(op_speed2) as Box); 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(); } } -- cgit v1.3