Skip to content

Commit 1e14851

Browse files
committed
simplify constructors
1 parent 3d564b8 commit 1e14851

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

zlib-rs/src/deflate.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,9 @@ impl<'a> DeflateStream<'a> {
122122
)
123123
}
124124

125-
pub fn new(level: i32, zlib_header: bool, window_bits: u8) -> Self {
125+
pub fn new(config: DeflateConfig) -> Self {
126126
let mut inner = crate::c_api::z_stream::default();
127127

128-
let config = DeflateConfig {
129-
window_bits: if zlib_header {
130-
i32::from(window_bits)
131-
} else {
132-
-i32::from(window_bits)
133-
},
134-
level,
135-
method: Method::Deflated,
136-
mem_level: DEF_MEM_LEVEL,
137-
strategy: Strategy::Default,
138-
};
139-
140128
let ret = crate::deflate::init(&mut inner, config);
141129
assert_eq!(ret, ReturnCode::Ok);
142130

@@ -151,7 +139,7 @@ const HASH_BITS: usize = 16;
151139

152140
/// Maximum value for memLevel in deflateInit2
153141
const MAX_MEM_LEVEL: i32 = 9;
154-
const DEF_MEM_LEVEL: i32 = if MAX_MEM_LEVEL > 8 { 8 } else { MAX_MEM_LEVEL };
142+
pub const DEF_MEM_LEVEL: i32 = if MAX_MEM_LEVEL > 8 { 8 } else { MAX_MEM_LEVEL };
155143

156144
#[repr(i32)]
157145
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]

zlib-rs/src/inflate.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,9 @@ impl<'a> InflateStream<'a> {
127127
unsafe { &mut *(self as *mut _ as *mut z_stream) }
128128
}
129129

130-
pub fn new(zlib_header: bool, window_bits: u8) -> Self {
130+
pub fn new(config: InflateConfig) -> Self {
131131
let mut inner = crate::c_api::z_stream::default();
132132

133-
let config = InflateConfig {
134-
window_bits: if zlib_header {
135-
i32::from(window_bits)
136-
} else {
137-
-i32::from(window_bits)
138-
},
139-
};
140-
141133
let ret = crate::inflate::init(&mut inner, config);
142134
assert_eq!(ret, ReturnCode::Ok);
143135

zlib-rs/src/stable.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::deflate::DeflateConfig;
12
use crate::inflate::InflateConfig;
23
use crate::ReturnCode;
34
pub use crate::{DeflateFlush, InflateFlush};
@@ -85,8 +86,18 @@ impl Inflate {
8586
}
8687

8788
/// Create a new instance. Note that it allocates in various ways and thus should be re-used.
89+
///
90+
/// The `window_bits` must be in the range `8..=15`, with `15` being most common.
8891
pub fn new(zlib_header: bool, window_bits: u8) -> Self {
89-
Self(crate::inflate::InflateStream::new(zlib_header, window_bits))
92+
let config = InflateConfig {
93+
window_bits: if zlib_header {
94+
i32::from(window_bits)
95+
} else {
96+
-i32::from(window_bits)
97+
},
98+
};
99+
100+
Self(crate::inflate::InflateStream::new(config))
90101
}
91102

92103
/// Reset the state to allow handling a new stream.
@@ -203,12 +214,20 @@ impl Deflate {
203214
}
204215

205216
/// Create a new instance - this allocates so should be done with care.
217+
///
218+
/// The `window_bits` must be in the range `8..=15`, with `15` being most common.
206219
pub fn new(level: i32, zlib_header: bool, window_bits: u8) -> Self {
207-
Self(crate::deflate::DeflateStream::new(
220+
let config = DeflateConfig {
221+
window_bits: if zlib_header {
222+
i32::from(window_bits)
223+
} else {
224+
-i32::from(window_bits)
225+
},
208226
level,
209-
zlib_header,
210-
window_bits,
211-
))
227+
..DeflateConfig::default()
228+
};
229+
230+
Self(crate::deflate::DeflateStream::new(config))
212231
}
213232

214233
/// Prepare the instance for a new stream.

0 commit comments

Comments
 (0)