diff --git a/types/src/lib.rs b/types/src/lib.rs index 7632128..c077ee5 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -9,4 +9,5 @@ pub use object::Obj; pub use pair::Pair; pub use sym::Sym; +pub use self::number::Number; pub use self::number::Int; diff --git a/types/src/number/integer.rs b/types/src/number/integer.rs index f842c6b..169e2a7 100644 --- a/types/src/number/integer.rs +++ b/types/src/number/integer.rs @@ -4,11 +4,33 @@ use std::any::Any; use std::fmt; +use std::ops::{Add, Mul}; use number::Number; use object::{Obj, Object}; -#[derive(Debug, Eq, PartialEq)] -pub struct Int(i64); +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct Int(pub i64); + +impl Add for Int { + type Output = Int; + fn add(self, rhs: Self) -> Self::Output { + Int(self.0 + rhs.0) + } +} + +impl<'a> Add for &'a Int { + type Output = Int; + fn add(self, rhs: Int) -> Self::Output { + Int(self.0 + rhs.0) + } +} + +impl<'a, 'b> Add<&'a Int> for &'b Int { + type Output = Int; + fn add(self, rhs: &Int) -> Self::Output { + Int(self.0 + rhs.0) + } +} impl fmt::Display for Int { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -25,6 +47,27 @@ impl Number for Int { fn as_int(&self) -> Option<&Int> { Some(self) } } +impl Mul for Int { + type Output = Int; + fn mul(self, rhs: Self) -> Self::Output { + Int(self.0 * rhs.0) + } +} + +impl<'a> Mul for &'a Int { + type Output = Int; + fn mul(self, rhs: Int) -> Self::Output { + Int(self.0 * rhs.0) + } +} + +impl<'a, 'b> Mul<&'a Int> for &'b Int { + type Output = Int; + fn mul(self, rhs: &Int) -> Self::Output { + Int(self.0 * rhs.0) + } +} + impl PartialEq for Int { fn eq<'a>(&self, rhs: &'a Obj) -> bool { match rhs.obj().and_then(Object::as_num) { diff --git a/types/src/number/mod.rs b/types/src/number/mod.rs index 65fac0a..e63aa46 100644 --- a/types/src/number/mod.rs +++ b/types/src/number/mod.rs @@ -18,7 +18,7 @@ use object::Object; pub use self::integer::Int; pub trait Number: - Object + Object { /// Cast this Number to an Int if possible. fn as_int(&self) -> Option<&Int> { None }