Skip to content

Commit d96f72e

Browse files
committed
uucore: Fix new_without_default clippy lint
1 parent dc09beb commit d96f72e

File tree

2 files changed

+63
-70
lines changed

2 files changed

+63
-70
lines changed

src/uucore/src/lib/features/checksum/mod.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -324,31 +324,31 @@ impl SizedAlgoKind {
324324
pub fn create_digest(&self) -> Box<dyn Digest + 'static> {
325325
use ShaLength::*;
326326
match self {
327-
Self::Sysv => Box::new(SysV::new()),
328-
Self::Bsd => Box::new(Bsd::new()),
329-
Self::Crc => Box::new(Crc::new()),
330-
Self::Crc32b => Box::new(CRC32B::new()),
331-
Self::Md5 => Box::new(Md5::new()),
332-
Self::Sm3 => Box::new(Sm3::new()),
333-
Self::Sha1 => Box::new(Sha1::new()),
334-
Self::Blake3 => Box::new(Blake3::new()),
335-
Self::Sha2(Len224) => Box::new(Sha224::new()),
336-
Self::Sha2(Len256) => Box::new(Sha256::new()),
337-
Self::Sha2(Len384) => Box::new(Sha384::new()),
338-
Self::Sha2(Len512) => Box::new(Sha512::new()),
339-
Self::Sha3(Len224) => Box::new(Sha3_224::new()),
340-
Self::Sha3(Len256) => Box::new(Sha3_256::new()),
341-
Self::Sha3(Len384) => Box::new(Sha3_384::new()),
342-
Self::Sha3(Len512) => Box::new(Sha3_512::new()),
343-
Self::Blake2b(len_opt) => Box::new(Blake2b::with_output_bytes(
344-
len_opt.unwrap_or(Blake2b::DEFAULT_BYTE_SIZE),
345-
)),
346-
Self::Shake128(len_opt) => Box::new(Shake128::with_output_bits(
347-
len_opt.unwrap_or(Shake128::DEFAULT_BIT_SIZE),
348-
)),
349-
Self::Shake256(len_opt) => Box::new(Shake256::with_output_bits(
350-
len_opt.unwrap_or(Shake256::DEFAULT_BIT_SIZE),
351-
)),
327+
Self::Sysv => Box::new(SysV::default()),
328+
Self::Bsd => Box::new(Bsd::default()),
329+
Self::Crc => Box::new(Crc::default()),
330+
Self::Crc32b => Box::new(CRC32B::default()),
331+
Self::Md5 => Box::new(Md5::default()),
332+
Self::Sm3 => Box::new(Sm3::default()),
333+
Self::Sha1 => Box::new(Sha1::default()),
334+
Self::Blake3 => Box::new(Blake3::default()),
335+
Self::Sha2(Len224) => Box::new(Sha224::default()),
336+
Self::Sha2(Len256) => Box::new(Sha256::default()),
337+
Self::Sha2(Len384) => Box::new(Sha384::default()),
338+
Self::Sha2(Len512) => Box::new(Sha512::default()),
339+
Self::Sha3(Len224) => Box::new(Sha3_224::default()),
340+
Self::Sha3(Len256) => Box::new(Sha3_256::default()),
341+
Self::Sha3(Len384) => Box::new(Sha3_384::default()),
342+
Self::Sha3(Len512) => Box::new(Sha3_512::default()),
343+
Self::Blake2b(len_opt) => {
344+
Box::new(len_opt.map(Blake2b::with_output_bytes).unwrap_or_default())
345+
}
346+
Self::Shake128(len_opt) => {
347+
Box::new(len_opt.map(Shake128::with_output_bits).unwrap_or_default())
348+
}
349+
Self::Shake256(len_opt) => {
350+
Box::new(len_opt.map(Shake256::with_output_bits).unwrap_or_default())
351+
}
352352
}
353353
}
354354

src/uucore/src/lib/features/sum.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ impl Blake2b {
9797
}
9898
}
9999

