Separating out CastToAny.

This commit is contained in:
Tom Alexander 2020-05-10 22:04:41 -04:00
parent d5e0c93205
commit 9baa669dea
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 39 additions and 10 deletions

View File

@ -140,3 +140,9 @@ impl Loopable for serde_json::Value {
} }
} }
} }
impl CompareContextElement for serde_json::Value {
fn equals(&self, other: &dyn ContextElement) -> bool {
false
}
}

View File

@ -30,9 +30,11 @@ pub trait Loopable {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement>; fn get_loop_elements(&self) -> Vec<&dyn ContextElement>;
} }
pub trait CompareContextElement { pub trait CastToAny {
fn to_any(&self) -> &dyn Any; fn to_any(&self) -> &dyn Any;
}
pub trait CompareContextElement: CastToAny {
fn equals(&self, other: &dyn ContextElement) -> bool; fn equals(&self, other: &dyn ContextElement) -> bool;
} }
@ -46,18 +48,24 @@ impl<C: 'static + ContextElement + Clone> CloneIntoBoxedContextElement for C {
} }
} }
impl<C: 'static + ContextElement + PartialEq> CompareContextElement for C { impl<C: 'static + ContextElement + PartialEq> CastToAny for C {
fn to_any(&self) -> &dyn Any { fn to_any(&self) -> &dyn Any {
self self
} }
}
fn equals(&self, other: &dyn ContextElement) -> bool { // impl<C: 'static + ContextElement + PartialEq> CompareContextElement for C {
other // fn to_any(&self) -> &dyn Any {
.to_any() // self
.downcast_ref::<Self>() // }
.map_or(false, |a| self == a)
} // fn equals(&self, other: &dyn ContextElement) -> bool {
} // other
// .to_any()
// .downcast_ref::<Self>()
// .map_or(false, |a| self == a)
// }
// }
impl<'a, 'b> PartialEq<&'b dyn ContextElement> for &'a dyn ContextElement { impl<'a, 'b> PartialEq<&'b dyn ContextElement> for &'a dyn ContextElement {
fn eq(&self, other: &&'b dyn ContextElement) -> bool { fn eq(&self, other: &&'b dyn ContextElement) -> bool {

View File

@ -1,5 +1,6 @@
use crate::parser::KVPair; use crate::parser::KVPair;
use crate::parser::{Filter, RValue}; use crate::parser::{Filter, RValue};
use crate::renderer::context_element::CastToAny;
use crate::renderer::context_element::CloneIntoBoxedContextElement; use crate::renderer::context_element::CloneIntoBoxedContextElement;
use crate::renderer::context_element::CompareContextElement; use crate::renderer::context_element::CompareContextElement;
use crate::renderer::context_element::ContextElement; use crate::renderer::context_element::ContextElement;
@ -111,11 +112,13 @@ impl Clone for NewParametersContext {
} }
} }
impl CompareContextElement for NewParametersContext { impl CastToAny for NewParametersContext {
fn to_any(&self) -> &dyn Any { fn to_any(&self) -> &dyn Any {
self self
} }
}
impl CompareContextElement for NewParametersContext {
fn equals(&self, other: &dyn ContextElement) -> bool { fn equals(&self, other: &dyn ContextElement) -> bool {
// TODO: Does this ever happen? perhaps I should have a panic here. // TODO: Does this ever happen? perhaps I should have a panic here.
false false
@ -197,3 +200,15 @@ impl Walkable for String {
Err(WalkError::CantWalk) Err(WalkError::CantWalk)
} }
} }
impl CompareContextElement for String {
fn equals(&self, other: &dyn ContextElement) -> bool {
// If its a String then compare them directly, otherwise defer
// to the other type's implementation of CompareContextElement
// since the end user could add any type.
match other.to_any().downcast_ref::<Self>() {
None => other.equals(self),
Some(other_string) => self == other_string,
}
}
}