From 4f5dcddcc5e24937bfe5685734526242567acec6 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 19 Feb 2020 21:23:07 -0500 Subject: [PATCH] Simplify types for ration impls --- src/num.rs | 4 +++- src/rational.rs | 56 +++++++++++----------------------------------- tests/fractions.rs | 24 ++++++++++---------- 3 files changed, 28 insertions(+), 56 deletions(-) diff --git a/src/num.rs b/src/num.rs index e9c0674..8cbec0b 100644 --- a/src/num.rs +++ b/src/num.rs @@ -99,7 +99,9 @@ pub trait Int: } /// A Trait representing unsigned integer primitives -pub trait Unsigned: Int { +pub trait Unsigned: + Int + Add + Sub + Mul + Div +{ /// Find the greatest common denominator of two numbers fn gcd(a: Self, b: Self) -> Self; diff --git a/src/rational.rs b/src/rational.rs index 237ba0e..d108427 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -144,19 +144,13 @@ impl Frac { } } -impl PartialOrd for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl PartialOrd for Frac { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl Ord for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl Ord for Frac { fn cmp(&self, other: &Self) -> Ordering { if self.sign != other.sign { return if self.sign == Positive { @@ -191,10 +185,7 @@ where } } -impl Mul for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl Mul for Frac { type Output = Self; fn mul(self, rhs: Self) -> Self { @@ -206,19 +197,13 @@ where } } -impl MulAssign for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl MulAssign for Frac { fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs } } -impl Div for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl Div for Frac { type Output = Self; fn div(self, rhs: Self) -> Self { @@ -230,19 +215,13 @@ where } } -impl DivAssign for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl DivAssign for Frac { fn div_assign(&mut self, rhs: Self) { *self = *self / rhs } } -impl Add for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl Add for Frac { type Output = Self; fn add(self, rhs: Self) -> Self::Output { @@ -276,19 +255,13 @@ where } } -impl AddAssign for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl AddAssign for Frac { fn add_assign(&mut self, rhs: Self) { *self = *self + rhs } } -impl Sub for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl Sub for Frac { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { @@ -324,10 +297,7 @@ where } } -impl SubAssign for Frac -where - T: Unsigned + Add + Sub + Mul + Div, -{ +impl SubAssign for Frac { fn sub_assign(&mut self, rhs: Self) { *self = *self - rhs } @@ -368,8 +338,8 @@ mod tests { assert_eq!(frac!(3 / 2), frac!(1 1/2)); assert_eq!(frac!(3 / 1), frac!(3)); - assert_eq!(-frac!(1/2), frac!(-1/2)); - assert_eq!(-frac!(1/2), frac!(1/-2)); - assert_eq!(frac!(1/2), frac!(-1/-2)); + assert_eq!(-frac!(1 / 2), frac!(-1 / 2)); + assert_eq!(-frac!(1 / 2), frac!(1 / -2)); + assert_eq!(frac!(1 / 2), frac!(-1 / -2)); } } diff --git a/tests/fractions.rs b/tests/fractions.rs index 2ef1ba2..f7eef4f 100644 --- a/tests/fractions.rs +++ b/tests/fractions.rs @@ -35,7 +35,7 @@ fn sub_test() { #[test] fn cmp_test() { - assert!(frac!(1/2) <= frac!(1/2)); + assert!(frac!(1 / 2) <= frac!(1 / 2)); assert!(frac!(0) < frac!(1)); assert!(-frac!(5 / 3) < frac!(1 / 10_000)); assert!(frac!(1 / 10_000) > -frac!(10)); @@ -67,22 +67,22 @@ fn zero_denom() { #[test] fn op_assign() { // Addition - let mut quart = frac!(1/4); - quart += frac!(1/4); - assert_eq!(frac!(1/2), quart); + let mut quart = frac!(1 / 4); + quart += frac!(1 / 4); + assert_eq!(frac!(1 / 2), quart); // Subtraction - let mut half = frac!(1/2); - half -= frac!(1/4); - assert_eq!(frac!(1/4), half); + let mut half = frac!(1 / 2); + half -= frac!(1 / 4); + assert_eq!(frac!(1 / 4), half); // Multiplication - let mut half = frac!(1/2); - half *= frac!(1/2); - assert_eq!(frac!(1/4), half); + let mut half = frac!(1 / 2); + half *= frac!(1 / 2); + assert_eq!(frac!(1 / 4), half); // Division - let mut quart = frac!(1/4); + let mut quart = frac!(1 / 4); quart /= frac!(4); - assert_eq!(frac!(1/16), quart); + assert_eq!(frac!(1 / 16), quart); }