Separating out CastToAny.
This commit is contained in:
parent
d5e0c93205
commit
9baa669dea
@ -140,3 +140,9 @@ impl Loopable for serde_json::Value {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CompareContextElement for serde_json::Value {
|
||||
fn equals(&self, other: &dyn ContextElement) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
@ -30,9 +30,11 @@ pub trait Loopable {
|
||||
fn get_loop_elements(&self) -> Vec<&dyn ContextElement>;
|
||||
}
|
||||
|
||||
pub trait CompareContextElement {
|
||||
pub trait CastToAny {
|
||||
fn to_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
pub trait CompareContextElement: CastToAny {
|
||||
fn equals(&self, other: &dyn ContextElement) -> bool;
|
||||
}
|
||||
|
||||
@ -46,19 +48,25 @@ 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 {
|
||||
self
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn ContextElement) -> bool {
|
||||
other
|
||||
.to_any()
|
||||
.downcast_ref::<Self>()
|
||||
.map_or(false, |a| self == a)
|
||||
}
|
||||
}
|
||||
|
||||
// impl<C: 'static + ContextElement + PartialEq> CompareContextElement for C {
|
||||
// fn to_any(&self) -> &dyn Any {
|
||||
// self
|
||||
// }
|
||||
|
||||
// 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 {
|
||||
fn eq(&self, other: &&'b dyn ContextElement) -> bool {
|
||||
self.equals(*other)
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::parser::KVPair;
|
||||
use crate::parser::{Filter, RValue};
|
||||
use crate::renderer::context_element::CastToAny;
|
||||
use crate::renderer::context_element::CloneIntoBoxedContextElement;
|
||||
use crate::renderer::context_element::CompareContextElement;
|
||||
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 {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl CompareContextElement for NewParametersContext {
|
||||
fn equals(&self, other: &dyn ContextElement) -> bool {
|
||||
// TODO: Does this ever happen? perhaps I should have a panic here.
|
||||
false
|
||||
@ -197,3 +200,15 @@ impl Walkable for String {
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user