diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index da38f2797..2ecdc5a6a 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -12,11 +12,11 @@ categories = ["command-line-utilities"] edition = "2018" [build-dependencies] -num-traits = "0.2" # used in src/numerics.rs, which is included by build.rs +num-traits = "0.2.13" # used in src/numerics.rs, which is included by build.rs [dependencies] -num-traits = "0.2" +num-traits = "0.2.13" # Needs at least version 0.2.13 for "OverflowingAdd" rand = { version="0.7", features=["small_rng"] } smallvec = { version="0.6.13, < 1.0" } uucore = { version=">=0.0.5", package="uucore", path="../../uucore" } diff --git a/src/uu/factor/build.rs b/src/uu/factor/build.rs index 4c3125472..eff21ae2f 100644 --- a/src/uu/factor/build.rs +++ b/src/uu/factor/build.rs @@ -29,6 +29,7 @@ use miller_rabin::is_prime; #[path = "src/numeric/modular_inverse.rs"] mod modular_inverse; +#[allow(unused_imports)] // imports there are used, but invisible from build.rs #[path = "src/numeric/traits.rs"] mod traits; use modular_inverse::modular_inverse; diff --git a/src/uu/factor/src/numeric/montgomery.rs b/src/uu/factor/src/numeric/montgomery.rs index e39b1dba1..8c38464bc 100644 --- a/src/uu/factor/src/numeric/montgomery.rs +++ b/src/uu/factor/src/numeric/montgomery.rs @@ -8,8 +8,7 @@ use super::*; -use num_traits::identities::{One, Zero}; -use traits::{DoubleInt, Int, OverflowingAdd}; +use traits::{DoubleInt, Int, One, OverflowingAdd, Zero}; pub(crate) trait Arithmetic: Copy + Sized { // The type of integers mod m, in some opaque representation @@ -74,7 +73,7 @@ impl Montgomery { let Montgomery { a, n } = self; let m = T::from_double_width(x).wrapping_mul(a); let nm = (n.as_double_width()) * (m.as_double_width()); - let (xnm, overflow) = x.overflowing_add_(nm); // x + n*m + let (xnm, overflow) = x.overflowing_add(&nm); // x + n*m debug_assert_eq!( xnm % (T::DoubleWidth::one() << T::zero().count_zeros() as usize), T::DoubleWidth::zero() @@ -130,7 +129,7 @@ impl Arithmetic for Montgomery { } fn add(&self, a: Self::ModInt, b: Self::ModInt) -> Self::ModInt { - let (r, overflow) = a.overflowing_add_(b); + let (r, overflow) = a.overflowing_add(&b); // In case of overflow, a+b = 2⁶⁴ + r = (2⁶⁴ - n) + r (working mod n) let r = if !overflow { diff --git a/src/uu/factor/src/numeric/traits.rs b/src/uu/factor/src/numeric/traits.rs index 50f5ab5c1..2e9167e0b 100644 --- a/src/uu/factor/src/numeric/traits.rs +++ b/src/uu/factor/src/numeric/traits.rs @@ -6,31 +6,16 @@ // * For the full copyright and license information, please view the LICENSE file // * that was distributed with this source code. +pub(crate) use num_traits::{ + identities::{One, Zero}, + ops::overflowing::OverflowingAdd, +}; use num_traits::{ int::PrimInt, ops::wrapping::{WrappingMul, WrappingNeg, WrappingSub}, }; use std::fmt::{Debug, Display}; -// NOTE: Trait can be removed once num-traits adds a similar one; -// see https://github.com/rust-num/num-traits/issues/168 -pub(crate) trait OverflowingAdd: Sized { - fn overflowing_add_(self, n: Self) -> (Self, bool); -} - -macro_rules! overflowing { - ($x:ty) => { - impl OverflowingAdd for $x { - fn overflowing_add_(self, n: Self) -> (Self, bool) { - self.overflowing_add(n) - } - } - }; -} -overflowing!(u32); -overflowing!(u64); -overflowing!(u128); - pub(crate) trait Int: Display + Debug + PrimInt + OverflowingAdd + WrappingNeg + WrappingSub + WrappingMul {