Skip to content

Commit 4bb60ac

Browse files
committed
uucore: Fix new_without_default clippy lint
1 parent 690b149 commit 4bb60ac

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);
@@ -600,8 +593,8 @@ mod tests {
600593
#[test]
601594
fn test_crc_basic_functionality() {
602595
// Test that our CRC implementation works with basic functionality
603-
let mut crc1 = Crc::new();
604-
let mut crc2 = Crc::new();
596+
let mut crc1 = Crc::default();
597+
let mut crc2 = Crc::default();
605598

606599
// Same input should give same output
607600
crc1.hash_update(b"test");
@@ -617,15 +610,15 @@ mod tests {
617610

618611
#[test]
619612
fn test_crc_digest_basic() {
620-
let mut crc = Crc::new();
613+
let mut crc = Crc::default();
621614

622615
// Test empty input
623616
let mut output = [0u8; 8];
624617
crc.hash_finalize(&mut output);
625618
let empty_result = u64::from_ne_bytes(output);
626619

627620
// Reset and test with "test" string
628-
let mut crc = Crc::new();
621+
let mut crc = Crc::default();
629622
crc.hash_update(b"test");
630623
crc.hash_finalize(&mut output);
631624
let test_result = u64::from_ne_bytes(output);
@@ -639,8 +632,8 @@ mod tests {
639632

640633
#[test]
641634
fn test_crc_digest_incremental() {
642-
let mut crc1 = Crc::new();
643-
let mut crc2 = Crc::new();
635+
let mut crc1 = Crc::default();
636+
let mut crc2 = Crc::default();
644637

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

668-
let mut crc_optimized = Crc::new();
661+
let mut crc_optimized = Crc::default();
669662
crc_optimized.hash_update(test_data);
670663
let mut output_opt = [0u8; 8];
671664
crc_optimized.hash_finalize(&mut output_opt);
672665

673666
// Create a reference implementation using hash_update
674-
let mut crc_reference = Crc::new();
667+
let mut crc_reference = Crc::default();
675668
for &byte in test_data {
676669
crc_reference.hash_update(&[byte]);
677670
}
@@ -692,7 +685,7 @@ mod tests {
692685
];
693686

694687
for (input, expected) in test_cases {
695-
let mut crc = Crc::new();
688+
let mut crc = Crc::default();
696689
crc.hash_update(input.as_bytes());
697690
let mut output = [0u8; 8];
698691
crc.hash_finalize(&mut output);
@@ -704,14 +697,14 @@ mod tests {
704697

705698
#[test]
706699
fn test_crc_hash_update_edge_cases() {
707-
let mut crc = Crc::new();
700+
let mut crc = Crc::default();
708701

709702
// Test with data that's not a multiple of 8 bytes
710703
let data7 = b"1234567"; // 7 bytes
711704
crc.hash_update(data7);
712705

713706
let data9 = b"123456789"; // 9 bytes
714-
let mut crc2 = Crc::new();
707+
let mut crc2 = Crc::default();
715708
crc2.hash_update(data9);
716709

717710
// Should not panic and should produce valid results

0 commit comments

Comments
 (0)