1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

Merge pull request #3378 from tertsdiepraam/rust-1.60-clippy

Clippy: Don't use `from_*` method names that take `self`
This commit is contained in:
Sylvestre Ledru 2022-04-12 09:26:28 +02:00 committed by GitHub
commit d8da10d4e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 20 deletions

View file

@ -69,7 +69,7 @@ pub(crate) fn test<A: Arithmetic + Basis>(m: A) -> Result {
continue; continue;
} }
let a = m.from_u64(_a); let a = m.to_mod(_a);
// x = a^r mod n // x = a^r mod n
let mut x = m.pow(a, r); let mut x = m.pow(a, r);

View file

@ -16,7 +16,7 @@ pub(crate) trait Arithmetic: Copy + Sized {
fn new(m: u64) -> Self; fn new(m: u64) -> Self;
fn modulus(&self) -> u64; fn modulus(&self) -> u64;
fn from_u64(&self, n: u64) -> Self::ModInt; fn to_mod(&self, n: u64) -> Self::ModInt;
fn to_u64(&self, n: Self::ModInt) -> u64; fn to_u64(&self, n: Self::ModInt) -> u64;
fn add(&self, a: Self::ModInt, b: Self::ModInt) -> Self::ModInt; fn add(&self, a: Self::ModInt, b: Self::ModInt) -> Self::ModInt;
fn mul(&self, a: Self::ModInt, b: Self::ModInt) -> Self::ModInt; fn mul(&self, a: Self::ModInt, b: Self::ModInt) -> Self::ModInt;
@ -47,13 +47,13 @@ pub(crate) trait Arithmetic: Copy + Sized {
} }
fn one(&self) -> Self::ModInt { fn one(&self) -> Self::ModInt {
self.from_u64(1) self.to_mod(1)
} }
fn minus_one(&self) -> Self::ModInt { fn minus_one(&self) -> Self::ModInt {
self.from_u64(self.modulus() - 1) self.to_mod(self.modulus() - 1)
} }
fn zero(&self) -> Self::ModInt { fn zero(&self) -> Self::ModInt {
self.from_u64(0) self.to_mod(0)
} }
} }
@ -113,7 +113,7 @@ impl<T: DoubleInt> Arithmetic for Montgomery<T> {
self.n.as_u64() self.n.as_u64()
} }
fn from_u64(&self, x: u64) -> Self::ModInt { fn to_mod(&self, x: u64) -> Self::ModInt {
// TODO: optimise! // TODO: optimise!
debug_assert!(x < self.n.as_u64()); debug_assert!(x < self.n.as_u64());
let r = T::from_double_width( let r = T::from_double_width(
@ -189,9 +189,9 @@ mod tests {
let n = 2 * n + 1; let n = 2 * n + 1;
let m = Montgomery::<A>::new(n); let m = Montgomery::<A>::new(n);
for x in 0..n { for x in 0..n {
let m_x = m.from_u64(x); let m_x = m.to_mod(x);
for y in 0..=x { for y in 0..=x {
let m_y = m.from_u64(y); let m_y = m.to_mod(y);
println!("{n:?}, {x:?}, {y:?}", n = n, x = x, y = y); println!("{n:?}, {x:?}, {y:?}", n = n, x = x, y = y);
assert_eq!((x + y) % n, m.to_u64(m.add(m_x, m_y))); assert_eq!((x + y) % n, m.to_u64(m.add(m_x, m_y)));
} }
@ -205,9 +205,9 @@ mod tests {
let n = 2 * n + 1; let n = 2 * n + 1;
let m = Montgomery::<A>::new(n); let m = Montgomery::<A>::new(n);
for x in 0..n { for x in 0..n {
let m_x = m.from_u64(x); let m_x = m.to_mod(x);
for y in 0..=x { for y in 0..=x {
let m_y = m.from_u64(y); let m_y = m.to_mod(y);
assert_eq!((x * y) % n, m.to_u64(m.mul(m_x, m_y))); assert_eq!((x * y) % n, m.to_u64(m.mul(m_x, m_y)));
} }
} }
@ -220,7 +220,7 @@ mod tests {
let n = 2 * n + 1; let n = 2 * n + 1;
let m = Montgomery::<A>::new(n); let m = Montgomery::<A>::new(n);
for x in 0..n { for x in 0..n {
let x_ = m.from_u64(x); let x_ = m.to_mod(x);
assert_eq!(x, m.to_u64(x_)); assert_eq!(x, m.to_u64(x_));
} }
} }

View file

@ -18,7 +18,7 @@ pub(crate) fn find_divisor<A: Arithmetic>(n: A) -> u64 {
let mut rand = { let mut rand = {
let range = Uniform::new(1, n.modulus()); let range = Uniform::new(1, n.modulus());
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap(); let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
move || n.from_u64(range.sample(&mut rng)) move || n.to_mod(range.sample(&mut rng))
}; };
let quadratic = |a, b| move |x| n.add(n.mul(a, n.mul(x, x)), b); let quadratic = |a, b| move |x| n.add(n.mul(a, n.mul(x, x)), b);

View file

@ -179,7 +179,7 @@ pub fn str_to_arrnum(src: &str, radix_def_src: &dyn RadixDef) -> Vec<u8> {
let mut intermed_in: Vec<u8> = Vec::new(); let mut intermed_in: Vec<u8> = Vec::new();
for c in src.chars() { for c in src.chars() {
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
match radix_def_src.from_char(c) { match radix_def_src.parse_char(c) {
Some(u) => { Some(u) => {
intermed_in.push(u); intermed_in.push(u);
} }
@ -193,7 +193,7 @@ pub fn arrnum_to_str(src: &[u8], radix_def_dest: &dyn RadixDef) -> String {
let mut str_out = String::new(); let mut str_out = String::new();
for u in src.iter() { for u in src.iter() {
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
match radix_def_dest.from_u8(*u) { match radix_def_dest.format_u8(*u) {
Some(c) => { Some(c) => {
str_out.push(c); str_out.push(c);
} }
@ -219,8 +219,8 @@ pub fn base_conv_str(
pub trait RadixDef { pub trait RadixDef {
fn get_max(&self) -> u8; fn get_max(&self) -> u8;
fn from_char(&self, x: char) -> Option<u8>; fn parse_char(&self, x: char) -> Option<u8>;
fn from_u8(&self, x: u8) -> Option<char>; fn format_u8(&self, x: u8) -> Option<char>;
} }
pub struct RadixTen; pub struct RadixTen;
@ -232,13 +232,13 @@ impl RadixDef for RadixTen {
fn get_max(&self) -> u8 { fn get_max(&self) -> u8 {
10 10
} }
fn from_char(&self, c: char) -> Option<u8> { fn parse_char(&self, c: char) -> Option<u8> {
match c { match c {
'0'..='9' => Some(c as u8 - ZERO_ASC), '0'..='9' => Some(c as u8 - ZERO_ASC),
_ => None, _ => None,
} }
} }
fn from_u8(&self, u: u8) -> Option<char> { fn format_u8(&self, u: u8) -> Option<char> {
match u { match u {
0..=9 => Some((ZERO_ASC + u) as char), 0..=9 => Some((ZERO_ASC + u) as char),
_ => None, _ => None,
@ -250,7 +250,7 @@ impl RadixDef for RadixHex {
fn get_max(&self) -> u8 { fn get_max(&self) -> u8 {
16 16
} }
fn from_char(&self, c: char) -> Option<u8> { fn parse_char(&self, c: char) -> Option<u8> {
match c { match c {
'0'..='9' => Some(c as u8 - ZERO_ASC), '0'..='9' => Some(c as u8 - ZERO_ASC),
'A'..='F' => Some(c as u8 + 10 - UPPER_A_ASC), 'A'..='F' => Some(c as u8 + 10 - UPPER_A_ASC),
@ -258,7 +258,7 @@ impl RadixDef for RadixHex {
_ => None, _ => None,
} }
} }
fn from_u8(&self, u: u8) -> Option<char> { fn format_u8(&self, u: u8) -> Option<char> {
match u { match u {
0..=9 => Some((ZERO_ASC + u) as char), 0..=9 => Some((ZERO_ASC + u) as char),
10..=15 => Some((UPPER_A_ASC + (u - 10)) as char), 10..=15 => Some((UPPER_A_ASC + (u - 10)) as char),