summaryrefslogtreecommitdiffstats
path: root/src/robo.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/robo.rs')
-rw-r--r--src/robo.rs64
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();
}
}