diff --git a/types/src/bool.rs b/types/src/bool.rs index 93ac0ab..888fcd0 100644 --- a/types/src/bool.rs +++ b/types/src/bool.rs @@ -3,17 +3,22 @@ */ use std::any::Any; -use super::value::*; +use value::*; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct Bool(pub bool); impl Value for Bool { fn as_value(&self) -> &Value { self } +} +impl IsBool for Bool { fn is_bool(&self) -> bool { true } } +impl IsChar for Bool { } +impl IsNumber for Bool { } + impl ValueEq for Bool { fn eq(&self, other: &Value) -> bool { other.as_any().downcast_ref::().map_or(false, |x| x == self) diff --git a/types/src/char.rs b/types/src/char.rs index 721c07f..ed39abc 100644 --- a/types/src/char.rs +++ b/types/src/char.rs @@ -10,10 +10,15 @@ pub struct Char(pub char); impl Value for Char { fn as_value(&self) -> &Value { self } +} +impl IsChar for Char { fn is_char(&self) -> bool { true } } +impl IsBool for Char { } +impl IsNumber for Char { } + impl ValueEq for Char { fn eq(&self, other: &Value) -> bool { other.as_any().downcast_ref::().map_or(false, |x| x == self) diff --git a/types/src/value.rs b/types/src/value.rs index 1989b17..e40827e 100644 --- a/types/src/value.rs +++ b/types/src/value.rs @@ -5,15 +5,33 @@ use std::fmt::Debug; use std::any::Any; -pub trait Value: Debug + ValueEq { +pub trait Value: Debug + IsBool + IsChar + IsNumber + ValueEq { fn as_value(&self) -> &Value; +} +pub trait IsBool { /// Should return `true` if this Value is a Bool. fn is_bool(&self) -> bool { false } +} + +pub trait IsChar { /// Should return `true` if this Value is a Char. fn is_char(&self) -> bool { false } } +pub trait IsNumber { + /// Should return `true` if this Value is a number type. + fn is_number(&self) -> bool { self.is_complex() || self.is_real() || self.is_rational() || self.is_integer() } + /// Should return `true` if this Value is a complex number type. + fn is_complex(&self) -> bool { false } + /// Should return `true` if this Value is a real number type. + fn is_real(&self) -> bool { false } + /// Should return `true` if this Value is a rational number type. + fn is_rational(&self) -> bool { false } + /// Should return `true` if this Value is a integer number type. + fn is_integer(&self) -> bool { false } +} + /// A trait on value types that makes it easier to compare values of disparate types. The methods /// provided by this trait are used by the PartialEq implementation on Values. pub trait ValueEq {