Simplify types for ration impls
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-02-19 21:23:07 -05:00
parent 3323c2ff23
commit 4f5dcddcc5
3 changed files with 28 additions and 56 deletions

View File

@ -99,7 +99,9 @@ pub trait Int:
}
/// A Trait representing unsigned integer primitives
pub trait Unsigned: Int {
pub trait Unsigned:
Int + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>
{
/// Find the greatest common denominator of two numbers
fn gcd(a: Self, b: Self) -> Self;

View File

@ -144,19 +144,13 @@ impl<T: Unsigned> Frac<T> {
}
}
impl<T> PartialOrd for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> PartialOrd for Frac<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T> Ord for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> Ord for Frac<T> {
fn cmp(&self, other: &Self) -> Ordering {
if self.sign != other.sign {
return if self.sign == Positive {
@ -191,10 +185,7 @@ where
}
}
impl<T> Mul for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> Mul for Frac<T> {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
@ -206,19 +197,13 @@ where
}
}
impl<T> MulAssign for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> MulAssign for Frac<T> {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs
}
}
impl<T> Div for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> Div for Frac<T> {
type Output = Self;
fn div(self, rhs: Self) -> Self {
@ -230,19 +215,13 @@ where
}
}
impl<T> DivAssign for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> DivAssign for Frac<T> {
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs
}
}
impl<T> Add for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> Add for Frac<T> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
@ -276,19 +255,13 @@ where
}
}
impl<T> AddAssign for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> AddAssign for Frac<T> {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl<T> Sub for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> Sub for Frac<T> {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
@ -324,10 +297,7 @@ where
}
}
impl<T> SubAssign for Frac<T>
where
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
{
impl<T: Unsigned> SubAssign for Frac<T> {
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));
}
}

View File

@ -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);
}