From 75ec0b67ecf547a980c17c8b3202fca516121912 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Sun, 22 Jun 2025 18:47:04 -0700 Subject: [PATCH] ran `cargo clippy --fix -- -A clippy::all -W clippy::use_self` --- src/bigint.rs | 139 +++++++++++++++++----------------- src/bigint/addition.rs | 62 +++++++-------- src/bigint/bits.rs | 30 ++++---- src/bigint/convert.rs | 48 ++++++------ src/bigint/division.rs | 78 +++++++++---------- src/bigint/multiplication.rs | 38 +++++----- src/bigint/subtraction.rs | 66 ++++++++-------- src/biguint.rs | 64 ++++++++-------- src/biguint/addition.rs | 24 +++--- src/biguint/bits.rs | 30 ++++---- src/biguint/convert.rs | 30 ++++---- src/biguint/division.rs | 52 ++++++------- src/biguint/monty.rs | 8 +- src/biguint/multiplication.rs | 14 ++-- src/biguint/power.rs | 14 ++-- src/biguint/subtraction.rs | 24 +++--- src/lib.rs | 6 +- 17 files changed, 361 insertions(+), 366 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index b4f84b9e..cf1aa9f7 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -40,11 +40,11 @@ pub enum Sign { } impl Neg for Sign { - type Output = Sign; + type Output = Self; /// Negate `Sign` value. #[inline] - fn neg(self) -> Sign { + fn neg(self) -> Self { match self { Minus => Plus, NoSign => NoSign, @@ -64,7 +64,7 @@ pub struct BigInt { impl Clone for BigInt { #[inline] fn clone(&self) -> Self { - BigInt { + Self { sign: self.sign, data: self.data.clone(), } @@ -90,7 +90,7 @@ impl hash::Hash for BigInt { impl PartialEq for BigInt { #[inline] - fn eq(&self, other: &BigInt) -> bool { + fn eq(&self, other: &Self) -> bool { debug_assert!((self.sign != NoSign) ^ self.data.is_zero()); debug_assert!((other.sign != NoSign) ^ other.data.is_zero()); self.sign == other.sign && (self.sign == NoSign || self.data == other.data) @@ -101,14 +101,14 @@ impl Eq for BigInt {} impl PartialOrd for BigInt { #[inline] - fn partial_cmp(&self, other: &BigInt) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for BigInt { #[inline] - fn cmp(&self, other: &BigInt) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { debug_assert!((self.sign != NoSign) ^ self.data.is_zero()); debug_assert!((other.sign != NoSign) ^ other.data.is_zero()); let scmp = self.sign.cmp(&other.sign); @@ -126,7 +126,7 @@ impl Ord for BigInt { impl Default for BigInt { #[inline] - fn default() -> BigInt { + fn default() -> Self { Self::ZERO } } @@ -174,9 +174,9 @@ impl fmt::UpperHex for BigInt { // ! 0 = !...0 00 = ...f ff = -1 // !+1 = !...0 01 = ...f fe = -2 impl Not for BigInt { - type Output = BigInt; + type Output = Self; - fn not(mut self) -> BigInt { + fn not(mut self) -> Self { match self.sign { NoSign | Plus => { self.data += 1u32; @@ -205,7 +205,7 @@ impl Not for &BigInt { impl Zero for BigInt { #[inline] - fn zero() -> BigInt { + fn zero() -> Self { Self::ZERO } @@ -228,8 +228,8 @@ impl ConstZero for BigInt { impl One for BigInt { #[inline] - fn one() -> BigInt { - BigInt { + fn one() -> Self { + Self { sign: Plus, data: BigUint::one(), } @@ -249,15 +249,15 @@ impl One for BigInt { impl Signed for BigInt { #[inline] - fn abs(&self) -> BigInt { + fn abs(&self) -> Self { match self.sign { Plus | NoSign => self.clone(), - Minus => BigInt::from(self.data.clone()), + Minus => Self::from(self.data.clone()), } } #[inline] - fn abs_sub(&self, other: &BigInt) -> BigInt { + fn abs_sub(&self, other: &Self) -> Self { if *self <= *other { Self::ZERO } else { @@ -266,10 +266,10 @@ impl Signed for BigInt { } #[inline] - fn signum(&self) -> BigInt { + fn signum(&self) -> Self { match self.sign { - Plus => BigInt::one(), - Minus => -BigInt::one(), + Plus => Self::one(), + Minus => -Self::one(), NoSign => Self::ZERO, } } @@ -321,10 +321,10 @@ impl_unsigned_abs!(i128, u128); impl_unsigned_abs!(isize, usize); impl Neg for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn neg(mut self) -> BigInt { + fn neg(mut self) -> Self { self.sign = -self.sign; self } @@ -341,11 +341,11 @@ impl Neg for &BigInt { impl Integer for BigInt { #[inline] - fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) { + fn div_rem(&self, other: &Self) -> (Self, Self) { // r.sign == self.sign let (d_ui, r_ui) = self.data.div_rem(&other.data); - let d = BigInt::from_biguint(self.sign, d_ui); - let r = BigInt::from_biguint(self.sign, r_ui); + let d = Self::from_biguint(self.sign, d_ui); + let r = Self::from_biguint(self.sign, r_ui); if other.is_negative() { (-d, r) } else { @@ -354,9 +354,9 @@ impl Integer for BigInt { } #[inline] - fn div_floor(&self, other: &BigInt) -> BigInt { + fn div_floor(&self, other: &Self) -> Self { let (d_ui, m) = self.data.div_mod_floor(&other.data); - let d = BigInt::from(d_ui); + let d = Self::from(d_ui); match (self.sign, other.sign) { (Plus, Plus) | (NoSign, Plus) | (Minus, Minus) => d, (Plus, Minus) | (NoSign, Minus) | (Minus, Plus) => { @@ -371,10 +371,10 @@ impl Integer for BigInt { } #[inline] - fn mod_floor(&self, other: &BigInt) -> BigInt { + fn mod_floor(&self, other: &Self) -> Self { // m.sign == other.sign let m_ui = self.data.mod_floor(&other.data); - let m = BigInt::from_biguint(other.sign, m_ui); + let m = Self::from_biguint(other.sign, m_ui); match (self.sign, other.sign) { (Plus, Plus) | (NoSign, Plus) | (Minus, Minus) => m, (Plus, Minus) | (NoSign, Minus) | (Minus, Plus) => { @@ -388,11 +388,11 @@ impl Integer for BigInt { } } - fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) { + fn div_mod_floor(&self, other: &Self) -> (Self, Self) { // m.sign == other.sign let (d_ui, m_ui) = self.data.div_mod_floor(&other.data); - let d = BigInt::from(d_ui); - let m = BigInt::from_biguint(other.sign, m_ui); + let d = Self::from(d_ui); + let m = Self::from_biguint(other.sign, m_ui); match (self.sign, other.sign) { (Plus, Plus) | (NoSign, Plus) | (Minus, Minus) => (d, m), (Plus, Minus) | (NoSign, Minus) | (Minus, Plus) => { @@ -409,7 +409,7 @@ impl Integer for BigInt { #[inline] fn div_ceil(&self, other: &Self) -> Self { let (d_ui, m) = self.data.div_mod_floor(&other.data); - let d = BigInt::from(d_ui); + let d = Self::from(d_ui); match (self.sign, other.sign) { (Plus, Minus) | (NoSign, Minus) | (Minus, Plus) => -d, (Plus, Plus) | (NoSign, Plus) | (Minus, Minus) => { @@ -427,45 +427,45 @@ impl Integer for BigInt { /// /// The result is always positive. #[inline] - fn gcd(&self, other: &BigInt) -> BigInt { - BigInt::from(self.data.gcd(&other.data)) + fn gcd(&self, other: &Self) -> Self { + Self::from(self.data.gcd(&other.data)) } /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. #[inline] - fn lcm(&self, other: &BigInt) -> BigInt { - BigInt::from(self.data.lcm(&other.data)) + fn lcm(&self, other: &Self) -> Self { + Self::from(self.data.lcm(&other.data)) } /// Calculates the Greatest Common Divisor (GCD) and /// Lowest Common Multiple (LCM) together. #[inline] - fn gcd_lcm(&self, other: &BigInt) -> (BigInt, BigInt) { + fn gcd_lcm(&self, other: &Self) -> (Self, Self) { let (gcd, lcm) = self.data.gcd_lcm(&other.data); - (BigInt::from(gcd), BigInt::from(lcm)) + (Self::from(gcd), Self::from(lcm)) } /// Greatest common divisor, least common multiple, and Bézout coefficients. #[inline] - fn extended_gcd_lcm(&self, other: &BigInt) -> (num_integer::ExtendedGcd, BigInt) { + fn extended_gcd_lcm(&self, other: &Self) -> (num_integer::ExtendedGcd, Self) { let egcd = self.extended_gcd(other); let lcm = if egcd.gcd.is_zero() { Self::ZERO } else { - BigInt::from(&self.data / &egcd.gcd.data * &other.data) + Self::from(&self.data / &egcd.gcd.data * &other.data) }; (egcd, lcm) } /// Deprecated, use `is_multiple_of` instead. #[inline] - fn divides(&self, other: &BigInt) -> bool { + fn divides(&self, other: &Self) -> bool { self.is_multiple_of(other) } /// Returns `true` if the number is a multiple of `other`. #[inline] - fn is_multiple_of(&self, other: &BigInt) -> bool { + fn is_multiple_of(&self, other: &Self) -> bool { self.data.is_multiple_of(&other.data) } @@ -510,21 +510,20 @@ impl Roots for BigInt { fn nth_root(&self, n: u32) -> Self { assert!( !(self.is_negative() && n.is_even()), - "root of degree {} is imaginary", - n + "root of degree {n} is imaginary" ); - BigInt::from_biguint(self.sign, self.data.nth_root(n)) + Self::from_biguint(self.sign, self.data.nth_root(n)) } fn sqrt(&self) -> Self { assert!(!self.is_negative(), "square root is imaginary"); - BigInt::from_biguint(self.sign, self.data.sqrt()) + Self::from_biguint(self.sign, self.data.sqrt()) } fn cbrt(&self) -> Self { - BigInt::from_biguint(self.sign, self.data.cbrt()) + Self::from_biguint(self.sign, self.data.cbrt()) } } @@ -564,7 +563,7 @@ pub trait ToBigInt { impl BigInt { /// A constant `BigInt` with value 0, useful for static initialization. - pub const ZERO: Self = BigInt { + pub const ZERO: Self = Self { sign: NoSign, data: BigUint::ZERO, }; @@ -573,30 +572,30 @@ impl BigInt { /// /// The base 232 digits are ordered least significant digit first. #[inline] - pub fn new(sign: Sign, digits: Vec) -> BigInt { - BigInt::from_biguint(sign, BigUint::new(digits)) + pub fn new(sign: Sign, digits: Vec) -> Self { + Self::from_biguint(sign, BigUint::new(digits)) } /// Creates and initializes a [`BigInt`]. /// /// The base 232 digits are ordered least significant digit first. #[inline] - pub fn from_biguint(mut sign: Sign, mut data: BigUint) -> BigInt { + pub fn from_biguint(mut sign: Sign, mut data: BigUint) -> Self { if sign == NoSign { data.assign_from_slice(&[]); } else if data.is_zero() { sign = NoSign; } - BigInt { sign, data } + Self { sign, data } } /// Creates and initializes a [`BigInt`]. /// /// The base 232 digits are ordered least significant digit first. #[inline] - pub fn from_slice(sign: Sign, slice: &[u32]) -> BigInt { - BigInt::from_biguint(sign, BigUint::from_slice(slice)) + pub fn from_slice(sign: Sign, slice: &[u32]) -> Self { + Self::from_biguint(sign, BigUint::from_slice(slice)) } /// Reinitializes a [`BigInt`]. @@ -631,16 +630,16 @@ impl BigInt { /// BigInt::parse_bytes(b"22405534230753963835153736737", 10).unwrap()); /// ``` #[inline] - pub fn from_bytes_be(sign: Sign, bytes: &[u8]) -> BigInt { - BigInt::from_biguint(sign, BigUint::from_bytes_be(bytes)) + pub fn from_bytes_be(sign: Sign, bytes: &[u8]) -> Self { + Self::from_biguint(sign, BigUint::from_bytes_be(bytes)) } /// Creates and initializes a [`BigInt`]. /// /// The bytes are in little-endian byte order. #[inline] - pub fn from_bytes_le(sign: Sign, bytes: &[u8]) -> BigInt { - BigInt::from_biguint(sign, BigUint::from_bytes_le(bytes)) + pub fn from_bytes_le(sign: Sign, bytes: &[u8]) -> Self { + Self::from_biguint(sign, BigUint::from_bytes_le(bytes)) } /// Creates and initializes a [`BigInt`] from an array of bytes in @@ -648,7 +647,7 @@ impl BigInt { /// /// The digits are in big-endian base 28. #[inline] - pub fn from_signed_bytes_be(digits: &[u8]) -> BigInt { + pub fn from_signed_bytes_be(digits: &[u8]) -> Self { convert::from_signed_bytes_be(digits) } @@ -656,7 +655,7 @@ impl BigInt { /// /// The digits are in little-endian base 28. #[inline] - pub fn from_signed_bytes_le(digits: &[u8]) -> BigInt { + pub fn from_signed_bytes_le(digits: &[u8]) -> Self { convert::from_signed_bytes_le(digits) } @@ -672,9 +671,9 @@ impl BigInt { /// assert_eq!(BigInt::parse_bytes(b"G", 16), None); /// ``` #[inline] - pub fn parse_bytes(buf: &[u8], radix: u32) -> Option { + pub fn parse_bytes(buf: &[u8], radix: u32) -> Option { let s = str::from_utf8(buf).ok()?; - BigInt::from_str_radix(s, radix).ok() + Self::from_str_radix(s, radix).ok() } /// Creates and initializes a [`BigInt`]. Each `u8` of the input slice is @@ -693,9 +692,9 @@ impl BigInt { /// let a = BigInt::from_radix_be(Sign::Minus, &inbase190, 190).unwrap(); /// assert_eq!(a.to_radix_be(190), (Sign:: Minus, inbase190)); /// ``` - pub fn from_radix_be(sign: Sign, buf: &[u8], radix: u32) -> Option { + pub fn from_radix_be(sign: Sign, buf: &[u8], radix: u32) -> Option { let u = BigUint::from_radix_be(buf, radix)?; - Some(BigInt::from_biguint(sign, u)) + Some(Self::from_biguint(sign, u)) } /// Creates and initializes a [`BigInt`]. Each `u8` of the input slice is @@ -714,9 +713,9 @@ impl BigInt { /// let a = BigInt::from_radix_be(Sign::Minus, &inbase190, 190).unwrap(); /// assert_eq!(a.to_radix_be(190), (Sign::Minus, inbase190)); /// ``` - pub fn from_radix_le(sign: Sign, buf: &[u8], radix: u32) -> Option { + pub fn from_radix_le(sign: Sign, buf: &[u8], radix: u32) -> Option { let u = BigUint::from_radix_le(buf, radix)?; - Some(BigInt::from_biguint(sign, u)) + Some(Self::from_biguint(sign, u)) } /// Returns the sign and the byte representation of the [`BigInt`] in big-endian byte order. @@ -986,22 +985,22 @@ impl BigInt { } #[inline] - pub fn checked_add(&self, v: &BigInt) -> Option { + pub fn checked_add(&self, v: &Self) -> Option { Some(self + v) } #[inline] - pub fn checked_sub(&self, v: &BigInt) -> Option { + pub fn checked_sub(&self, v: &Self) -> Option { Some(self - v) } #[inline] - pub fn checked_mul(&self, v: &BigInt) -> Option { + pub fn checked_mul(&self, v: &Self) -> Option { Some(self * v) } #[inline] - pub fn checked_div(&self, v: &BigInt) -> Option { + pub fn checked_div(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -1080,7 +1079,7 @@ impl BigInt { (false, true) => (Minus, &modulus.data - result), (true, true) => (Minus, result), }; - Some(BigInt::from_biguint(sign, mag)) + Some(Self::from_biguint(sign, mag)) } /// Returns the truncated principal square root of `self` -- diff --git a/src/bigint/addition.rs b/src/bigint/addition.rs index 0d3a0e2a..1ce872a1 100644 --- a/src/bigint/addition.rs +++ b/src/bigint/addition.rs @@ -55,27 +55,27 @@ impl Add for &BigInt { } } -impl Add<&BigInt> for BigInt { - type Output = BigInt; +impl Add<&Self> for BigInt { + type Output = Self; #[inline] - fn add(self, other: &BigInt) -> BigInt { + fn add(self, other: &Self) -> Self { bigint_add!(self, self, self.data, other, other.clone(), &other.data) } } -impl Add for BigInt { - type Output = BigInt; +impl Add for BigInt { + type Output = Self; #[inline] - fn add(self, other: BigInt) -> BigInt { + fn add(self, other: Self) -> Self { bigint_add!(self, self, self.data, other, other, other.data) } } -impl AddAssign<&BigInt> for BigInt { +impl AddAssign<&Self> for BigInt { #[inline] - fn add_assign(&mut self, other: &BigInt) { + fn add_assign(&mut self, other: &Self) { let n = mem::replace(self, Self::ZERO); *self = n + other; } @@ -89,17 +89,17 @@ forward_all_scalar_binop_to_val_val_commutative!(impl Add for BigInt, add); forward_all_scalar_binop_to_val_val_commutative!(impl Add for BigInt, add); impl Add for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn add(self, other: u32) -> BigInt { + fn add(self, other: u32) -> Self { match self.sign { NoSign => From::from(other), - Plus => BigInt::from(self.data + other), + Plus => Self::from(self.data + other), Minus => match self.data.cmp(&From::from(other)) { Equal => Self::ZERO, - Less => BigInt::from(other - self.data), - Greater => -BigInt::from(self.data - other), + Less => Self::from(other - self.data), + Greater => -Self::from(self.data - other), }, } } @@ -114,17 +114,17 @@ impl AddAssign for BigInt { } impl Add for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn add(self, other: u64) -> BigInt { + fn add(self, other: u64) -> Self { match self.sign { NoSign => From::from(other), - Plus => BigInt::from(self.data + other), + Plus => Self::from(self.data + other), Minus => match self.data.cmp(&From::from(other)) { Equal => Self::ZERO, - Less => BigInt::from(other - self.data), - Greater => -BigInt::from(self.data - other), + Less => Self::from(other - self.data), + Greater => -Self::from(self.data - other), }, } } @@ -139,17 +139,17 @@ impl AddAssign for BigInt { } impl Add for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn add(self, other: u128) -> BigInt { + fn add(self, other: u128) -> Self { match self.sign { - NoSign => BigInt::from(other), - Plus => BigInt::from(self.data + other), + NoSign => Self::from(other), + Plus => Self::from(self.data + other), Minus => match self.data.cmp(&From::from(other)) { Equal => Self::ZERO, - Less => BigInt::from(other - self.data), - Greater => -BigInt::from(self.data - other), + Less => Self::from(other - self.data), + Greater => -Self::from(self.data - other), }, } } @@ -167,10 +167,10 @@ forward_all_scalar_binop_to_val_val_commutative!(impl Add for BigInt, add); forward_all_scalar_binop_to_val_val_commutative!(impl Add for BigInt, add); impl Add for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn add(self, other: i32) -> BigInt { + fn add(self, other: i32) -> Self { match other.checked_uabs() { Positive(u) => self + u, Negative(u) => self - u, @@ -188,10 +188,10 @@ impl AddAssign for BigInt { } impl Add for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn add(self, other: i64) -> BigInt { + fn add(self, other: i64) -> Self { match other.checked_uabs() { Positive(u) => self + u, Negative(u) => self - u, @@ -209,10 +209,10 @@ impl AddAssign for BigInt { } impl Add for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn add(self, other: i128) -> BigInt { + fn add(self, other: i128) -> Self { match other.checked_uabs() { Positive(u) => self + u, Negative(u) => self - u, @@ -231,7 +231,7 @@ impl AddAssign for BigInt { impl CheckedAdd for BigInt { #[inline] - fn checked_add(&self, v: &BigInt) -> Option { + fn checked_add(&self, v: &Self) -> Option { Some(self.add(v)) } } diff --git a/src/bigint/bits.rs b/src/bigint/bits.rs index ac4fe1de..8698ee43 100644 --- a/src/bigint/bits.rs +++ b/src/bigint/bits.rs @@ -130,11 +130,11 @@ impl BitAnd<&BigInt> for &BigInt { } } -impl BitAnd<&BigInt> for BigInt { - type Output = BigInt; +impl BitAnd<&Self> for BigInt { + type Output = Self; #[inline] - fn bitand(mut self, other: &BigInt) -> BigInt { + fn bitand(mut self, other: &Self) -> Self { self &= other; self } @@ -142,8 +142,8 @@ impl BitAnd<&BigInt> for BigInt { forward_val_assign!(impl BitAndAssign for BigInt, bitand_assign); -impl BitAndAssign<&BigInt> for BigInt { - fn bitand_assign(&mut self, other: &BigInt) { +impl BitAndAssign<&Self> for BigInt { + fn bitand_assign(&mut self, other: &Self) { match (self.sign, other.sign) { (NoSign, _) => {} (_, NoSign) => self.set_zero(), @@ -270,11 +270,11 @@ impl BitOr<&BigInt> for &BigInt { } } -impl BitOr<&BigInt> for BigInt { - type Output = BigInt; +impl BitOr<&Self> for BigInt { + type Output = Self; #[inline] - fn bitor(mut self, other: &BigInt) -> BigInt { + fn bitor(mut self, other: &Self) -> Self { self |= other; self } @@ -282,8 +282,8 @@ impl BitOr<&BigInt> for BigInt { forward_val_assign!(impl BitOrAssign for BigInt, bitor_assign); -impl BitOrAssign<&BigInt> for BigInt { - fn bitor_assign(&mut self, other: &BigInt) { +impl BitOrAssign<&Self> for BigInt { + fn bitor_assign(&mut self, other: &Self) { match (self.sign, other.sign) { (_, NoSign) => {} (NoSign, _) => self.clone_from(other), @@ -408,11 +408,11 @@ fn bitxor_neg_neg(a: &mut Vec, b: &[BigDigit]) { forward_all_binop_to_val_ref_commutative!(impl BitXor for BigInt, bitxor); -impl BitXor<&BigInt> for BigInt { - type Output = BigInt; +impl BitXor<&Self> for BigInt { + type Output = Self; #[inline] - fn bitxor(mut self, other: &BigInt) -> BigInt { + fn bitxor(mut self, other: &Self) -> Self { self ^= other; self } @@ -420,8 +420,8 @@ impl BitXor<&BigInt> for BigInt { forward_val_assign!(impl BitXorAssign for BigInt, bitxor_assign); -impl BitXorAssign<&BigInt> for BigInt { - fn bitxor_assign(&mut self, other: &BigInt) { +impl BitXorAssign<&Self> for BigInt { + fn bitxor_assign(&mut self, other: &Self) { match (self.sign, other.sign) { (_, NoSign) => {} (NoSign, _) => self.clone_from(other), diff --git a/src/bigint/convert.rs b/src/bigint/convert.rs index d0e28b67..35e37640 100644 --- a/src/bigint/convert.rs +++ b/src/bigint/convert.rs @@ -14,8 +14,8 @@ impl FromStr for BigInt { type Err = ParseBigIntError; #[inline] - fn from_str(s: &str) -> Result { - BigInt::from_str_radix(s, 10) + fn from_str(s: &str) -> Result { + Self::from_str_radix(s, 10) } } @@ -24,7 +24,7 @@ impl Num for BigInt { /// Creates and initializes a [`BigInt`]. #[inline] - fn from_str_radix(mut s: &str, radix: u32) -> Result { + fn from_str_radix(mut s: &str, radix: u32) -> Result { let sign = if let Some(tail) = s.strip_prefix('-') { if !tail.starts_with('+') { s = tail @@ -34,7 +34,7 @@ impl Num for BigInt { Plus }; let bu = BigUint::from_str_radix(s, radix)?; - Ok(BigInt::from_biguint(sign, bu)) + Ok(Self::from_biguint(sign, bu)) } } @@ -142,32 +142,32 @@ impl_try_from_bigint!(i128, ToPrimitive::to_i128); impl FromPrimitive for BigInt { #[inline] - fn from_i64(n: i64) -> Option { - Some(BigInt::from(n)) + fn from_i64(n: i64) -> Option { + Some(Self::from(n)) } #[inline] - fn from_i128(n: i128) -> Option { - Some(BigInt::from(n)) + fn from_i128(n: i128) -> Option { + Some(Self::from(n)) } #[inline] - fn from_u64(n: u64) -> Option { - Some(BigInt::from(n)) + fn from_u64(n: u64) -> Option { + Some(Self::from(n)) } #[inline] - fn from_u128(n: u128) -> Option { - Some(BigInt::from(n)) + fn from_u128(n: u128) -> Option { + Some(Self::from(n)) } #[inline] - fn from_f64(n: f64) -> Option { + fn from_f64(n: f64) -> Option { if n >= 0.0 { - BigUint::from_f64(n).map(BigInt::from) + BigUint::from_f64(n).map(Self::from) } else { let x = BigUint::from_f64(-n)?; - Some(-BigInt::from(x)) + Some(-Self::from(x)) } } } @@ -176,10 +176,10 @@ impl From for BigInt { #[inline] fn from(n: i64) -> Self { if n >= 0 { - BigInt::from(n as u64) + Self::from(n as u64) } else { let u = u64::MAX - (n as u64) + 1; - BigInt { + Self { sign: Minus, data: BigUint::from(u), } @@ -191,10 +191,10 @@ impl From for BigInt { #[inline] fn from(n: i128) -> Self { if n >= 0 { - BigInt::from(n as u128) + Self::from(n as u128) } else { let u = u128::MAX - (n as u128) + 1; - BigInt { + Self { sign: Minus, data: BigUint::from(u), } @@ -222,7 +222,7 @@ impl From for BigInt { #[inline] fn from(n: u64) -> Self { if n > 0 { - BigInt { + Self { sign: Plus, data: BigUint::from(n), } @@ -236,7 +236,7 @@ impl From for BigInt { #[inline] fn from(n: u128) -> Self { if n > 0 { - BigInt { + Self { sign: Plus, data: BigUint::from(n), } @@ -268,7 +268,7 @@ impl From for BigInt { if n.is_zero() { Self::ZERO } else { - BigInt { + Self { sign: Plus, data: n, } @@ -312,7 +312,7 @@ impl TryFrom<&BigInt> for BigUint { type Error = TryFromBigIntError<()>; #[inline] - fn try_from(value: &BigInt) -> Result> { + fn try_from(value: &BigInt) -> Result> { value .to_biguint() .ok_or_else(|| TryFromBigIntError::new(())) @@ -323,7 +323,7 @@ impl TryFrom for BigUint { type Error = TryFromBigIntError; #[inline] - fn try_from(value: BigInt) -> Result> { + fn try_from(value: BigInt) -> Result> { if value.sign() == Sign::Minus { Err(TryFromBigIntError::new(value)) } else { diff --git a/src/bigint/division.rs b/src/bigint/division.rs index 0d4d23f3..30f784cf 100644 --- a/src/bigint/division.rs +++ b/src/bigint/division.rs @@ -20,9 +20,9 @@ impl Div<&BigInt> for &BigInt { } } -impl DivAssign<&BigInt> for BigInt { +impl DivAssign<&Self> for BigInt { #[inline] - fn div_assign(&mut self, other: &BigInt) { + fn div_assign(&mut self, other: &Self) { *self = &*self / other; } } @@ -35,11 +35,11 @@ forward_all_scalar_binop_to_val_val!(impl Div for BigInt, div); forward_all_scalar_binop_to_val_val!(impl Div for BigInt, div); impl Div for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn div(self, other: u32) -> BigInt { - BigInt::from_biguint(self.sign, self.data / other) + fn div(self, other: u32) -> Self { + Self::from_biguint(self.sign, self.data / other) } } @@ -63,11 +63,11 @@ impl Div for u32 { } impl Div for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn div(self, other: u64) -> BigInt { - BigInt::from_biguint(self.sign, self.data / other) + fn div(self, other: u64) -> Self { + Self::from_biguint(self.sign, self.data / other) } } @@ -91,11 +91,11 @@ impl Div for u64 { } impl Div for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn div(self, other: u128) -> BigInt { - BigInt::from_biguint(self.sign, self.data / other) + fn div(self, other: u128) -> Self { + Self::from_biguint(self.sign, self.data / other) } } @@ -123,10 +123,10 @@ forward_all_scalar_binop_to_val_val!(impl Div for BigInt, div); forward_all_scalar_binop_to_val_val!(impl Div for BigInt, div); impl Div for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn div(self, other: i32) -> BigInt { + fn div(self, other: i32) -> Self { match other.checked_uabs() { Positive(u) => self / u, Negative(u) => -self / u, @@ -160,10 +160,10 @@ impl Div for i32 { } impl Div for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn div(self, other: i64) -> BigInt { + fn div(self, other: i64) -> Self { match other.checked_uabs() { Positive(u) => self / u, Negative(u) => -self / u, @@ -197,10 +197,10 @@ impl Div for i64 { } impl Div for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn div(self, other: i128) -> BigInt { + fn div(self, other: i128) -> Self { match other.checked_uabs() { Positive(u) => self / u, Negative(u) => -self / u, @@ -251,9 +251,9 @@ impl Rem<&BigInt> for &BigInt { } } -impl RemAssign<&BigInt> for BigInt { +impl RemAssign<&Self> for BigInt { #[inline] - fn rem_assign(&mut self, other: &BigInt) { + fn rem_assign(&mut self, other: &Self) { *self = &*self % other; } } @@ -266,11 +266,11 @@ forward_all_scalar_binop_to_val_val!(impl Rem for BigInt, rem); forward_all_scalar_binop_to_val_val!(impl Rem for BigInt, rem); impl Rem for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn rem(self, other: u32) -> BigInt { - BigInt::from_biguint(self.sign, self.data % other) + fn rem(self, other: u32) -> Self { + Self::from_biguint(self.sign, self.data % other) } } @@ -294,11 +294,11 @@ impl Rem for u32 { } impl Rem for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn rem(self, other: u64) -> BigInt { - BigInt::from_biguint(self.sign, self.data % other) + fn rem(self, other: u64) -> Self { + Self::from_biguint(self.sign, self.data % other) } } @@ -322,11 +322,11 @@ impl Rem for u64 { } impl Rem for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn rem(self, other: u128) -> BigInt { - BigInt::from_biguint(self.sign, self.data % other) + fn rem(self, other: u128) -> Self { + Self::from_biguint(self.sign, self.data % other) } } @@ -354,10 +354,10 @@ forward_all_scalar_binop_to_val_val!(impl Rem for BigInt, rem); forward_all_scalar_binop_to_val_val!(impl Rem for BigInt, rem); impl Rem for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn rem(self, other: i32) -> BigInt { + fn rem(self, other: i32) -> Self { self % other.unsigned_abs() } } @@ -382,10 +382,10 @@ impl Rem for i32 { } impl Rem for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn rem(self, other: i64) -> BigInt { + fn rem(self, other: i64) -> Self { self % other.unsigned_abs() } } @@ -410,10 +410,10 @@ impl Rem for i64 { } impl Rem for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn rem(self, other: i128) -> BigInt { + fn rem(self, other: i128) -> Self { self % other.unsigned_abs() } } @@ -439,7 +439,7 @@ impl Rem for i128 { impl CheckedDiv for BigInt { #[inline] - fn checked_div(&self, v: &BigInt) -> Option { + fn checked_div(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -449,7 +449,7 @@ impl CheckedDiv for BigInt { impl CheckedEuclid for BigInt { #[inline] - fn checked_div_euclid(&self, v: &BigInt) -> Option { + fn checked_div_euclid(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -457,7 +457,7 @@ impl CheckedEuclid for BigInt { } #[inline] - fn checked_rem_euclid(&self, v: &BigInt) -> Option { + fn checked_rem_euclid(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -471,7 +471,7 @@ impl CheckedEuclid for BigInt { impl Euclid for BigInt { #[inline] - fn div_euclid(&self, v: &BigInt) -> BigInt { + fn div_euclid(&self, v: &Self) -> Self { let (q, r) = self.div_rem(v); if r.is_negative() { if v.is_positive() { @@ -485,7 +485,7 @@ impl Euclid for BigInt { } #[inline] - fn rem_euclid(&self, v: &BigInt) -> BigInt { + fn rem_euclid(&self, v: &Self) -> Self { let r = self % v; if r.is_negative() { if v.is_positive() { diff --git a/src/bigint/multiplication.rs b/src/bigint/multiplication.rs index 82e64c28..44558f01 100644 --- a/src/bigint/multiplication.rs +++ b/src/bigint/multiplication.rs @@ -8,11 +8,11 @@ use core::iter::Product; use core::ops::{Mul, MulAssign}; use num_traits::{CheckedMul, One, Zero}; -impl Mul for Sign { - type Output = Sign; +impl Mul for Sign { + type Output = Self; #[inline] - fn mul(self, other: Sign) -> Sign { + fn mul(self, other: Self) -> Self { match (self, other) { (NoSign, _) | (_, NoSign) => NoSign, (Plus, Plus) | (Minus, Minus) => Plus, @@ -72,11 +72,11 @@ forward_all_scalar_binop_to_val_val_commutative!(impl Mul for BigInt, mul); forward_all_scalar_binop_to_val_val_commutative!(impl Mul for BigInt, mul); impl Mul for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn mul(self, other: u32) -> BigInt { - BigInt::from_biguint(self.sign, self.data * other) + fn mul(self, other: u32) -> Self { + Self::from_biguint(self.sign, self.data * other) } } @@ -91,11 +91,11 @@ impl MulAssign for BigInt { } impl Mul for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn mul(self, other: u64) -> BigInt { - BigInt::from_biguint(self.sign, self.data * other) + fn mul(self, other: u64) -> Self { + Self::from_biguint(self.sign, self.data * other) } } @@ -110,11 +110,11 @@ impl MulAssign for BigInt { } impl Mul for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn mul(self, other: u128) -> BigInt { - BigInt::from_biguint(self.sign, self.data * other) + fn mul(self, other: u128) -> Self { + Self::from_biguint(self.sign, self.data * other) } } @@ -133,10 +133,10 @@ forward_all_scalar_binop_to_val_val_commutative!(impl Mul for BigInt, mul); forward_all_scalar_binop_to_val_val_commutative!(impl Mul for BigInt, mul); impl Mul for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn mul(self, other: i32) -> BigInt { + fn mul(self, other: i32) -> Self { match other.checked_uabs() { Positive(u) => self * u, Negative(u) => -self * u, @@ -158,10 +158,10 @@ impl MulAssign for BigInt { } impl Mul for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn mul(self, other: i64) -> BigInt { + fn mul(self, other: i64) -> Self { match other.checked_uabs() { Positive(u) => self * u, Negative(u) => -self * u, @@ -183,10 +183,10 @@ impl MulAssign for BigInt { } impl Mul for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn mul(self, other: i128) -> BigInt { + fn mul(self, other: i128) -> Self { match other.checked_uabs() { Positive(u) => self * u, Negative(u) => -self * u, @@ -209,7 +209,7 @@ impl MulAssign for BigInt { impl CheckedMul for BigInt { #[inline] - fn checked_mul(&self, v: &BigInt) -> Option { + fn checked_mul(&self, v: &Self) -> Option { Some(self.mul(v)) } } diff --git a/src/bigint/subtraction.rs b/src/bigint/subtraction.rs index ef778549..cacb4fcc 100644 --- a/src/bigint/subtraction.rs +++ b/src/bigint/subtraction.rs @@ -54,27 +54,27 @@ impl Sub for &BigInt { } } -impl Sub<&BigInt> for BigInt { - type Output = BigInt; +impl Sub<&Self> for BigInt { + type Output = Self; #[inline] - fn sub(self, other: &BigInt) -> BigInt { + fn sub(self, other: &Self) -> Self { bigint_sub!(self, self, self.data, other, other.clone(), &other.data) } } -impl Sub for BigInt { - type Output = BigInt; +impl Sub for BigInt { + type Output = Self; #[inline] - fn sub(self, other: BigInt) -> BigInt { + fn sub(self, other: Self) -> Self { bigint_sub!(self, self, self.data, other, other, other.data) } } -impl SubAssign<&BigInt> for BigInt { +impl SubAssign<&Self> for BigInt { #[inline] - fn sub_assign(&mut self, other: &BigInt) { + fn sub_assign(&mut self, other: &Self) { let n = mem::replace(self, Self::ZERO); *self = n - other; } @@ -88,17 +88,17 @@ forward_all_scalar_binop_to_val_val!(impl Sub for BigInt, sub); forward_all_scalar_binop_to_val_val!(impl Sub for BigInt, sub); impl Sub for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn sub(self, other: u32) -> BigInt { + fn sub(self, other: u32) -> Self { match self.sign { - NoSign => -BigInt::from(other), - Minus => -BigInt::from(self.data + other), + NoSign => -Self::from(other), + Minus => -Self::from(self.data + other), Plus => match self.data.cmp(&From::from(other)) { Equal => Self::ZERO, - Greater => BigInt::from(self.data - other), - Less => -BigInt::from(other - self.data), + Greater => Self::from(self.data - other), + Less => -Self::from(other - self.data), }, } } @@ -139,17 +139,17 @@ impl Sub for u128 { } impl Sub for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn sub(self, other: u64) -> BigInt { + fn sub(self, other: u64) -> Self { match self.sign { - NoSign => -BigInt::from(other), - Minus => -BigInt::from(self.data + other), + NoSign => -Self::from(other), + Minus => -Self::from(self.data + other), Plus => match self.data.cmp(&From::from(other)) { Equal => Self::ZERO, - Greater => BigInt::from(self.data - other), - Less => -BigInt::from(other - self.data), + Greater => Self::from(self.data - other), + Less => -Self::from(other - self.data), }, } } @@ -164,17 +164,17 @@ impl SubAssign for BigInt { } impl Sub for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn sub(self, other: u128) -> BigInt { + fn sub(self, other: u128) -> Self { match self.sign { - NoSign => -BigInt::from(other), - Minus => -BigInt::from(self.data + other), + NoSign => -Self::from(other), + Minus => -Self::from(self.data + other), Plus => match self.data.cmp(&From::from(other)) { Equal => Self::ZERO, - Greater => BigInt::from(self.data - other), - Less => -BigInt::from(other - self.data), + Greater => Self::from(self.data - other), + Less => -Self::from(other - self.data), }, } } @@ -193,10 +193,10 @@ forward_all_scalar_binop_to_val_val!(impl Sub for BigInt, sub); forward_all_scalar_binop_to_val_val!(impl Sub for BigInt, sub); impl Sub for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn sub(self, other: i32) -> BigInt { + fn sub(self, other: i32) -> Self { match other.checked_uabs() { Positive(u) => self - u, Negative(u) => self + u, @@ -226,10 +226,10 @@ impl Sub for i32 { } impl Sub for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn sub(self, other: i64) -> BigInt { + fn sub(self, other: i64) -> Self { match other.checked_uabs() { Positive(u) => self - u, Negative(u) => self + u, @@ -259,10 +259,10 @@ impl Sub for i64 { } impl Sub for BigInt { - type Output = BigInt; + type Output = Self; #[inline] - fn sub(self, other: i128) -> BigInt { + fn sub(self, other: i128) -> Self { match other.checked_uabs() { Positive(u) => self - u, Negative(u) => self + u, @@ -294,7 +294,7 @@ impl Sub for i128 { impl CheckedSub for BigInt { #[inline] - fn checked_sub(&self, v: &BigInt) -> Option { + fn checked_sub(&self, v: &Self) -> Option { Some(self.sub(v)) } } diff --git a/src/biguint.rs b/src/biguint.rs index 196fa323..8dcfe91e 100644 --- a/src/biguint.rs +++ b/src/biguint.rs @@ -40,7 +40,7 @@ pub struct BigUint { impl Clone for BigUint { #[inline] fn clone(&self) -> Self { - BigUint { + Self { data: self.data.clone(), } } @@ -61,7 +61,7 @@ impl hash::Hash for BigUint { impl PartialEq for BigUint { #[inline] - fn eq(&self, other: &BigUint) -> bool { + fn eq(&self, other: &Self) -> bool { debug_assert!(self.data.last() != Some(&0)); debug_assert!(other.data.last() != Some(&0)); self.data == other.data @@ -71,14 +71,14 @@ impl Eq for BigUint {} impl PartialOrd for BigUint { #[inline] - fn partial_cmp(&self, other: &BigUint) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for BigUint { #[inline] - fn cmp(&self, other: &BigUint) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { cmp_slice(&self.data[..], &other.data[..]) } } @@ -96,7 +96,7 @@ fn cmp_slice(a: &[BigDigit], b: &[BigDigit]) -> Ordering { impl Default for BigUint { #[inline] - fn default() -> BigUint { + fn default() -> Self { Self::ZERO } } @@ -141,7 +141,7 @@ impl fmt::Octal for BigUint { impl Zero for BigUint { #[inline] - fn zero() -> BigUint { + fn zero() -> Self { Self::ZERO } @@ -163,8 +163,8 @@ impl ConstZero for BigUint { impl One for BigUint { #[inline] - fn one() -> BigUint { - BigUint { data: vec![1] } + fn one() -> Self { + Self { data: vec![1] } } #[inline] @@ -183,29 +183,29 @@ impl Unsigned for BigUint {} impl Integer for BigUint { #[inline] - fn div_rem(&self, other: &BigUint) -> (BigUint, BigUint) { + fn div_rem(&self, other: &Self) -> (Self, Self) { division::div_rem_ref(self, other) } #[inline] - fn div_floor(&self, other: &BigUint) -> BigUint { + fn div_floor(&self, other: &Self) -> Self { let (d, _) = division::div_rem_ref(self, other); d } #[inline] - fn mod_floor(&self, other: &BigUint) -> BigUint { + fn mod_floor(&self, other: &Self) -> Self { let (_, m) = division::div_rem_ref(self, other); m } #[inline] - fn div_mod_floor(&self, other: &BigUint) -> (BigUint, BigUint) { + fn div_mod_floor(&self, other: &Self) -> (Self, Self) { division::div_rem_ref(self, other) } #[inline] - fn div_ceil(&self, other: &BigUint) -> BigUint { + fn div_ceil(&self, other: &Self) -> Self { let (d, m) = division::div_rem_ref(self, other); if m.is_zero() { d @@ -254,7 +254,7 @@ impl Integer for BigUint { /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. #[inline] - fn lcm(&self, other: &BigUint) -> BigUint { + fn lcm(&self, other: &Self) -> Self { if self.is_zero() && other.is_zero() { Self::ZERO } else { @@ -277,13 +277,13 @@ impl Integer for BigUint { /// Deprecated, use `is_multiple_of` instead. #[inline] - fn divides(&self, other: &BigUint) -> bool { + fn divides(&self, other: &Self) -> bool { self.is_multiple_of(other) } /// Returns `true` if the number is a multiple of `other`. #[inline] - fn is_multiple_of(&self, other: &BigUint) -> bool { + fn is_multiple_of(&self, other: &Self) -> bool { if other.is_zero() { return self.is_zero(); } @@ -385,7 +385,7 @@ impl Roots for BigUint { let bits = self.bits(); let n64 = u64::from(n); if bits <= n64 { - return BigUint::one(); + return Self::one(); } // If we fit in `u64`, compute the root that way. @@ -401,7 +401,7 @@ impl Roots for BigUint { use num_traits::FromPrimitive; // We fit in `f64` (lossy), so get a better initial guess from that. - BigUint::from_f64((f.ln() / f64::from(n)).exp()).unwrap() + Self::from_f64((f.ln() / f64::from(n)).exp()).unwrap() } _ => { // Try to guess by scaling down such that it does fit in `f64`. @@ -412,7 +412,7 @@ impl Roots for BigUint { if scale < bits && bits - scale > n64 { (self >> scale).nth_root(n) << root_scale } else { - BigUint::one() << max_bits + Self::one() << max_bits } } }; @@ -449,7 +449,7 @@ impl Roots for BigUint { use num_traits::FromPrimitive; // We fit in `f64` (lossy), so get a better initial guess from that. - BigUint::from_f64(f.sqrt()).unwrap() + Self::from_f64(f.sqrt()).unwrap() } _ => { // Try to guess by scaling down such that it does fit in `f64`. @@ -490,7 +490,7 @@ impl Roots for BigUint { use num_traits::FromPrimitive; // We fit in `f64` (lossy), so get a better initial guess from that. - BigUint::from_f64(f.cbrt()).unwrap() + Self::from_f64(f.cbrt()).unwrap() } _ => { // Try to guess by scaling down such that it does fit in `f64`. @@ -529,13 +529,13 @@ pub(crate) fn biguint_from_vec(digits: Vec) -> BigUint { impl BigUint { /// A constant `BigUint` with value 0, useful for static initialization. - pub const ZERO: Self = BigUint { data: Vec::new() }; + pub const ZERO: Self = Self { data: Vec::new() }; /// Creates and initializes a [`BigUint`]. /// /// The base 232 digits are ordered least significant digit first. #[inline] - pub fn new(digits: Vec) -> BigUint { + pub fn new(digits: Vec) -> Self { let mut big = Self::ZERO; cfg_digit_expr!( @@ -553,7 +553,7 @@ impl BigUint { /// /// The base 232 digits are ordered least significant digit first. #[inline] - pub fn from_slice(slice: &[u32]) -> BigUint { + pub fn from_slice(slice: &[u32]) -> Self { let mut big = Self::ZERO; big.assign_from_slice(slice); big @@ -593,13 +593,13 @@ impl BigUint { /// BigUint::parse_bytes(b"22405534230753963835153736737", 10).unwrap()); /// ``` #[inline] - pub fn from_bytes_be(bytes: &[u8]) -> BigUint { + pub fn from_bytes_be(bytes: &[u8]) -> Self { if bytes.is_empty() { Self::ZERO } else { let mut v = bytes.to_vec(); v.reverse(); - BigUint::from_bytes_le(&v) + Self::from_bytes_le(&v) } } @@ -607,7 +607,7 @@ impl BigUint { /// /// The bytes are in little-endian byte order. #[inline] - pub fn from_bytes_le(bytes: &[u8]) -> BigUint { + pub fn from_bytes_le(bytes: &[u8]) -> Self { if bytes.is_empty() { Self::ZERO } else { @@ -632,9 +632,9 @@ impl BigUint { /// assert_eq!(BigUint::parse_bytes(b"G", 16), None); /// ``` #[inline] - pub fn parse_bytes(buf: &[u8], radix: u32) -> Option { + pub fn parse_bytes(buf: &[u8], radix: u32) -> Option { let s = str::from_utf8(buf).ok()?; - BigUint::from_str_radix(s, radix).ok() + Self::from_str_radix(s, radix).ok() } /// Creates and initializes a [`BigUint`]. Each `u8` of the input slice is @@ -653,7 +653,7 @@ impl BigUint { /// let a = BigUint::from_radix_be(inbase190, 190).unwrap(); /// assert_eq!(a.to_radix_be(190), inbase190); /// ``` - pub fn from_radix_be(buf: &[u8], radix: u32) -> Option { + pub fn from_radix_be(buf: &[u8], radix: u32) -> Option { convert::from_radix_be(buf, radix) } @@ -673,7 +673,7 @@ impl BigUint { /// let a = BigUint::from_radix_be(inbase190, 190).unwrap(); /// assert_eq!(a.to_radix_be(190), inbase190); /// ``` - pub fn from_radix_le(buf: &[u8], radix: u32) -> Option { + pub fn from_radix_le(buf: &[u8], radix: u32) -> Option { convert::from_radix_le(buf, radix) } @@ -870,7 +870,7 @@ impl BigUint { /// Returns a normalized [`BigUint`]. #[inline] - fn normalized(mut self) -> BigUint { + fn normalized(mut self) -> Self { self.normalize(); self } diff --git a/src/biguint/addition.rs b/src/biguint/addition.rs index b6711314..8a2f282a 100644 --- a/src/biguint/addition.rs +++ b/src/biguint/addition.rs @@ -88,17 +88,17 @@ pub(super) fn add2(a: &mut [BigDigit], b: &[BigDigit]) { forward_all_binop_to_val_ref_commutative!(impl Add for BigUint, add); forward_val_assign!(impl AddAssign for BigUint, add_assign); -impl Add<&BigUint> for BigUint { - type Output = BigUint; +impl Add<&Self> for BigUint { + type Output = Self; - fn add(mut self, other: &BigUint) -> BigUint { + fn add(mut self, other: &Self) -> Self { self += other; self } } -impl AddAssign<&BigUint> for BigUint { +impl AddAssign<&Self> for BigUint { #[inline] - fn add_assign(&mut self, other: &BigUint) { + fn add_assign(&mut self, other: &Self) { let self_len = self.data.len(); let carry = if self_len < other.data.len() { let lo_carry = __add2(&mut self.data[..], &other.data[..self_len]); @@ -120,10 +120,10 @@ forward_all_scalar_binop_to_val_val_commutative!(impl Add for BigUint, add) forward_all_scalar_binop_to_val_val_commutative!(impl Add for BigUint, add); impl Add for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn add(mut self, other: u32) -> BigUint { + fn add(mut self, other: u32) -> Self { self += other; self } @@ -146,10 +146,10 @@ impl AddAssign for BigUint { } impl Add for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn add(mut self, other: u64) -> BigUint { + fn add(mut self, other: u64) -> Self { self += other; self } @@ -191,10 +191,10 @@ impl AddAssign for BigUint { } impl Add for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn add(mut self, other: u128) -> BigUint { + fn add(mut self, other: u128) -> Self { self += other; self } @@ -248,7 +248,7 @@ impl AddAssign for BigUint { impl CheckedAdd for BigUint { #[inline] - fn checked_add(&self, v: &BigUint) -> Option { + fn checked_add(&self, v: &Self) -> Option { Some(self.add(v)) } } diff --git a/src/biguint/bits.rs b/src/biguint/bits.rs index 42d7ec0c..99f58727 100644 --- a/src/biguint/bits.rs +++ b/src/biguint/bits.rs @@ -23,18 +23,18 @@ impl BitAnd<&BigUint> for &BigUint { forward_val_assign!(impl BitAndAssign for BigUint, bitand_assign); -impl BitAnd<&BigUint> for BigUint { - type Output = BigUint; +impl BitAnd<&Self> for BigUint { + type Output = Self; #[inline] - fn bitand(mut self, other: &BigUint) -> BigUint { + fn bitand(mut self, other: &Self) -> Self { self &= other; self } } -impl BitAndAssign<&BigUint> for BigUint { +impl BitAndAssign<&Self> for BigUint { #[inline] - fn bitand_assign(&mut self, other: &BigUint) { + fn bitand_assign(&mut self, other: &Self) { for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) { *ai &= bi; } @@ -46,17 +46,17 @@ impl BitAndAssign<&BigUint> for BigUint { forward_all_binop_to_val_ref_commutative!(impl BitOr for BigUint, bitor); forward_val_assign!(impl BitOrAssign for BigUint, bitor_assign); -impl BitOr<&BigUint> for BigUint { - type Output = BigUint; +impl BitOr<&Self> for BigUint { + type Output = Self; - fn bitor(mut self, other: &BigUint) -> BigUint { + fn bitor(mut self, other: &Self) -> Self { self |= other; self } } -impl BitOrAssign<&BigUint> for BigUint { +impl BitOrAssign<&Self> for BigUint { #[inline] - fn bitor_assign(&mut self, other: &BigUint) { + fn bitor_assign(&mut self, other: &Self) { for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) { *ai |= bi; } @@ -70,17 +70,17 @@ impl BitOrAssign<&BigUint> for BigUint { forward_all_binop_to_val_ref_commutative!(impl BitXor for BigUint, bitxor); forward_val_assign!(impl BitXorAssign for BigUint, bitxor_assign); -impl BitXor<&BigUint> for BigUint { - type Output = BigUint; +impl BitXor<&Self> for BigUint { + type Output = Self; - fn bitxor(mut self, other: &BigUint) -> BigUint { + fn bitxor(mut self, other: &Self) -> Self { self ^= other; self } } -impl BitXorAssign<&BigUint> for BigUint { +impl BitXorAssign<&Self> for BigUint { #[inline] - fn bitxor_assign(&mut self, other: &BigUint) { + fn bitxor_assign(&mut self, other: &Self) { for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) { *ai ^= bi; } diff --git a/src/biguint/convert.rs b/src/biguint/convert.rs index 3daf3dcb..b2d07496 100644 --- a/src/biguint/convert.rs +++ b/src/biguint/convert.rs @@ -34,8 +34,8 @@ impl FromStr for BigUint { type Err = ParseBigIntError; #[inline] - fn from_str(s: &str) -> Result { - BigUint::from_str_radix(s, 10) + fn from_str(s: &str) -> Result { + Self::from_str_radix(s, 10) } } @@ -218,7 +218,7 @@ impl Num for BigUint { type FromStrRadixErr = ParseBigIntError; /// Creates and initializes a `BigUint`. - fn from_str_radix(s: &str, radix: u32) -> Result { + fn from_str_radix(s: &str, radix: u32) -> Result { assert!(2 <= radix && radix <= 36, "The radix must be within 2...36"); let mut s = s; if let Some(tail) = s.strip_prefix('+') { @@ -429,35 +429,35 @@ impl_try_from_biguint!(i128, ToPrimitive::to_i128); impl FromPrimitive for BigUint { #[inline] - fn from_i64(n: i64) -> Option { + fn from_i64(n: i64) -> Option { if n >= 0 { - Some(BigUint::from(n as u64)) + Some(Self::from(n as u64)) } else { None } } #[inline] - fn from_i128(n: i128) -> Option { + fn from_i128(n: i128) -> Option { if n >= 0 { - Some(BigUint::from(n as u128)) + Some(Self::from(n as u128)) } else { None } } #[inline] - fn from_u64(n: u64) -> Option { - Some(BigUint::from(n)) + fn from_u64(n: u64) -> Option { + Some(Self::from(n)) } #[inline] - fn from_u128(n: u128) -> Option { - Some(BigUint::from(n)) + fn from_u128(n: u128) -> Option { + Some(Self::from(n)) } #[inline] - fn from_f64(mut n: f64) -> Option { + fn from_f64(mut n: f64) -> Option { // handle NAN, INFINITY, NEG_INFINITY if !n.is_finite() { return None; @@ -477,7 +477,7 @@ impl FromPrimitive for BigUint { return None; } - let mut ret = BigUint::from(mantissa); + let mut ret = Self::from(mantissa); match exponent.cmp(&0) { Greater => ret <<= exponent as usize, Equal => {} @@ -490,7 +490,7 @@ impl FromPrimitive for BigUint { impl From for BigUint { #[inline] fn from(mut n: u64) -> Self { - let mut ret: BigUint = Self::ZERO; + let mut ret: Self = Self::ZERO; while n != 0 { ret.data.push(n as BigDigit); @@ -505,7 +505,7 @@ impl From for BigUint { impl From for BigUint { #[inline] fn from(mut n: u128) -> Self { - let mut ret: BigUint = Self::ZERO; + let mut ret: Self = Self::ZERO; while n != 0 { ret.data.push(n as BigDigit); diff --git a/src/biguint/division.rs b/src/biguint/division.rs index 3dfb0bbb..3dba3a71 100644 --- a/src/biguint/division.rs +++ b/src/biguint/division.rs @@ -346,11 +346,11 @@ forward_val_ref_binop!(impl Div for BigUint, div); forward_ref_val_binop!(impl Div for BigUint, div); forward_val_assign!(impl DivAssign for BigUint, div_assign); -impl Div for BigUint { - type Output = BigUint; +impl Div for BigUint { + type Output = Self; #[inline] - fn div(self, other: BigUint) -> BigUint { + fn div(self, other: Self) -> Self { let (q, _) = div_rem(self, other); q } @@ -365,9 +365,9 @@ impl Div<&BigUint> for &BigUint { q } } -impl DivAssign<&BigUint> for BigUint { +impl DivAssign<&Self> for BigUint { #[inline] - fn div_assign(&mut self, other: &BigUint) { + fn div_assign(&mut self, other: &Self) { *self = &*self / other; } } @@ -379,10 +379,10 @@ forward_all_scalar_binop_to_val_val!(impl Div for BigUint, div); forward_all_scalar_binop_to_val_val!(impl Div for BigUint, div); impl Div for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn div(self, other: u32) -> BigUint { + fn div(self, other: u32) -> Self { let (q, _) = div_rem_digit(self, other as BigDigit); q } @@ -408,10 +408,10 @@ impl Div for u32 { } impl Div for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn div(self, other: u64) -> BigUint { + fn div(self, other: u64) -> Self { let (q, _) = div_rem(self, From::from(other)); q } @@ -451,10 +451,10 @@ impl Div for u64 { } impl Div for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn div(self, other: u128) -> BigUint { + fn div(self, other: u128) -> Self { let (q, _) = div_rem(self, From::from(other)); q } @@ -492,7 +492,7 @@ impl Div for u128 { fn div(self, other: BigUint) -> BigUint { match other.data.len() { 0 => panic!("attempt to divide by zero"), - 1 => From::from(self / other.data[0] as u128), + 1 => From::from(self / other.data[0] as Self), 2 => From::from(self / big_digit::to_doublebigdigit(other.data[1], other.data[0])), _ => BigUint::ZERO, } @@ -504,11 +504,11 @@ forward_val_ref_binop!(impl Rem for BigUint, rem); forward_ref_val_binop!(impl Rem for BigUint, rem); forward_val_assign!(impl RemAssign for BigUint, rem_assign); -impl Rem for BigUint { - type Output = BigUint; +impl Rem for BigUint { + type Output = Self; #[inline] - fn rem(self, other: BigUint) -> BigUint { + fn rem(self, other: Self) -> Self { if let Some(other) = other.to_u32() { &self % other } else { @@ -531,9 +531,9 @@ impl Rem<&BigUint> for &BigUint { } } } -impl RemAssign<&BigUint> for BigUint { +impl RemAssign<&Self> for BigUint { #[inline] - fn rem_assign(&mut self, other: &BigUint) { + fn rem_assign(&mut self, other: &Self) { *self = &*self % other; } } @@ -600,10 +600,10 @@ impl_rem_assign_scalar!(i16, to_i16); impl_rem_assign_scalar!(i8, to_i8); impl Rem for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn rem(self, other: u64) -> BigUint { + fn rem(self, other: u64) -> Self { let (_, r) = div_rem(self, From::from(other)); r } @@ -626,10 +626,10 @@ impl Rem for u64 { } impl Rem for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn rem(self, other: u128) -> BigUint { + fn rem(self, other: u128) -> Self { let (_, r) = div_rem(self, From::from(other)); r } @@ -654,7 +654,7 @@ impl Rem for u128 { impl CheckedDiv for BigUint { #[inline] - fn checked_div(&self, v: &BigUint) -> Option { + fn checked_div(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -664,7 +664,7 @@ impl CheckedDiv for BigUint { impl CheckedEuclid for BigUint { #[inline] - fn checked_div_euclid(&self, v: &BigUint) -> Option { + fn checked_div_euclid(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -672,7 +672,7 @@ impl CheckedEuclid for BigUint { } #[inline] - fn checked_rem_euclid(&self, v: &BigUint) -> Option { + fn checked_rem_euclid(&self, v: &Self) -> Option { if v.is_zero() { return None; } @@ -686,13 +686,13 @@ impl CheckedEuclid for BigUint { impl Euclid for BigUint { #[inline] - fn div_euclid(&self, v: &BigUint) -> BigUint { + fn div_euclid(&self, v: &Self) -> Self { // trivially same as regular division self / v } #[inline] - fn rem_euclid(&self, v: &BigUint) -> BigUint { + fn rem_euclid(&self, v: &Self) -> Self { // trivially same as regular remainder self % v } diff --git a/src/biguint/monty.rs b/src/biguint/monty.rs index a0b26b54..9370fa51 100644 --- a/src/biguint/monty.rs +++ b/src/biguint/monty.rs @@ -31,7 +31,7 @@ fn inv_mod_alt(b: BigDigit) -> BigDigit { impl MontyReducer { fn new(n: &BigUint) -> Self { let n0inv = inv_mod_alt(n.data[0]); - MontyReducer { n0inv } + Self { n0inv } } } @@ -50,11 +50,7 @@ fn montgomery(x: &BigUint, y: &BigUint, m: &BigUint, k: BigDigit, n: usize) -> B // or else the result will not be properly reduced. assert!( x.data.len() == n && y.data.len() == n && m.data.len() == n, - "{:?} {:?} {:?} {}", - x, - y, - m, - n + "{x:?} {y:?} {m:?} {n}" ); let mut z = BigUint::ZERO; diff --git a/src/biguint/multiplication.rs b/src/biguint/multiplication.rs index e9d21384..83cdfccf 100644 --- a/src/biguint/multiplication.rs +++ b/src/biguint/multiplication.rs @@ -517,10 +517,10 @@ forward_all_scalar_binop_to_val_val_commutative!(impl Mul for BigUint, mul) forward_all_scalar_binop_to_val_val_commutative!(impl Mul for BigUint, mul); impl Mul for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn mul(mut self, other: u32) -> BigUint { + fn mul(mut self, other: u32) -> Self { self *= other; self } @@ -533,10 +533,10 @@ impl MulAssign for BigUint { } impl Mul for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn mul(mut self, other: u64) -> BigUint { + fn mul(mut self, other: u64) -> Self { self *= other; self } @@ -561,10 +561,10 @@ impl MulAssign for BigUint { } impl Mul for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn mul(mut self, other: u128) -> BigUint { + fn mul(mut self, other: u128) -> Self { self *= other; self } @@ -599,7 +599,7 @@ impl MulAssign for BigUint { impl CheckedMul for BigUint { #[inline] - fn checked_mul(&self, v: &BigUint) -> Option { + fn checked_mul(&self, v: &Self) -> Option { Some(self.mul(v)) } } diff --git a/src/biguint/power.rs b/src/biguint/power.rs index fa1c4926..d99828ef 100644 --- a/src/biguint/power.rs +++ b/src/biguint/power.rs @@ -6,13 +6,13 @@ use crate::big_digit::{self, BigDigit}; use num_integer::Integer; use num_traits::{One, Pow, ToPrimitive, Zero}; -impl Pow<&BigUint> for BigUint { - type Output = BigUint; +impl Pow<&Self> for BigUint { + type Output = Self; #[inline] - fn pow(self, exp: &BigUint) -> BigUint { + fn pow(self, exp: &Self) -> Self { if self.is_one() || exp.is_zero() { - BigUint::one() + Self::one() } else if self.is_zero() { Self::ZERO } else if let Some(exp) = exp.to_u64() { @@ -27,11 +27,11 @@ impl Pow<&BigUint> for BigUint { } } -impl Pow for BigUint { - type Output = BigUint; +impl Pow for BigUint { + type Output = Self; #[inline] - fn pow(self, exp: BigUint) -> BigUint { + fn pow(self, exp: Self) -> Self { Pow::pow(self, &exp) } } diff --git a/src/biguint/subtraction.rs b/src/biguint/subtraction.rs index 47a5015f..5a8a0c39 100644 --- a/src/biguint/subtraction.rs +++ b/src/biguint/subtraction.rs @@ -108,16 +108,16 @@ forward_val_val_binop!(impl Sub for BigUint, sub); forward_ref_ref_binop!(impl Sub for BigUint, sub); forward_val_assign!(impl SubAssign for BigUint, sub_assign); -impl Sub<&BigUint> for BigUint { - type Output = BigUint; +impl Sub<&Self> for BigUint { + type Output = Self; - fn sub(mut self, other: &BigUint) -> BigUint { + fn sub(mut self, other: &Self) -> Self { self -= other; self } } -impl SubAssign<&BigUint> for BigUint { - fn sub_assign(&mut self, other: &BigUint) { +impl SubAssign<&Self> for BigUint { + fn sub_assign(&mut self, other: &Self) { sub2(&mut self.data[..], &other.data[..]); self.normalize(); } @@ -148,10 +148,10 @@ forward_all_scalar_binop_to_val_val!(impl Sub for BigUint, sub); forward_all_scalar_binop_to_val_val!(impl Sub for BigUint, sub); impl Sub for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn sub(mut self, other: u32) -> BigUint { + fn sub(mut self, other: u32) -> Self { self -= other; self } @@ -191,10 +191,10 @@ impl Sub for u32 { } impl Sub for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn sub(mut self, other: u64) -> BigUint { + fn sub(mut self, other: u64) -> Self { self -= other; self } @@ -245,10 +245,10 @@ impl Sub for u64 { } impl Sub for BigUint { - type Output = BigUint; + type Output = Self; #[inline] - fn sub(mut self, other: u128) -> BigUint { + fn sub(mut self, other: u128) -> Self { self -= other; self } @@ -302,7 +302,7 @@ impl Sub for u128 { impl CheckedSub for BigUint { #[inline] - fn checked_sub(&self, v: &BigUint) -> Option { + fn checked_sub(&self, v: &Self) -> Option { match self.cmp(v) { Less => None, Equal => Some(Self::ZERO), diff --git a/src/lib.rs b/src/lib.rs index 6e47479d..fa770874 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,13 +146,13 @@ impl ParseBigIntError { } fn empty() -> Self { - ParseBigIntError { + Self { kind: BigIntErrorKind::Empty, } } fn invalid() -> Self { - ParseBigIntError { + Self { kind: BigIntErrorKind::InvalidDigit, } } @@ -180,7 +180,7 @@ pub struct TryFromBigIntError { impl TryFromBigIntError { fn new(original: T) -> Self { - TryFromBigIntError { original } + Self { original } } fn __description(&self) -> &str {