Separating out CastToAny.

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

View File

@@ -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)