Switching over to dyn Walkables

This commit is contained in:
Tom Alexander 2020-04-11 21:57:24 -04:00
parent ec3276af79
commit 5098ee6338
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 16 additions and 22 deletions

View File

@ -95,27 +95,25 @@ impl<'a> DustRenderer<'a> {
} }
} }
fn walk_path<'a, C>(context: &'a C, path: &Vec<&str>) -> &'a C // fn walk_path<'a, C>(context: &'a C, path: &Vec<&str>) -> &'a C
where // where
C: Walkable<Output = C>, // C: Walkable<Output = C>,
{ // {
let mut output: &C = context; // let mut output: &C = context;
for elem in path.iter() { // for elem in path.iter() {
output = context.walk(elem); // output = context.walk(elem);
} // }
output // output
} // }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
impl<'a, I: Walkable> Walkable for HashMap<&str, I> { impl<'a, I: Walkable> Walkable for HashMap<&str, I> {
type Output = I; fn walk(&self, segment: &str) -> &dyn Walkable {
fn walk(&self, segment: &str) -> &I {
self.get(segment).unwrap() self.get(segment).unwrap()
} }
@ -125,9 +123,7 @@ mod tests {
} }
impl<'a> Walkable for &str { impl<'a> Walkable for &str {
type Output = u32; fn walk(&self, segment: &str) -> &dyn Walkable {
fn walk(&self, segment: &str) -> &u32 {
panic!("Tried to walk down a str"); panic!("Tried to walk down a str");
} }
@ -137,9 +133,7 @@ mod tests {
} }
impl<'a> Walkable for u32 { impl<'a> Walkable for u32 {
type Output = u32; fn walk(&self, segment: &str) -> &dyn Walkable {
fn walk(&self, segment: &str) -> &u32 {
panic!("Tried to walk down a str"); panic!("Tried to walk down a str");
} }
@ -148,7 +142,7 @@ mod tests {
} }
} }
fn do_the_walk<'a>(context: &'a impl Walkable, path: &Vec<&str>) -> &'a impl Walkable { fn do_the_walk<'a>(context: &'a impl Walkable, path: &Vec<&str>) -> &'a dyn Walkable {
let mut output = context; let mut output = context;
context.walk(path.first().unwrap()) context.walk(path.first().unwrap())

View File

@ -1,9 +1,9 @@
use super::renderable::Renderable; use super::renderable::Renderable;
pub trait Walkable { pub trait Walkable {
type Output: Walkable; // type Output: Walkable;
fn walk(&self, segment: &str) -> &Self::Output; fn walk(&self, segment: &str) -> &dyn Walkable;
fn val(&self) -> String; fn val(&self) -> String;
} }