Maybe have a generic implementation of walkable working

This commit is contained in:
Tom Alexander 2020-04-11 21:03:21 -04:00
parent 7ace4be3c7
commit d296ad6b56
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -112,32 +112,32 @@ where
mod tests { mod tests {
use super::*; use super::*;
impl<'a> Walkable for HashMap<&str, &str> { impl<'a, I: Walkable> Walkable for HashMap<&str, I> {
type Output = str; type Output = I;
fn walk(&self, segment: &str) -> &str { fn walk(&self, segment: &str) -> &I {
self.get(segment).unwrap() self.get(segment).unwrap()
} }
} }
impl<'a> Walkable for str { impl<'a> Walkable for u32 {
type Output = str; type Output = u32;
fn walk(&self, segment: &str) -> &str { fn walk(&self, segment: &str) -> &u32 {
panic!("Tried to walk down a str"); panic!("Tried to walk down a str");
} }
} }
fn do_the_walk<'a>(c: &'a (dyn Walkable<Output = u32> + 'a), path: &str) -> &'a u32 {
c.walk(path)
}
#[test] #[test]
fn test_walk_path() { fn test_walk_path() {
let context: HashMap<&str, &str> = [ let context: HashMap<&str, u32> = [("cat", 1), ("dog", 2), ("tiger", 3)]
("cat", "kitty"), .iter()
("dog", "doggy"), .cloned()
("tiger", "murder kitty"), .collect();
] assert_eq!(do_the_walk(&context, "cat"), &1);
.iter()
.cloned()
.collect();
assert_eq!(context.walk("cat"), "kitty");
} }
} }