Clean up some Value/Bool/Char code

This commit is contained in:
Eryn Wells 2017-04-08 16:35:11 -07:00
parent 27f1fb63ea
commit c2ac6450ba
4 changed files with 20 additions and 37 deletions

View file

@ -5,15 +5,13 @@
use std::any::Any; use std::any::Any;
use super::value::*; use super::value::*;
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Bool(pub bool); pub struct Bool(pub bool);
impl Bool {
pub fn new(v: bool) -> Bool { Bool(v) }
}
impl Value for Bool { impl Value for Bool {
fn as_value(&self) -> &Value { self } fn as_value(&self) -> &Value { self }
fn is_bool(&self) -> bool { true }
} }
impl ValueEq for Bool { impl ValueEq for Bool {
@ -24,18 +22,10 @@ impl ValueEq for Bool {
fn as_any(&self) -> &Any { self } fn as_any(&self) -> &Any { self }
} }
impl IsBool for Bool {
fn is_bool(&self) -> bool { true }
}
impl IsChar for Bool {
fn is_char(&self) -> bool { false }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Bool; use super::Bool;
use types::value::{IsBool, IsChar, Value}; use value::*;
#[test] #[test]
fn equal_bools_are_equal() { fn equal_bools_are_equal() {
@ -50,6 +40,7 @@ mod tests {
#[test] #[test]
fn bools_are_bools() { fn bools_are_bools() {
assert_eq!(Bool(false).is_bool(), true); assert_eq!(Bool(false).is_bool(), true);
assert_eq!(Bool(false).is_char(), false);
} }
#[test] #[test]

View file

@ -5,15 +5,13 @@
use std::any::Any; use std::any::Any;
use super::value::*; use super::value::*;
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Char(pub char); pub struct Char(pub char);
impl Char {
pub fn new(v: char) -> Char { Char(v) }
}
impl Value for Char { impl Value for Char {
fn as_value(&self) -> &Value { self } fn as_value(&self) -> &Value { self }
fn is_char(&self) -> bool { true }
} }
impl ValueEq for Char { impl ValueEq for Char {
@ -24,18 +22,10 @@ impl ValueEq for Char {
fn as_any(&self) -> &Any { self } fn as_any(&self) -> &Any { self }
} }
impl IsBool for Char {
fn is_bool(&self) -> bool { false }
}
impl IsChar for Char {
fn is_char(&self) -> bool { true }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Char; use super::Char;
use types::value::{IsBool, IsChar, Value}; use value::*;
#[test] #[test]
fn equal_chars_are_equal() { fn equal_chars_are_equal() {
@ -47,6 +37,7 @@ mod tests {
#[test] #[test]
fn chars_are_chars() { fn chars_are_chars() {
assert_eq!(Char('a').is_char(), true); assert_eq!(Char('a').is_char(), true);
assert_eq!(Char('a').is_bool(), false);
} }
#[test] #[test]

View file

@ -1,3 +1,7 @@
mod bool;
mod char;
mod value;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]

View file

@ -5,8 +5,13 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::any::Any; use std::any::Any;
pub trait Value: Debug + ValueEq + IsBool + IsChar { pub trait Value: Debug + ValueEq {
fn as_value(&self) -> &Value; fn as_value(&self) -> &Value;
/// Should return `true` if this Value is a Bool.
fn is_bool(&self) -> bool { false }
/// Should return `true` if this Value is a Char.
fn is_char(&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
@ -21,11 +26,3 @@ impl<'lhs,'rhs> PartialEq<Value+'rhs> for Value+'lhs {
ValueEq::eq(self, other) ValueEq::eq(self, other)
} }
} }
pub trait IsBool {
fn is_bool(&self) -> bool { false }
}
pub trait IsChar {
fn is_char(&self) -> bool { false }
}