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
1 changed files with 15 additions and 15 deletions

View File

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