Skip to content

Commit

Permalink
Mark multiple functions as const
Browse files Browse the repository at this point in the history
  • Loading branch information
dnsl48 committed Oct 12, 2022
1 parent bca8855 commit ea53eb4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 56 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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!)

Expand Down
31 changes: 16 additions & 15 deletions src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,11 @@ where
P: Copy + GenericInteger + Into<usize>,
{
/// Returns Some(Sign) of the decimal, or None if NaN is the value
pub fn sign(&self) -> Option<Sign>
pub const fn sign(&self) -> Option<Sign>
where
T: CheckedAdd + CheckedMul + CheckedSub,
{
self.apply_ref(|f, _| f.sign())
self.0.sign()
}

/// Sets representational precision for the Decimal
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1059,6 +1059,7 @@ where
}
}

#[deprecated(note = "Use `match decimal {GenericDecimal(fraction, precision) => ... }`")]
pub fn apply_ref<R>(&self, fun: impl FnOnce(&GenericFraction<T>, P) -> R) -> R {
match self {
GenericDecimal(fra, pres) => fun(fra, *pres),
Expand Down
34 changes: 17 additions & 17 deletions src/fraction/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<fmt::Alignment> {
pub const fn cloned_align(&self) -> Option<fmt::Alignment> {
match self.align {
Some(ref align) => match *align {
fmt::Alignment::Left => Some(fmt::Alignment::Left),
Expand All @@ -95,38 +95,38 @@ impl Format {
}
}

pub fn align(&self) -> &Option<fmt::Alignment> {
pub const fn align(&self) -> &Option<fmt::Alignment> {
&self.align
}

pub fn set_align(mut self, align: Option<fmt::Alignment>) -> Self {
pub const fn set_align(mut self, align: Option<fmt::Alignment>) -> Self {
self.align = align;
self
}

pub fn width(&self) -> &Option<usize> {
pub const fn width(&self) -> &Option<usize> {
&self.width
}

pub fn set_width(mut self, width: Option<usize>) -> Self {
pub const fn set_width(mut self, width: Option<usize>) -> Self {
self.width = width;
self
}

pub fn precision(&self) -> &Option<usize> {
pub const fn precision(&self) -> &Option<usize> {
&self.precision
}

pub fn set_precision(mut self, precision: Option<usize>) -> Self {
pub const fn set_precision(mut self, precision: Option<usize>) -> 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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
52 changes: 28 additions & 24 deletions src/fraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,7 @@ impl<T: Clone + Integer> GenericFraction<T> {
///
/// assert_eq! (F::nan (), F::new (0, 0));
/// ```
pub fn nan() -> Self {
pub const fn nan() -> Self {
GenericFraction::NaN
}

Expand All @@ -2017,7 +2017,7 @@ impl<T: Clone + Integer> GenericFraction<T> {
///
/// assert_eq! (F::infinity (), F::new (1, 0));
/// ```
pub fn infinity() -> Self {
pub const fn infinity() -> Self {
GenericFraction::Infinity(Sign::Plus)
}

Expand All @@ -2031,7 +2031,7 @@ impl<T: Clone + Integer> GenericFraction<T> {
///
/// 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)
}

Expand Down Expand Up @@ -2079,7 +2079,7 @@ impl<T: Clone + Integer> GenericFraction<T> {
/// 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)
}

Expand All @@ -2095,7 +2095,7 @@ impl<T: Clone + Integer> GenericFraction<T> {
/// 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(_))
}

Expand All @@ -2111,7 +2111,7 @@ impl<T: Clone + Integer> GenericFraction<T> {
/// 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(_))
}

Expand Down Expand Up @@ -2294,19 +2294,19 @@ impl<T: Clone + Integer> GenericFraction<T> {
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()))
}
}
}
Expand All @@ -2323,11 +2323,13 @@ impl<T: Clone + Integer> GenericFraction<T> {
/// 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,
}
}

Expand All @@ -2344,11 +2346,13 @@ impl<T: Clone + Integer> GenericFraction<T> {
/// 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,
}
}

Expand Down

0 comments on commit ea53eb4

Please sign in to comment.