From 5c1c453482f697156ee3ae9b97e4cc252d3f30c2 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Sun, 5 Jan 2025 09:14:29 -0500 Subject: [PATCH] refactor: streamline arithmetic traits with macros (#95) Consolidated repetitive implementations of `Add`, `Sub`, `Mul`, and `Div` into reusable macros for `FractionLike`, reducing code duplication and improving maintainability. This change ensures uniform functionality across operations and simplifies future updates. --- src/entities/fractions/fraction.rs | 186 ++++++++++------------------- 1 file changed, 60 insertions(+), 126 deletions(-) diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index 9e8f381..10fddc6 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -225,141 +225,75 @@ impl PartialOrd for FractionLike { } } -impl Add for FractionLike { - type Output = Self; - - #[inline] - fn add(self, other: Self) -> Self::Output { - if self.denominator == other.denominator { - FractionBase::new( - self.numerator + other.numerator, - self.denominator, - self.meta, - ) - } else { - FractionBase::new( - self.numerator * other.denominator + other.numerator * self.denominator, - self.denominator * other.denominator, - self.meta, - ) +macro_rules! impl_add_sub { + ($trait:ident, $method:ident, $op:tt, $Rhs:ty) => { + impl $trait<$Rhs> for FractionLike { + type Output = Self; + + #[inline] + fn $method(self, other: $Rhs) -> Self::Output { + if self.denominator == other.denominator { + FractionBase::new( + self.numerator $op other.numerator, + self.denominator, + self.meta, + ) + } else { + FractionBase::new( + self.numerator * other.denominator $op other.numerator * self.denominator, + self.denominator * other.denominator, + self.meta, + ) + } + } } - } + }; } -impl Add<&Self> for FractionLike { - type Output = Self; - - #[inline] - fn add(self, other: &Self) -> Self::Output { - if self.denominator == other.denominator { - FractionBase::new( - self.numerator + other.numerator, - self.denominator, - self.meta, - ) - } else { - FractionBase::new( - self.numerator * other.denominator + other.numerator * self.denominator, - self.denominator * other.denominator, - self.meta, - ) +impl_add_sub!(Add, add, +, Self); +impl_add_sub!(Add, add, +, &Self); +impl_add_sub!(Sub, sub, -, Self); +impl_add_sub!(Sub, sub, -, &Self); + +macro_rules! impl_mul { + ($trait:ident, $method:ident, $Rhs:ty) => { + impl $trait<$Rhs> for FractionLike { + type Output = Self; + + #[inline] + fn $method(self, other: $Rhs) -> Self::Output { + FractionBase::new( + self.numerator * other.numerator, + self.denominator * other.denominator, + self.meta, + ) + } } - } + }; } -impl Sub for FractionLike { - type Output = Self; - - #[inline] - fn sub(self, other: Self) -> Self::Output { - if self.denominator == other.denominator { - FractionBase::new( - self.numerator - other.numerator, - self.denominator, - self.meta, - ) - } else { - FractionBase::new( - self.numerator * other.denominator - other.numerator * self.denominator, - self.denominator * other.denominator, - self.meta, - ) +impl_mul!(Mul, mul, Self); +impl_mul!(Mul, mul, &Self); + +macro_rules! impl_div { + ($trait:ident, $method:ident, $Rhs:ty) => { + impl $trait<$Rhs> for FractionLike { + type Output = Self; + + #[inline] + fn $method(self, other: $Rhs) -> Self::Output { + FractionBase::new( + self.numerator * other.denominator, + self.denominator * other.numerator, + self.meta, + ) + } } - } -} - -impl Sub<&Self> for FractionLike { - type Output = Self; - - #[inline] - fn sub(self, other: &Self) -> Self::Output { - if self.denominator == other.denominator { - FractionBase::new( - self.numerator - other.numerator, - self.denominator, - self.meta, - ) - } else { - FractionBase::new( - self.numerator * other.denominator - other.numerator * self.denominator, - self.denominator * other.denominator, - self.meta, - ) - } - } -} - -impl Mul for FractionLike { - type Output = Self; - - #[inline] - fn mul(self, other: Self) -> Self::Output { - FractionBase::new( - self.numerator * other.numerator, - self.denominator * other.denominator, - self.meta, - ) - } -} - -impl Mul<&Self> for FractionLike { - type Output = Self; - - #[inline] - fn mul(self, other: &Self) -> Self::Output { - FractionBase::new( - self.numerator * other.numerator, - self.denominator * other.denominator, - self.meta, - ) - } + }; } -impl Div for FractionLike { - type Output = Self; - - #[inline] - fn div(self, other: Self) -> Self::Output { - FractionBase::new( - self.numerator * other.denominator, - self.denominator * other.numerator, - self.meta, - ) - } -} - -impl Div<&Self> for FractionLike { - type Output = Self; - - #[inline] - fn div(self, other: &Self) -> Self::Output { - FractionBase::new( - self.numerator * other.denominator, - self.denominator * other.numerator, - self.meta, - ) - } -} +impl_div!(Div, div, Self); +impl_div!(Div, div, &Self); #[cfg(test)] mod tests {