From ea53eb4e9a36e20847462e3174e64bdfc2580340 Mon Sep 17 00:00:00 2001 From: Serge Latyntcev Date: Thu, 13 Oct 2022 09:56:28 +1300 Subject: [PATCH] Mark multiple functions as const --- CHANGELOG.md | 9 +++++++ src/decimal/mod.rs | 31 ++++++++++++------------ src/fraction/display.rs | 34 +++++++++++++-------------- src/fraction/mod.rs | 52 ++++++++++++++++++++++------------------- 4 files changed, 70 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b15ae669..1e9ad278 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Change Log +## [0.12.0] + +### Changed + - Multiple functions made const in GenericFraction, GenericDecimal and fraction::display::Format + Special thanks to Stijn Frishert (aka stijnfrishert). + +### Deprecated + - GenericDecimal::apply_ref has been deprecated + ## [0.11.2] - 2022-09-18 - `DynaInt` now implements serde `Serialize & Unserialize` (Thanks to Richard Davies aka @optevo for the contribution!) diff --git a/src/decimal/mod.rs b/src/decimal/mod.rs index 49537a79..ea9877c4 100644 --- a/src/decimal/mod.rs +++ b/src/decimal/mod.rs @@ -819,11 +819,11 @@ where P: Copy + GenericInteger + Into, { /// Returns Some(Sign) of the decimal, or None if NaN is the value - pub fn sign(&self) -> Option + pub const fn sign(&self) -> Option where T: CheckedAdd + CheckedMul + CheckedSub, { - self.apply_ref(|f, _| f.sign()) + self.0.sign() } /// Sets representational precision for the Decimal @@ -868,7 +868,7 @@ where } /// Returns the current representational precision for the Decimal - pub fn get_precision(&self) -> P { + pub const fn get_precision(&self) -> P { match self { GenericDecimal(_, precision) => *precision, } @@ -969,24 +969,24 @@ where GenericDecimal(GenericFraction::min_positive_value(), P::max_value()) } - pub fn is_nan(&self) -> bool { - self.apply_ref(|f, _| f.is_nan()) + pub const fn is_nan(&self) -> bool { + self.0.is_nan() } - pub fn is_infinite(&self) -> bool { - self.apply_ref(|f, _| f.is_infinite()) + pub const fn is_infinite(&self) -> bool { + self.0.is_infinite() } - pub fn is_finite(&self) -> bool { - self.apply_ref(|f, _| f.is_finite()) + pub const fn is_finite(&self) -> bool { + self.0.is_finite() } pub fn is_normal(&self) -> bool { - self.apply_ref(|f, _| f.is_normal()) + self.0.is_normal() } pub fn classify(&self) -> FpCategory { - self.apply_ref(|f, _| f.classify()) + self.0.classify() } pub fn floor(&self) -> Self { @@ -1025,12 +1025,12 @@ where self.map_ref(|f| f.signum()) } - pub fn is_sign_positive(&self) -> bool { - self.apply_ref(|f, _| f.is_sign_positive()) + pub const fn is_sign_positive(&self) -> bool { + self.0.is_sign_positive() } - pub fn is_sign_negative(&self) -> bool { - self.apply_ref(|f, _| f.is_sign_negative()) + pub const fn is_sign_negative(&self) -> bool { + self.0.is_sign_negative() } pub fn mul_add(&self, a: Self, b: Self) -> Self { @@ -1059,6 +1059,7 @@ where } } + #[deprecated(note = "Use `match decimal {GenericDecimal(fraction, precision) => ... }`")] pub fn apply_ref(&self, fun: impl FnOnce(&GenericFraction, P) -> R) -> R { match self { GenericDecimal(fra, pres) => fun(fra, *pres), diff --git a/src/fraction/display.rs b/src/fraction/display.rs index 3ed950fe..a18ca4ed 100644 --- a/src/fraction/display.rs +++ b/src/fraction/display.rs @@ -45,7 +45,7 @@ const ALTERNATE: u8 = 4; const SIGAZEPAD: u8 = 8; impl Format { - pub fn empty() -> Self { + pub const fn empty() -> Self { Format { fill: ' ', align: None, @@ -80,11 +80,11 @@ impl Format { } } - pub fn fill(&self) -> char { + pub const fn fill(&self) -> char { self.fill } - pub fn cloned_align(&self) -> Option { + pub const fn cloned_align(&self) -> Option { match self.align { Some(ref align) => match *align { fmt::Alignment::Left => Some(fmt::Alignment::Left), @@ -95,38 +95,38 @@ impl Format { } } - pub fn align(&self) -> &Option { + pub const fn align(&self) -> &Option { &self.align } - pub fn set_align(mut self, align: Option) -> Self { + pub const fn set_align(mut self, align: Option) -> Self { self.align = align; self } - pub fn width(&self) -> &Option { + pub const fn width(&self) -> &Option { &self.width } - pub fn set_width(mut self, width: Option) -> Self { + pub const fn set_width(mut self, width: Option) -> Self { self.width = width; self } - pub fn precision(&self) -> &Option { + pub const fn precision(&self) -> &Option { &self.precision } - pub fn set_precision(mut self, precision: Option) -> Self { + pub const fn set_precision(mut self, precision: Option) -> Self { self.precision = precision; self } - pub fn sign_plus(&self) -> bool { + pub const fn sign_plus(&self) -> bool { self.flags & SIGN_PLUS > 0 } - pub fn set_sign_plus(mut self, flag: bool) -> Self { + pub const fn set_sign_plus(mut self, flag: bool) -> Self { if flag { self.flags |= SIGN_PLUS; } else { @@ -136,11 +136,11 @@ impl Format { self } - pub fn sign_minus(&self) -> bool { + pub const fn sign_minus(&self) -> bool { self.flags & SIGN_MINUS > 0 } - pub fn set_sign_minus(mut self, flag: bool) -> Self { + pub const fn set_sign_minus(mut self, flag: bool) -> Self { if flag { self.flags |= SIGN_MINUS; } else { @@ -150,11 +150,11 @@ impl Format { self } - pub fn alternate(&self) -> bool { + pub const fn alternate(&self) -> bool { self.flags & ALTERNATE > 0 } - pub fn set_alternate(mut self, flag: bool) -> Self { + pub const fn set_alternate(mut self, flag: bool) -> Self { if flag { self.flags |= ALTERNATE; } else { @@ -164,11 +164,11 @@ impl Format { self } - pub fn sign_aware_zero_pad(&self) -> bool { + pub const fn sign_aware_zero_pad(&self) -> bool { self.flags & SIGAZEPAD > 0 } - pub fn set_sign_aware_zero_pad(mut self, flag: bool) -> Self { + pub const fn set_sign_aware_zero_pad(mut self, flag: bool) -> Self { if flag { self.flags |= SIGAZEPAD; } else { diff --git a/src/fraction/mod.rs b/src/fraction/mod.rs index ba3d4e89..f61a63ad 100644 --- a/src/fraction/mod.rs +++ b/src/fraction/mod.rs @@ -2003,7 +2003,7 @@ impl GenericFraction { /// /// assert_eq! (F::nan (), F::new (0, 0)); /// ``` - pub fn nan() -> Self { + pub const fn nan() -> Self { GenericFraction::NaN } @@ -2017,7 +2017,7 @@ impl GenericFraction { /// /// assert_eq! (F::infinity (), F::new (1, 0)); /// ``` - pub fn infinity() -> Self { + pub const fn infinity() -> Self { GenericFraction::Infinity(Sign::Plus) } @@ -2031,7 +2031,7 @@ impl GenericFraction { /// /// assert_eq! (F::neg_infinity (), F::new_neg (1, 0)); /// ``` - pub fn neg_infinity() -> Self { + pub const fn neg_infinity() -> Self { GenericFraction::Infinity(Sign::Minus) } @@ -2079,7 +2079,7 @@ impl GenericFraction { /// assert! (F::nan ().is_nan ()); /// assert! (F::new (0, 0).is_nan ()); /// ``` - pub fn is_nan(&self) -> bool { + pub const fn is_nan(&self) -> bool { matches!(*self, GenericFraction::NaN) } @@ -2095,7 +2095,7 @@ impl GenericFraction { /// assert! (F::new (1u8, 0).is_infinite ()); /// assert! (F::new_neg (1u8, 0).is_infinite ()); /// ``` - pub fn is_infinite(&self) -> bool { + pub const fn is_infinite(&self) -> bool { matches!(*self, GenericFraction::Infinity(_)) } @@ -2111,7 +2111,7 @@ impl GenericFraction { /// assert! (! F::new (1u8, 0).is_finite ()); /// assert! (! F::new_neg (1u8, 0).is_finite ()); /// ``` - pub fn is_finite(&self) -> bool { + pub const fn is_finite(&self) -> bool { !matches!(*self, GenericFraction::Infinity(_)) } @@ -2294,19 +2294,19 @@ impl GenericFraction { pub fn signum(&self) -> Self { match *self { GenericFraction::NaN => GenericFraction::NaN, - GenericFraction::Infinity(s) => { - if s == Sign::Plus { - GenericFraction::Rational(Sign::Plus, Ratio::new(T::one(), T::one())) - } else { - GenericFraction::Rational(Sign::Minus, Ratio::new(T::one(), T::one())) - } + + GenericFraction::Infinity(Sign::Plus) => { + GenericFraction::Rational(Sign::Plus, Ratio::new(T::one(), T::one())) } - GenericFraction::Rational(s, _) => { - if s == Sign::Plus { - GenericFraction::Rational(Sign::Plus, Ratio::new(T::one(), T::one())) - } else { - GenericFraction::Rational(Sign::Minus, Ratio::new(T::one(), T::one())) - } + GenericFraction::Infinity(Sign::Minus) => { + GenericFraction::Rational(Sign::Minus, Ratio::new(T::one(), T::one())) + } + + GenericFraction::Rational(Sign::Plus, _) => { + GenericFraction::Rational(Sign::Plus, Ratio::new(T::one(), T::one())) + } + GenericFraction::Rational(Sign::Minus, _) => { + GenericFraction::Rational(Sign::Minus, Ratio::new(T::one(), T::one())) } } } @@ -2323,11 +2323,13 @@ impl GenericFraction { /// assert! (F::infinity ().is_sign_positive ()); /// assert! (! F::nan ().is_sign_positive ()); /// ``` - pub fn is_sign_positive(&self) -> bool { + pub const fn is_sign_positive(&self) -> bool { match *self { GenericFraction::NaN => false, - GenericFraction::Infinity(sign) => sign == Sign::Plus, - GenericFraction::Rational(sign, _) => sign == Sign::Plus, + GenericFraction::Infinity(Sign::Plus) => true, + GenericFraction::Infinity(Sign::Minus) => false, + GenericFraction::Rational(Sign::Plus, _) => true, + GenericFraction::Rational(Sign::Minus, _) => false, } } @@ -2344,11 +2346,13 @@ impl GenericFraction { /// assert! (F::neg_infinity ().is_sign_negative ()); /// assert! (! F::nan ().is_sign_negative ()); /// ``` - pub fn is_sign_negative(&self) -> bool { + pub const fn is_sign_negative(&self) -> bool { match *self { GenericFraction::NaN => false, - GenericFraction::Infinity(sign) => sign == Sign::Minus, - GenericFraction::Rational(sign, _) => sign == Sign::Minus, + GenericFraction::Infinity(Sign::Plus) => false, + GenericFraction::Infinity(Sign::Minus) => true, + GenericFraction::Rational(Sign::Plus, _) => false, + GenericFraction::Rational(Sign::Minus, _) => true, } }