From 60619d3e0db86a0accea5629cfb040925a8829f5 Mon Sep 17 00:00:00 2001 From: Raimundo Saona <37874270+saona-raimundo@users.noreply.github.com> Date: Sun, 14 Apr 2024 23:35:21 +0200 Subject: [PATCH 1/2] Add ConstOne and ConstZero implementation --- src/fraction/generic_fraction.rs | 10 +++++++++- src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/fraction/generic_fraction.rs b/src/fraction/generic_fraction.rs index bb255f8d..a6ca4ed4 100644 --- a/src/fraction/generic_fraction.rs +++ b/src/fraction/generic_fraction.rs @@ -1,6 +1,6 @@ use crate::fraction::Sign; use crate::{ - display, Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Integer, Num, + display, Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, ConstOne, ConstZero, FromPrimitive, Integer, Num, One, ParseRatioError, Ratio, Signed, ToPrimitive, Zero, }; #[cfg(feature = "with-bigint")] @@ -594,12 +594,20 @@ impl Zero for GenericFraction { } } +impl ConstZero for GenericFraction { + const ZERO: GenericFraction = GenericFraction::Rational(Sign::Plus, Ratio::new_raw(ConstZero::ZERO, ConstOne::ONE)); +} + impl One for GenericFraction { fn one() -> Self { GenericFraction::Rational(Sign::Plus, Ratio::one()) } } +impl ConstOne for GenericFraction { + const ONE: GenericFraction = GenericFraction::Rational(Sign::Plus, Ratio::new_raw(ConstOne::ONE, ConstOne::ONE)); +} + impl Num for GenericFraction { type FromStrRadixErr = ParseRatioError; diff --git a/src/lib.rs b/src/lib.rs index 5a70aa67..00a05935 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -225,7 +225,7 @@ pub use num::rational::{ParseRatioError, Ratio}; pub use num::{ Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Integer, Num, One, - Signed, ToPrimitive, Zero, + Signed, ToPrimitive, traits::{ConstOne, ConstZero}, Zero, }; #[cfg(test)] From c61974079a32703859a4bd1afa739fd7014e02ec Mon Sep 17 00:00:00 2001 From: Raimundo Saona <37874270+saona-raimundo@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:08:20 +0200 Subject: [PATCH 2/2] Add simple tests for ConstOne and ConstZero implementations --- src/fraction/generic_fraction.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/fraction/generic_fraction.rs b/src/fraction/generic_fraction.rs index a6ca4ed4..cb3bc0cd 100644 --- a/src/fraction/generic_fraction.rs +++ b/src/fraction/generic_fraction.rs @@ -1264,7 +1264,7 @@ mod tests { #[cfg(feature = "with-bigint")] use crate::{BigInt, BigUint}; - use crate::{Bounded, Fraction, GenericFraction, Num, One, Sign, Signed, ToPrimitive, Zero}; + use crate::{Bounded, ConstOne, ConstZero, Fraction, GenericFraction, Num, One, Sign, Signed, ToPrimitive, Zero}; use super::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub}; @@ -2709,6 +2709,20 @@ mod tests { } } + #[test] + fn constant_one() { + let constant_one = ::ONE; + let one = Frac::one(); + assert_eq!(constant_one, one); + } + + #[test] + fn constant_zero() { + let constant_zero = ::ZERO; + let zero = Frac::zero(); + assert_eq!(constant_zero, zero); + } + #[test] fn consistency_partial_cmp() { let nan = Frac::nan();