Skip to content

Commit

Permalink
Add const ZERO/ONE and impl ConstZero/ConstOne
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed May 8, 2024
1 parent e580ced commit 2fb828a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ default-features = false
features = ["i128"]

[dependencies.num-traits]
version = "0.2.11"
version = "0.2.18"
default-features = false
features = ["i128"]

Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
use num_integer::Integer;
use num_traits::float::FloatCore;
use num_traits::{
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Inv, Num, NumCast, One,
Pow, Signed, ToPrimitive, Unsigned, Zero,
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, ConstOne, ConstZero, FromPrimitive,
Inv, Num, NumCast, One, Pow, Signed, ToPrimitive, Unsigned, Zero,
};

mod pow;
Expand Down Expand Up @@ -922,6 +922,15 @@ where
}

// Constants
impl<T: ConstZero + ConstOne> Ratio<T> {
/// A constant `Ratio` 0/1.
pub const ZERO: Self = Self::new_raw(T::ZERO, T::ONE);
}

impl<T: Clone + Integer + ConstZero + ConstOne> ConstZero for Ratio<T> {
const ZERO: Self = Self::ZERO;
}

impl<T: Clone + Integer> Zero for Ratio<T> {
#[inline]
fn zero() -> Ratio<T> {
Expand All @@ -940,6 +949,15 @@ impl<T: Clone + Integer> Zero for Ratio<T> {
}
}

impl<T: ConstOne> Ratio<T> {
/// A constant `Ratio` 1/1.
pub const ONE: Self = Self::new_raw(T::ONE, T::ONE);
}

impl<T: Clone + Integer + ConstOne> ConstOne for Ratio<T> {
const ONE: Self = Self::ONE;
}

impl<T: Clone + Integer> One for Ratio<T> {
#[inline]
fn one() -> Ratio<T> {
Expand Down

0 comments on commit 2fb828a

Please sign in to comment.