From 2b2d1029f30206c81c16b92cde592e303a025494 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 14 Apr 2017 07:47:39 -0700 Subject: [PATCH] Make Number its own struct with real/imag elements --- types/src/number/mod.rs | 46 ++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/types/src/number/mod.rs b/types/src/number/mod.rs index b768631..05ea25a 100644 --- a/types/src/number/mod.rs +++ b/types/src/number/mod.rs @@ -23,29 +23,41 @@ use super::value::*; type Int = i64; type Flt = f64; -trait Number: Debug + IsBool + IsChar + IsNumber + Value { } - -trait IsExact { - /// Should return `true` if this Number is represented exactly. This should be an inverse of - /// `is_inexact()`. - fn is_exact(&self) -> bool { false } - - /// Should return `true` if this Number is not represented exactly. This should be an inverse - /// of `is_exact()`. - fn is_inexact(&self) -> bool { !self.is_exact() } +// TODO: Implement PartialEq myself cause there are some weird nuances to comparing numbers. +#[derive(Debug, PartialEq)] +pub struct Number { + real: Real, + imag: Option, + exact: bool, } -impl Value for Box { - fn as_value(&self) -> &Value { self.deref().as_value() } +impl Number { + fn new(real: Real, imag: Option, exact: bool) -> Number { + Number { + real: real.reduce(), + imag: imag.map(|n| n.reduce()), + exact: exact, + } + } + + pub fn from_int(value: Int, exact: bool) -> Number { + Number::new(Real::Integer(value), None, exact) + } + + pub fn is_exact(&self) -> bool { self.exact } } -impl IsBool for Box { } -impl IsChar for Box { } -impl IsNumber for Box { } +impl Value for Number { + fn as_value(&self) -> &Value { self } +} -impl ValueEq for Box { +impl ValueEq for Number { fn eq(&self, other: &Value) -> bool { - self.deref().eq(other) + other.as_any().downcast_ref::().map_or(false, |x| x == self) } fn as_any(&self) -> &Any { self } } + +impl IsBool for Number { } +impl IsChar for Number { } +impl IsNumber for Number { }