100+
impl Default for Blake2b {
101+
fn default() -> Self {
102+
Self::with_output_bytes(Self::DEFAULT_BYTE_SIZE)
103+
}
104+
}
105+
100106
impl Digest for Blake2b {
101107
fn hash_update(&mut self, input: &[u8]) {
102108
self.digest.update(input);
@@ -116,14 +122,9 @@ impl Digest for Blake2b {
116122
}
117123
}
118124

125+
#[derive(Default)]
119126
pub struct Blake3(blake3::Hasher);
120127

121-
impl Blake3 {
122-
pub fn new() -> Self {
123-
Self(blake3::Hasher::new())
124-
}
125-
}
126-
127128
impl Digest for Blake3 {
128129
fn hash_update(&mut self, input: &[u8]) {
129130
self.0.update(input);
@@ -135,22 +136,17 @@ impl Digest for Blake3 {
135136
}
136137

137138
fn reset(&mut self) {
138-
*self = Self::new();
139+
*self = Self::default();
139140
}
140141

141142
fn output_bits(&self) -> usize {
142143
256
143144
}
144145
}
145146

147+
#[derive(Default)]
146148
pub struct Sm3(sm3::Sm3);
147149

148-
impl Sm3 {
149-
pub fn new() -> Self {
150-
Self(<sm3::Sm3 as sm3::Digest>::new())
151-
}
152-
}
153-
154150
impl Digest for Sm3 {
155151
fn hash_update(&mut self, input: &[u8]) {
156152
<sm3::Sm3 as sm3::Digest>::update(&mut self.0, input);
@@ -161,7 +157,7 @@ impl Digest for Sm3 {
161157
}
162158

163159
fn reset(&mut self) {
164-
*self = Self::new();
160+
*self = Self::default();
165161
}
166162

167163
fn output_bits(&self) -> usize {
@@ -188,8 +184,10 @@ impl Crc {
188184
0, // Check value (not used)
189185
)
190186
}
187+
}
191188

192-
pub fn new() -> Self {
189+
impl Default for Crc {
190+
fn default() -> Self {
193191
Self {
194192
digest: crc_fast::Digest::new_with_params(Self::get_posix_cksum_params()),
195193
size: 0,
@@ -236,8 +234,8 @@ pub struct CRC32B {
236234
digest: crc_fast::Digest,
237235
}
238236

239-
impl CRC32B {
240-
pub fn new() -> Self {
237+
impl Default for CRC32B {
238+
fn default() -> Self {
241239
Self {
242240
digest: crc_fast::Digest::new(crc_fast::CrcAlgorithm::Crc32IsoHdlc),
243241
}
@@ -272,16 +270,11 @@ impl Digest for CRC32B {
272270
}
273271
}
274272

273+
#[derive(Default)]
275274
pub struct Bsd {
276275
state: u16,
277276
}
278277

279-
impl Bsd {
280-
pub fn new() -> Self {
281-
Self { state: 0 }
282-
}
283-
}
284-
285278
impl Digest for Bsd {
286279
fn hash_update(&mut self, input: &[u8]) {
287280
for &byte in input {
@@ -301,24 +294,19 @@ impl Digest for Bsd {
301294
}
302295

303296
fn reset(&mut self) {
304-
*self = Self::new();
297+
*self = Self::default();
305298
}
306299

307300
fn output_bits(&self) -> usize {
308301
128
309302
}
310303
}
311304

305+
#[derive(Default)]
312306
pub struct SysV {
313307
state: u32,
314308
}
315309

316-
impl SysV {
317-
pub fn new() -> Self {
318-
Self { state: 0 }
319-
}
320-
}
321-
322310
impl Digest for SysV {
323311
fn hash_update(&mut self, input: &[u8]) {
324312
for &byte in input {
@@ -339,7 +327,7 @@ impl Digest for SysV {
339327
}
340328

341329
fn reset(&mut self) {
342-
*self = Self::new();
330+
*self = Self::default();
343331
}
344332

345333
fn output_bits(&self) -> usize {
@@ -350,8 +338,8 @@ impl Digest for SysV {
350338
// Implements the Digest trait for sha2 / sha3 algorithms with fixed output
351339
macro_rules! impl_digest_common {
352340
($algo_type: ty, $size: literal) => {
353-
impl $algo_type {
354-
pub fn new() -> Self {
341+
impl Default for $algo_type {
342+
fn default() -> Self {
355343
Self(Default::default())
356344
}
357345
}
@@ -365,7 +353,7 @@ macro_rules! impl_digest_common {
365353
}
366354

367355
fn reset(&mut self) {
368-
*self = Self::new();
356+
*self = Self::default();
369357
}
370358

371359
fn output_bits(&self) -> usize {
@@ -388,6 +376,11 @@ macro_rules! impl_digest_shake {
388376
}
389377
}
390378
}
379+
impl Default for $algo_type {
380+
fn default() -> Self {
381+
Self::with_output_bits(Self::DEFAULT_BIT_SIZE)
382+
}
383+
}
391384
impl Digest for $algo_type {
392385
fn hash_update(&mut self, input: &[u8]) {
393386
digest::Update::update(&mut self.digest, input);
@@ -594,8 +587,8 @@ mod tests {
594587
#[test]
595588
fn test_crc_basic_functionality() {
596589
// Test that our CRC implementation works with basic functionality
597-
let mut crc1 = Crc::new();
598-
let mut crc2 = Crc::new();
590+
let mut crc1 = Crc::default();
591+
let mut crc2 = Crc::default();
599592

600593
// Same input should give same output
601594
crc1.hash_update(b"test");
@@ -611,15 +604,15 @@ mod tests {
611604

612605
#[test]
613606
fn test_crc_digest_basic() {
614-
let mut crc = Crc::new();
607+
let mut crc = Crc::default();
615608

616609
// Test empty input
617610
let mut output = [0u8; 8];
618611
crc.hash_finalize(&mut output);
619612
let empty_result = u64::from_ne_bytes(output);
620613

621614
// Reset and test with "test" string
622-
let mut crc = Crc::new();
615+
let mut crc = Crc::default();
623616
crc.hash_update(b"test");
624617
crc.hash_finalize(&mut output);
625618
let test_result = u64::from_ne_bytes(output);
@@ -633,8 +626,8 @@ mod tests {
633626

634627
#[test]
635628
fn test_crc_digest_incremental() {
636-
let mut crc1 = Crc::new();
637-
let mut crc2 = Crc::new();
629+
let mut crc1 = Crc::default();
630+
let mut crc2 = Crc::default();
638631

639632
// Test that processing in chunks gives same result as all at once
640633
let data = b"Hello, World! This is a test string for CRC computation.";
@@ -659,13 +652,13 @@ mod tests {
659652
// Test that our optimized slice-by-8 gives same results as byte-by-byte
660653
let test_data = b"This is a longer test string to verify slice-by-8 optimization works correctly with various data sizes including remainders.";
661654

662-
let mut crc_optimized = Crc::new();
655+
let mut crc_optimized = Crc::default();
663656
crc_optimized.hash_update(test_data);
664657
let mut output_opt = [0u8; 8];
665658
crc_optimized.hash_finalize(&mut output_opt);
666659

667660
// Create a reference implementation using hash_update
668-
let mut crc_reference = Crc::new();
661+
let mut crc_reference = Crc::default();
669662
for &byte in test_data {
670663
crc_reference.hash_update(&[byte]);
671664
}
@@ -686,7 +679,7 @@ mod tests {
686679
];
687680

688681
for (input, expected) in test_cases {
689-
let mut crc = Crc::new();
682+
let mut crc = Crc::default();
690683
crc.hash_update(input.as_bytes());
691684
let mut output = [0u8; 8];
692685
crc.hash_finalize(&mut output);
@@ -698,14 +691,14 @@ mod tests {
698691

699692
#[test]
700693
fn test_crc_hash_update_edge_cases() {
701-
let mut crc = Crc::new();
694+
let mut crc = Crc::default();
702695

703696
// Test with data that's not a multiple of 8 bytes
704697
let data7 = b"1234567"; // 7 bytes
705698
crc.hash_update(data7);
706699

707700
let data9 = b"123456789"; // 9 bytes
708-
let mut crc2 = Crc::new();
701+
let mut crc2 = Crc::default();
709702
crc2.hash_update(data9);
710703

711704
// Should not panic and should produce valid results

0 commit comments

Comments
 (0)