Skip to content

Commit 5a2fd04

Browse files
authored
Merge pull request #462 from CosminPerRam/feat/flush_mzflush
feat: impl From<Flush> to MZFlush
2 parents bf5bf56 + 40c2e0f commit 5a2fd04

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

src/ffi/rust.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ impl fmt::Debug for Inflate {
5050
}
5151
}
5252

53+
impl From<FlushDecompress> for MZFlush {
54+
fn from(value: FlushDecompress) -> Self {
55+
match value {
56+
FlushDecompress::None => Self::None,
57+
FlushDecompress::Sync => Self::Sync,
58+
FlushDecompress::Finish => Self::Finish,
59+
}
60+
}
61+
}
62+
5363
impl InflateBackend for Inflate {
5464
fn make(zlib_header: bool, _window_bits: u8) -> Self {
5565
let format = format_from_bool(zlib_header);
@@ -67,9 +77,8 @@ impl InflateBackend for Inflate {
6777
output: &mut [u8],
6878
flush: FlushDecompress,
6979
) -> Result<Status, DecompressError> {
70-
let flush = MZFlush::new(flush as i32).unwrap();
71-
72-
let res = inflate::stream::inflate(&mut self.inner, input, output, flush);
80+
let mz_flush = flush.into();
81+
let res = inflate::stream::inflate(&mut self.inner, input, output, mz_flush);
7382
self.total_in += res.bytes_consumed as u64;
7483
self.total_out += res.bytes_written as u64;
7584

@@ -123,6 +132,17 @@ impl fmt::Debug for Deflate {
123132
}
124133
}
125134

135+
impl From<FlushCompress> for MZFlush {
136+
fn from(value: FlushCompress) -> Self {
137+
match value {
138+
FlushCompress::None => Self::None,
139+
FlushCompress::Partial | FlushCompress::Sync => Self::Sync,
140+
FlushCompress::Full => Self::Full,
141+
FlushCompress::Finish => Self::Finish,
142+
}
143+
}
144+
}
145+
126146
impl DeflateBackend for Deflate {
127147
fn make(level: Compression, zlib_header: bool, _window_bits: u8) -> Self {
128148
// Check in case the integer value changes at some point.
@@ -145,8 +165,8 @@ impl DeflateBackend for Deflate {
145165
output: &mut [u8],
146166
flush: FlushCompress,
147167
) -> Result<Status, CompressError> {
148-
let flush = MZFlush::new(flush as i32).unwrap();
149-
let res = deflate::stream::deflate(&mut self.inner, input, output, flush);
168+
let mz_flush = flush.into();
169+
let res = deflate::stream::deflate(&mut self.inner, input, output, mz_flush);
150170
self.total_in += res.bytes_consumed as u64;
151171
self.total_out += res.bytes_written as u64;
152172

src/mem.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ pub enum FlushCompress {
4949
/// accumulate before producing output in order to maximize compression.
5050
None = ffi::MZ_NO_FLUSH as isize,
5151

52+
/// All pending output is flushed to the output buffer, but the output is
53+
/// not aligned to a byte boundary.
54+
///
55+
/// All input data so far will be available to the decompressor (as with
56+
/// `Flush::Sync`). This completes the current deflate block and follows it
57+
/// with an empty fixed codes block that is 10 bytes long, and it assures
58+
/// that enough bytes are output in order for the decompressor to finish the
59+
/// block before the empty fixed code block.
60+
Partial = ffi::MZ_PARTIAL_FLUSH as isize,
61+
5262
/// All pending output is flushed to the output buffer and the output is
5363
/// aligned on a byte boundary so that the decompressor can get all input
5464
/// data available so far.
@@ -58,16 +68,6 @@ pub enum FlushCompress {
5868
/// deflate block and follow it with an empty stored block.
5969
Sync = ffi::MZ_SYNC_FLUSH as isize,
6070

61-
/// All pending output is flushed to the output buffer, but the output is
62-
/// not aligned to a byte boundary.
63-
///
64-
/// All of the input data so far will be available to the decompressor (as
65-
/// with `Flush::Sync`. This completes the current deflate block and follows
66-
/// it with an empty fixed codes block that is 10 bites long, and it assures
67-
/// that enough bytes are output in order for the decompressor to finish the
68-
/// block before the empty fixed code block.
69-
Partial = ffi::MZ_PARTIAL_FLUSH as isize,
70-
7171
/// All output is flushed as with `Flush::Sync` and the compression state is
7272
/// reset so decompression can restart from this point if previous
7373
/// compressed data has been damaged or if random access is desired.

0 commit comments

Comments
 (0)