Break type checks into separate traits
This commit is contained in:
parent
b094310b3c
commit
370083e2f0
3 changed files with 30 additions and 2 deletions
|
@ -3,17 +3,22 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use super::value::*;
|
use value::*;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub struct Bool(pub bool);
|
pub struct Bool(pub bool);
|
||||||
|
|
||||||
impl Value for Bool {
|
impl Value for Bool {
|
||||||
fn as_value(&self) -> &Value { self }
|
fn as_value(&self) -> &Value { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IsBool for Bool {
|
||||||
fn is_bool(&self) -> bool { true }
|
fn is_bool(&self) -> bool { true }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IsChar for Bool { }
|
||||||
|
impl IsNumber for Bool { }
|
||||||
|
|
||||||
impl ValueEq for Bool {
|
impl ValueEq for Bool {
|
||||||
fn eq(&self, other: &Value) -> bool {
|
fn eq(&self, other: &Value) -> bool {
|
||||||
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self)
|
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self)
|
||||||
|
|
|
@ -10,10 +10,15 @@ pub struct Char(pub char);
|
||||||
|
|
||||||
impl Value for Char {
|
impl Value for Char {
|
||||||
fn as_value(&self) -> &Value { self }
|
fn as_value(&self) -> &Value { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IsChar for Char {
|
||||||
fn is_char(&self) -> bool { true }
|
fn is_char(&self) -> bool { true }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IsBool for Char { }
|
||||||
|
impl IsNumber for Char { }
|
||||||
|
|
||||||
impl ValueEq for Char {
|
impl ValueEq for Char {
|
||||||
fn eq(&self, other: &Value) -> bool {
|
fn eq(&self, other: &Value) -> bool {
|
||||||
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self)
|
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self)
|
||||||
|
|
|
@ -5,15 +5,33 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
pub trait Value: Debug + ValueEq {
|
pub trait Value: Debug + IsBool + IsChar + IsNumber + ValueEq {
|
||||||
fn as_value(&self) -> &Value;
|
fn as_value(&self) -> &Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait IsBool {
|
||||||
/// Should return `true` if this Value is a Bool.
|
/// Should return `true` if this Value is a Bool.
|
||||||
fn is_bool(&self) -> bool { false }
|
fn is_bool(&self) -> bool { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait IsChar {
|
||||||
/// Should return `true` if this Value is a Char.
|
/// Should return `true` if this Value is a Char.
|
||||||
fn is_char(&self) -> bool { false }
|
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
|
/// 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.
|
/// provided by this trait are used by the PartialEq implementation on Values.
|
||||||
pub trait ValueEq {
|
pub trait ValueEq {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue