Skip to content

Commit c1c8903

Browse files
committed
Refactor redundant Default implementations using a shared macro
Introduced a shared macro `impl_default_new!` in `src/common.rs` to automate the implementation of the `Default` trait for types that delegate directly to their `new()` method. This reduces boilerplate and improves maintainability across `src/api.rs`, `src/batch.rs`, and `src/decompress/mod.rs`.
1 parent 90c8d41 commit c1c8903

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/api.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ pub struct Decompressor {
130130
limit_ratio: usize,
131131
}
132132

133-
impl Default for Decompressor {
134-
fn default() -> Self {
135-
Self::new()
136-
}
137-
}
133+
crate::impl_default_new!(Decompressor);
138134

139135
impl Decompressor {
140136
pub fn new() -> Self {

src/batch.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ impl BatchCompressor {
4141

4242
pub struct BatchDecompressor;
4343

44-
impl Default for BatchDecompressor {
45-
fn default() -> Self {
46-
Self::new()
47-
}
48-
}
44+
crate::impl_default_new!(BatchDecompressor);
4945

5046
impl BatchDecompressor {
5147
pub fn new() -> Self {

src/common.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,15 @@ pub fn slice_as_uninit_mut(slice: &mut [u8]) -> &mut [std::mem::MaybeUninit<u8>]
8484
)
8585
}
8686
}
87+
88+
#[macro_export]
89+
macro_rules! impl_default_new {
90+
($t:ty) => {
91+
impl Default for $t {
92+
#[inline(always)]
93+
fn default() -> Self {
94+
Self::new()
95+
}
96+
}
97+
};
98+
}

src/decompress/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ pub enum DecompressResult {
8484
ShortInput,
8585
}
8686

87-
impl Default for Decompressor {
88-
fn default() -> Self {
89-
Self::new()
90-
}
91-
}
87+
crate::impl_default_new!(Decompressor);
9288

9389
impl Decompressor {
9490
pub fn new() -> Self {

0 commit comments

Comments
 (0)