Skip to content

feat: impl From<Flush> to MZFlush #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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