Adding &str

This commit is contained in:
Tom Alexander 2020-04-11 21:07:12 -04:00
parent d296ad6b56
commit 63124bc6e3
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 15 additions and 6 deletions

View File

@ -120,6 +120,14 @@ mod tests {
}
}
impl<'a> Walkable for &str {
type Output = u32;
fn walk(&self, segment: &str) -> &u32 {
panic!("Tried to walk down a str");
}
}
impl<'a> Walkable for u32 {
type Output = u32;
@ -128,16 +136,17 @@ mod tests {
}
}
fn do_the_walk<'a>(c: &'a (dyn Walkable<Output = u32> + 'a), path: &str) -> &'a u32 {
fn do_the_walk<'a>(c: &'a (dyn Walkable<Output = &'static str> + 'a), path: &str) -> &'a str {
c.walk(path)
}
#[test]
fn test_walk_path() {
let context: HashMap<&str, u32> = [("cat", 1), ("dog", 2), ("tiger", 3)]
.iter()
.cloned()
.collect();
assert_eq!(do_the_walk(&context, "cat"), &1);
let context: HashMap<&str, &str> =
[("cat", "kitty"), ("dog", "doggy"), ("tiger", "murderkitty")]
.iter()
.cloned()
.collect();
assert_eq!(do_the_walk(&context, "cat"), "kitty");
}
}