[types] Implement Add and Mul on Int
This commit is contained in:
parent
1dfc6823f0
commit
0ed4aa3ae5
3 changed files with 47 additions and 3 deletions
|
@ -9,4 +9,5 @@ pub use object::Obj;
|
||||||
pub use pair::Pair;
|
pub use pair::Pair;
|
||||||
pub use sym::Sym;
|
pub use sym::Sym;
|
||||||
|
|
||||||
|
pub use self::number::Number;
|
||||||
pub use self::number::Int;
|
pub use self::number::Int;
|
||||||
|
|
|
@ -4,11 +4,33 @@
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::ops::{Add, Mul};
|
||||||
use number::Number;
|
use number::Number;
|
||||||
use object::{Obj, Object};
|
use object::{Obj, Object};
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Int(i64);
|
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<Int> 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 {
|
impl fmt::Display for Int {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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) }
|
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<Int> 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<Obj> for Int {
|
impl PartialEq<Obj> for Int {
|
||||||
fn eq<'a>(&self, rhs: &'a Obj) -> bool {
|
fn eq<'a>(&self, rhs: &'a Obj) -> bool {
|
||||||
match rhs.obj().and_then(Object::as_num) {
|
match rhs.obj().and_then(Object::as_num) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue