Clean up some Value/Bool/Char code
This commit is contained in:
		
							parent
							
								
									27f1fb63ea
								
							
						
					
					
						commit
						c2ac6450ba
					
				
					 4 changed files with 20 additions and 37 deletions
				
			
		| 
						 | 
				
			
			@ -5,15 +5,13 @@
 | 
			
		|||
use std::any::Any;
 | 
			
		||||
use super::value::*;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Copy, Debug, PartialEq)]
 | 
			
		||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
 | 
			
		||||
pub struct Bool(pub bool);
 | 
			
		||||
 | 
			
		||||
impl Bool {
 | 
			
		||||
    pub fn new(v: bool) -> Bool { Bool(v) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Value for Bool {
 | 
			
		||||
    fn as_value(&self) -> &Value { self }
 | 
			
		||||
 | 
			
		||||
    fn is_bool(&self) -> bool { true }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ValueEq for Bool {
 | 
			
		||||
| 
						 | 
				
			
			@ -24,18 +22,10 @@ impl ValueEq for Bool {
 | 
			
		|||
    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)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::Bool;
 | 
			
		||||
    use types::value::{IsBool, IsChar, Value};
 | 
			
		||||
    use value::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn equal_bools_are_equal() {
 | 
			
		||||
| 
						 | 
				
			
			@ -50,6 +40,7 @@ mod tests {
 | 
			
		|||
    #[test]
 | 
			
		||||
    fn bools_are_bools() {
 | 
			
		||||
        assert_eq!(Bool(false).is_bool(), true);
 | 
			
		||||
        assert_eq!(Bool(false).is_char(), false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,15 +5,13 @@
 | 
			
		|||
use std::any::Any;
 | 
			
		||||
use super::value::*;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Copy, Debug, PartialEq)]
 | 
			
		||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
 | 
			
		||||
pub struct Char(pub char);
 | 
			
		||||
 | 
			
		||||
impl Char {
 | 
			
		||||
    pub fn new(v: char) -> Char { Char(v) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Value for Char {
 | 
			
		||||
    fn as_value(&self) -> &Value { self }
 | 
			
		||||
 | 
			
		||||
    fn is_char(&self) -> bool { true }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ValueEq for Char {
 | 
			
		||||
| 
						 | 
				
			
			@ -24,18 +22,10 @@ impl ValueEq for Char {
 | 
			
		|||
    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)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::Char;
 | 
			
		||||
    use types::value::{IsBool, IsChar, Value};
 | 
			
		||||
    use value::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn equal_chars_are_equal() {
 | 
			
		||||
| 
						 | 
				
			
			@ -47,6 +37,7 @@ mod tests {
 | 
			
		|||
    #[test]
 | 
			
		||||
    fn chars_are_chars() {
 | 
			
		||||
        assert_eq!(Char('a').is_char(), true);
 | 
			
		||||
        assert_eq!(Char('a').is_bool(), false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
mod bool;
 | 
			
		||||
mod char;
 | 
			
		||||
mod value;
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    #[test]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,8 +5,13 @@
 | 
			
		|||
use std::fmt::Debug;
 | 
			
		||||
use std::any::Any;
 | 
			
		||||
 | 
			
		||||
pub trait Value: Debug + ValueEq + IsBool + IsChar {
 | 
			
		||||
pub trait Value: Debug + ValueEq {
 | 
			
		||||
    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
 | 
			
		||||
| 
						 | 
				
			
			@ -21,11 +26,3 @@ impl<'lhs,'rhs> PartialEq<Value+'rhs> for Value+'lhs {
 | 
			
		|||
        ValueEq::eq(self, other)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait IsBool {
 | 
			
		||||
    fn is_bool(&self) -> bool { false }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait IsChar {
 | 
			
		||||
    fn is_char(&self) -> bool { false }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue