Wholesale move modules from src/types to types/src

This commit is contained in:
Eryn Wells 2017-04-08 16:25:02 -07:00
parent 0e6231bd94
commit 27f1fb63ea
5 changed files with 0 additions and 0 deletions

59
types/src/bool.rs Normal file
View file

@ -0,0 +1,59 @@
/* types/bool.rs
* Eryn Wells <eryn@erynwells.me>
*/
use std::any::Any;
use super::value::*;
#[derive(Clone, Copy, Debug, 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 }
}
impl ValueEq for Bool {
fn eq(&self, other: &Value) -> bool {
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == 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)]
mod tests {
use super::Bool;
use types::value::{IsBool, IsChar, Value};
#[test]
fn equal_bools_are_equal() {
assert_eq!(Bool(true), Bool(true));
assert_eq!(Bool(false), Bool(false));
assert_ne!(Bool(true), Bool(false));
assert_eq!(Bool(true).as_value(), Bool(true).as_value());
assert_ne!(Bool(true).as_value(), Bool(false).as_value());
}
#[test]
fn bools_are_bools() {
assert_eq!(Bool(false).is_bool(), true);
}
#[test]
fn bools_are_not_chars() {
assert_eq!(Bool(false).is_char(), false);
}
}

56
types/src/char.rs Normal file
View file

@ -0,0 +1,56 @@
/* types/char.rs
* Eryn Wells <eryn@erynwells.me>
*/
use std::any::Any;
use super::value::*;
#[derive(Clone, Copy, Debug, 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 }
}
impl ValueEq for Char {
fn eq(&self, other: &Value) -> bool {
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == 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)]
mod tests {
use super::Char;
use types::value::{IsBool, IsChar, Value};
#[test]
fn equal_chars_are_equal() {
assert_eq!(Char('a'), Char('a'));
assert_eq!(Char('a').as_value(), Char('a').as_value());
assert_ne!(Char('a').as_value(), Char('b').as_value());
}
#[test]
fn chars_are_chars() {
assert_eq!(Char('a').is_char(), true);
}
#[test]
fn chars_are_not_bools() {
assert_eq!(Char('a').is_bool(), false);
}
}

23
types/src/mod.rs Normal file
View file

@ -0,0 +1,23 @@
/* types/mod.rs
* Eryn Wells <eryn@erynwells.me>
*/
pub mod bool;
pub mod char;
pub mod number;
pub mod value;
pub use self::bool::Bool;
pub use self::char::Char;
pub use self::number::Number;
pub use self::value::Value;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn booleans_and_chars_are_not_equal() {
assert_ne!(Bool::new(true).as_value(), Char::new('a').as_value());
}
}

60
types/src/number.rs Normal file
View file

@ -0,0 +1,60 @@
/* number.rs
* Eryn Wells <eryn@erynwells.me>
*/
/// # Numbers
///
/// Scheme numbers are complex, literally.
#[derive(PartialEq, Debug)]
pub struct Number {
pub value: f64
}
impl Number {
pub fn from_int(v: i64) -> Number {
Number { value: v as f64 }
}
pub fn from_float(v: f64) -> Number {
Number { value: v }
}
}
/*
pub trait Number {
fn new() -> Number;
fn from_int(v: i64);
fn from_float(v: f64);
}
pub trait Exact {
fn exact() -> bool;
}
type Integer = i64;
impl Exact for Integer {
fn exact() -> bool { true }
}
#[derive(PartialEq, Debug)]
pub struct Rational { numer: i64, denom: i64 }
impl Exact for Rational {
fn exact() -> bool { true }
}
type Real = f64;
impl Exact for Real {
fn exact() -> bool { false }
}
#[derive(PartialEq, Debug)]
pub struct Complex { real: f64, imag: f64 }
impl Exact for Complex {
fn exact() -> bool { false }
}
*/

31
types/src/value.rs Normal file
View file

@ -0,0 +1,31 @@
/* types/value.rs
* Eryn Wells <eryn@erynwells.me>
*/
use std::fmt::Debug;
use std::any::Any;
pub trait Value: Debug + ValueEq + IsBool + IsChar {
fn as_value(&self) -> &Value;
}
/// 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 {
fn eq(&self, other: &Value) -> bool;
fn as_any(&self) -> &Any;
}
impl<'lhs,'rhs> PartialEq<Value+'rhs> for Value+'lhs {
fn eq(&self, other: &(Value+'rhs)) -> bool {
ValueEq::eq(self, other)
}
}
pub trait IsBool {
fn is_bool(&self) -> bool { false }
}
pub trait IsChar {
fn is_char(&self) -> bool { false }
}