Skip to content

Commit

Permalink
Merge pull request #462 from CosminPerRam/feat/flush_mzflush
Browse files Browse the repository at this point in the history
feat: impl From<Flush> to MZFlush
  • Loading branch information
Byron authored Feb 19, 2025
2 parents bf5bf56 + 40c2e0f commit 5a2fd04
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
30 changes: 25 additions & 5 deletions src/ffi/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ impl fmt::Debug for Inflate {
}
}

impl From<FlushDecompress> for MZFlush {
fn from(value: FlushDecompress) -> Self {
match value {
FlushDecompress::None => Self::None,
FlushDecompress::Sync => Self::Sync,
FlushDecompress::Finish => Self::Finish,
}
}
}

impl InflateBackend for Inflate {
fn make(zlib_header: bool, _window_bits: u8) -> Self {
let format = format_from_bool(zlib_header);
Expand All @@ -67,9 +77,8 @@ impl InflateBackend for Inflate {
output: &mut [u8],
flush: FlushDecompress,
) -> Result<Status, DecompressError> {
let flush = MZFlush::new(flush as i32).unwrap();

let res = inflate::stream::inflate(&mut self.inner, input, output, flush);
let mz_flush = flush.into();
let res = inflate::stream::inflate(&mut self.inner, input, output, mz_flush);
self.total_in += res.bytes_consumed as u64;
self.total_out += res.bytes_written as u64;

Expand Down Expand Up @@ -123,6 +132,17 @@ impl fmt::Debug for Deflate {
}
}

impl From<FlushCompress> for MZFlush {
fn from(value: FlushCompress) -> Self {
match value {
FlushCompress::None => Self::None,
FlushCompress::Partial | FlushCompress::Sync => Self::Sync,
FlushCompress::Full => Self::Full,
FlushCompress::Finish => Self::Finish,
}
}
}

impl DeflateBackend for Deflate {
fn make(level: Compression, zlib_header: bool, _window_bits: u8) -> Self {
// Check in case the integer value changes at some point.
Expand All @@ -145,8 +165,8 @@ impl DeflateBackend for Deflate {
output: &mut [u8],
flush: FlushCompress,
) -> Result<Status, CompressError> {
let flush = MZFlush::new(flush as i32).unwrap();
let res = deflate::stream::deflate(&mut self.inner, input, output, flush);
let mz_flush = flush.into();
let res = deflate::stream::deflate(&mut self.inner, input, output, mz_flush);
self.total_in += res.bytes_consumed as u64;
self.total_out += res.bytes_written as u64;

Expand Down
20 changes: 10 additions & 10 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ pub enum FlushCompress {
/// accumulate before producing output in order to maximize compression.
None = ffi::MZ_NO_FLUSH as isize,

/// All pending output is flushed to the output buffer, but the output is
/// not aligned to a byte boundary.
///
/// All input data so far will be available to the decompressor (as with
/// `Flush::Sync`). This completes the current deflate block and follows it
/// with an empty fixed codes block that is 10 bytes long, and it assures
/// that enough bytes are output in order for the decompressor to finish the
/// block before the empty fixed code block.
Partial = ffi::MZ_PARTIAL_FLUSH as isize,

/// All pending output is flushed to the output buffer and the output is
/// aligned on a byte boundary so that the decompressor can get all input
/// data available so far.
Expand All @@ -58,16 +68,6 @@ pub enum FlushCompress {
/// deflate block and follow it with an empty stored block.
Sync = ffi::MZ_SYNC_FLUSH as isize,

/// All pending output is flushed to the output buffer, but the output is
/// not aligned to a byte boundary.
///
/// All of the input data so far will be available to the decompressor (as
/// with `Flush::Sync`. This completes the current deflate block and follows
/// it with an empty fixed codes block that is 10 bites long, and it assures
/// that enough bytes are output in order for the decompressor to finish the
/// block before the empty fixed code block.
Partial = ffi::MZ_PARTIAL_FLUSH as isize,

/// All output is flushed as with `Flush::Sync` and the compression state is
/// reset so decompression can restart from this point if previous
/// compressed data has been damaged or if random access is desired.
Expand Down

0 comments on commit 5a2fd04

Please sign in to comment.