From c7f267d12d8f6c820ec1288f18338fcee735080b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 14 Jan 2017 10:23:39 -0800 Subject: [PATCH] Fiddling with PartialEq on Value trait --- src/types/mod.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/types/mod.rs b/src/types/mod.rs index 6439233..c44f492 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -2,15 +2,41 @@ * Eryn Wells */ -use std::fmt; +use std::fmt::Debug; +use std::any::Any; pub use self::number::Number; pub mod number; -pub type Boolean = bool; -pub type Character = char; +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Boolean(bool); -pub trait Value: fmt::Debug { } +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Character(char); + +pub trait Value: Debug + 'static { } impl Value for Boolean { } impl Value for Character { } + +impl<'a,'b> PartialEq<&'a Value> for &'b Value { + fn eq(&self, other: &&Value) -> bool { + other.as_any().downcast_ref::().map_or(false, |x| x == self) + } +} + +impl Value { + fn as_any(&self) -> &Any { self } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn booleans_are_equal() { + assert_eq!(Boolean(true), Boolean(true)); + assert_eq!(Boolean(false), Boolean(false)); + assert_ne!(Boolean(true), Boolean(false)); + } +}