From 110b03fc45ec357ec9798c2299f70c6a27359c90 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 11 Apr 2020 21:44:42 -0400 Subject: [PATCH] Going to try returning impl trait before resorting to boxing --- src/renderer/renderer.rs | 26 +++++++++++++++++++------- src/renderer/walkable.rs | 2 ++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 446a95d..87f3101 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -118,6 +118,10 @@ mod tests { fn walk(&self, segment: &str) -> &I { self.get(segment).unwrap() } + + fn val(&self) -> String { + "val".to_owned() + } } impl<'a> Walkable for &str { @@ -126,6 +130,10 @@ mod tests { fn walk(&self, segment: &str) -> &u32 { panic!("Tried to walk down a str"); } + + fn val(&self) -> String { + "val".to_owned() + } } impl<'a> Walkable for u32 { @@ -134,12 +142,16 @@ mod tests { fn walk(&self, segment: &str) -> &u32 { panic!("Tried to walk down a str"); } + + fn val(&self) -> String { + "val".to_owned() + } } fn do_the_walk<'a, O: Walkable>( context: &'a dyn Walkable, path: &Vec<&str>, - ) -> &'a O { + ) -> &'a impl Walkable { let mut output = context; context.walk(path.first().unwrap()) @@ -170,11 +182,11 @@ mod tests { .iter() .cloned() .collect(); - assert_eq!(do_the_walk(&context, &vec!["cat"]), &"kitty"); - assert_eq!(do_the_walk(&number_context, &vec!["tiger"]), &3); - assert_eq!( - do_the_walk(&deep_context, &vec!["tiger"]), - &[("food", "people")].iter().cloned().collect() - ); + assert_eq!(do_the_walk(&context, &vec!["cat"]).val(), "kitty"); + // assert_eq!(do_the_walk(&number_context, &vec!["tiger"]), &3); + // assert_eq!( + // do_the_walk(&deep_context, &vec!["tiger"]), + // &[("food", "people")].iter().cloned().collect() + // ); } } diff --git a/src/renderer/walkable.rs b/src/renderer/walkable.rs index 1c2fdc9..99c31de 100644 --- a/src/renderer/walkable.rs +++ b/src/renderer/walkable.rs @@ -4,4 +4,6 @@ pub trait Walkable { type Output: ?Sized + Walkable; fn walk(&self, segment: &str) -> &Self::Output; + + fn val(&self) -> String; }