uucore: Fix new_without_default clippy lint

This commit is contained in:
Dorian Peron
2026-02-08 00:00:42 +01:00
committed by Dorian Péron
parent 690b149b4b
commit 4bb60ac754
2 changed files with 63 additions and 70 deletions
+25 -25
View File
@@ -324,31 +324,31 @@ impl SizedAlgoKind {
pub fn create_digest(&self) -> Box<dyn Digest + 'static> {
use ShaLength::*;
match self {
Self::Sysv => Box::new(SysV::new()),
Self::Bsd => Box::new(Bsd::new()),
Self::Crc => Box::new(Crc::new()),
Self::Crc32b => Box::new(CRC32B::new()),
Self::Md5 => Box::new(Md5::new()),
Self::Sm3 => Box::new(Sm3::new()),
Self::Sha1 => Box::new(Sha1::new()),
Self::Blake3 => Box::new(Blake3::new()),
Self::Sha2(Len224) => Box::new(Sha224::new()),
Self::Sha2(Len256) => Box::new(Sha256::new()),
Self::Sha2(Len384) => Box::new(Sha384::new()),
Self::Sha2(Len512) => Box::new(Sha512::new()),
Self::Sha3(Len224) => Box::new(Sha3_224::new()),
Self::Sha3(Len256) => Box::new(Sha3_256::new()),
Self::Sha3(Len384) => Box::new(Sha3_384::new()),
Self::Sha3(Len512) => Box::new(Sha3_512::new()),
Self::Blake2b(len_opt) => Box::new(Blake2b::with_output_bytes(
len_opt.unwrap_or(Blake2b::DEFAULT_BYTE_SIZE),
)),
Self::Shake128(len_opt) => Box::new(Shake128::with_output_bits(
len_opt.unwrap_or(Shake128::DEFAULT_BIT_SIZE),
)),
Self::Shake256(len_opt) => Box::new(Shake256::with_output_bits(
len_opt.unwrap_or(Shake256::DEFAULT_BIT_SIZE),
)),
Self::Sysv => Box::new(SysV::default()),
Self::Bsd => Box::new(Bsd::default()),
Self::Crc => Box::new(Crc::default()),
Self::Crc32b => Box::new(CRC32B::default()),
Self::Md5 => Box::new(Md5::default()),
Self::Sm3 => Box::new(Sm3::default()),
Self::Sha1 => Box::new(Sha1::default()),
Self::Blake3 => Box::new(Blake3::default()),
Self::Sha2(Len224) => Box::new(Sha224::default()),
Self::Sha2(Len256) => Box::new(Sha256::default()),
Self::Sha2(Len384) => Box::new(Sha384::default()),
Self::Sha2(Len512) => Box::new(Sha512::default()),
Self::Sha3(Len224) => Box::new(Sha3_224::default()),
Self::Sha3(Len256) => Box::new(Sha3_256::default()),
Self::Sha3(Len384) => Box::new(Sha3_384::default()),
Self::Sha3(Len512) => Box::new(Sha3_512::default()),
Self::Blake2b(len_opt) => {
Box::new(len_opt.map(Blake2b::with_output_bytes).unwrap_or_default())
}
Self::Shake128(len_opt) => {
Box::new(len_opt.map(Shake128::with_output_bits).unwrap_or_default())
}
Self::Shake256(len_opt) => {
Box::new(len_opt.map(Shake256::with_output_bits).unwrap_or_default())
}
}
}
+38 -45
View File
@@ -97,6 +97,12 @@ impl Blake2b {
}
}
impl Default for Blake2b {
fn default() -> Self {
Self::with_output_bytes(Self::DEFAULT_BYTE_SIZE)
}
}
impl Digest for Blake2b {
fn hash_update(&mut self, input: &[u8]) {
self.digest.update(input);
@@ -116,14 +122,9 @@ impl Digest for Blake2b {
}
}
#[derive(Default)]
pub struct Blake3(blake3::Hasher);
impl Blake3 {
pub fn new() -> Self {
Self(blake3::Hasher::new())
}
}
impl Digest for Blake3 {
fn hash_update(&mut self, input: &[u8]) {
self.0.update(input);
@@ -135,7 +136,7 @@ impl Digest for Blake3 {
}
fn reset(&mut self) {
*self = Self::new();
*self = Self::default();
}
fn output_bits(&self) -> usize {
@@ -143,14 +144,9 @@ impl Digest for Blake3 {
}
}
#[derive(Default)]
pub struct Sm3(sm3::Sm3);
impl Sm3 {
pub fn new() -> Self {
Self(<sm3::Sm3 as sm3::Digest>::new())
}
}
impl Digest for Sm3 {
fn hash_update(&mut self, input: &[u8]) {
<sm3::Sm3 as sm3::Digest>::update(&mut self.0, input);
@@ -161,7 +157,7 @@ impl Digest for Sm3 {
}
fn reset(&mut self) {
*self = Self::new();
*self = Self::default();
}
fn output_bits(&self) -> usize {
@@ -188,8 +184,10 @@ impl Crc {
0, // Check value (not used)
)
}
}
pub fn new() -> Self {
impl Default for Crc {
fn default() -> Self {
Self {
digest: crc_fast::Digest::new_with_params(Self::get_posix_cksum_params()),
size: 0,
@@ -236,8 +234,8 @@ pub struct CRC32B {
digest: crc_fast::Digest,
}
impl CRC32B {
pub fn new() -> Self {
impl Default for CRC32B {
fn default() -> Self {
Self {
digest: crc_fast::Digest::new(crc_fast::CrcAlgorithm::Crc32IsoHdlc),
}
@@ -272,16 +270,11 @@ impl Digest for CRC32B {
}
}
#[derive(Default)]
pub struct Bsd {
state: u16,
}
impl Bsd {
pub fn new() -> Self {
Self { state: 0 }
}
}
impl Digest for Bsd {
fn hash_update(&mut self, input: &[u8]) {
for &byte in input {
@@ -301,7 +294,7 @@ impl Digest for Bsd {
}
fn reset(&mut self) {
*self = Self::new();
*self = Self::default();
}
fn output_bits(&self) -> usize {
@@ -309,16 +302,11 @@ impl Digest for Bsd {
}
}
#[derive(Default)]
pub struct SysV {
state: u32,
}
impl SysV {
pub fn new() -> Self {
Self { state: 0 }
}
}
impl Digest for SysV {
fn hash_update(&mut self, input: &[u8]) {
for &byte in input {
@@ -339,7 +327,7 @@ impl Digest for SysV {
}
fn reset(&mut self) {
*self = Self::new();
*self = Self::default();
}
fn output_bits(&self) -> usize {
@@ -350,8 +338,8 @@ impl Digest for SysV {
// Implements the Digest trait for sha2 / sha3 algorithms with fixed output
macro_rules! impl_digest_common {
($algo_type: ty, $size: literal) => {
impl $algo_type {
pub fn new() -> Self {
impl Default for $algo_type {
fn default() -> Self {
Self(Default::default())
}
}
@@ -365,7 +353,7 @@ macro_rules! impl_digest_common {
}
fn reset(&mut self) {
*self = Self::new();
*self = Self::default();
}
fn output_bits(&self) -> usize {
@@ -388,6 +376,11 @@ macro_rules! impl_digest_shake {
}
}
}
impl Default for $algo_type {
fn default() -> Self {
Self::with_output_bits(Self::DEFAULT_BIT_SIZE)
}
}
impl Digest for $algo_type {
fn hash_update(&mut self, input: &[u8]) {
digest::Update::update(&mut self.digest, input);
@@ -600,8 +593,8 @@ mod tests {
#[test]
fn test_crc_basic_functionality() {
// Test that our CRC implementation works with basic functionality
let mut crc1 = Crc::new();
let mut crc2 = Crc::new();
let mut crc1 = Crc::default();
let mut crc2 = Crc::default();
// Same input should give same output
crc1.hash_update(b"test");
@@ -617,7 +610,7 @@ mod tests {
#[test]
fn test_crc_digest_basic() {
let mut crc = Crc::new();
let mut crc = Crc::default();
// Test empty input
let mut output = [0u8; 8];
@@ -625,7 +618,7 @@ mod tests {
let empty_result = u64::from_ne_bytes(output);
// Reset and test with "test" string
let mut crc = Crc::new();
let mut crc = Crc::default();
crc.hash_update(b"test");
crc.hash_finalize(&mut output);
let test_result = u64::from_ne_bytes(output);
@@ -639,8 +632,8 @@ mod tests {
#[test]
fn test_crc_digest_incremental() {
let mut crc1 = Crc::new();
let mut crc2 = Crc::new();
let mut crc1 = Crc::default();
let mut crc2 = Crc::default();
// Test that processing in chunks gives same result as all at once
let data = b"Hello, World! This is a test string for CRC computation.";
@@ -665,13 +658,13 @@ mod tests {
// Test that our optimized slice-by-8 gives same results as byte-by-byte
let test_data = b"This is a longer test string to verify slice-by-8 optimization works correctly with various data sizes including remainders.";
let mut crc_optimized = Crc::new();
let mut crc_optimized = Crc::default();
crc_optimized.hash_update(test_data);
let mut output_opt = [0u8; 8];
crc_optimized.hash_finalize(&mut output_opt);
// Create a reference implementation using hash_update
let mut crc_reference = Crc::new();
let mut crc_reference = Crc::default();
for &byte in test_data {
crc_reference.hash_update(&[byte]);
}
@@ -692,7 +685,7 @@ mod tests {
];
for (input, expected) in test_cases {
let mut crc = Crc::new();
let mut crc = Crc::default();
crc.hash_update(input.as_bytes());
let mut output = [0u8; 8];
crc.hash_finalize(&mut output);
@@ -704,14 +697,14 @@ mod tests {
#[test]
fn test_crc_hash_update_edge_cases() {
let mut crc = Crc::new();
let mut crc = Crc::default();
// Test with data that's not a multiple of 8 bytes
let data7 = b"1234567"; // 7 bytes
crc.hash_update(data7);
let data9 = b"123456789"; // 9 bytes
let mut crc2 = Crc::new();
let mut crc2 = Crc::default();
crc2.hash_update(data9);
// Should not panic and should produce valid results