//! Extra streaming compression functionality. //! //! As of now this is mainly intended for use to build a higher-level wrapper. //! //! There is no DeflateState as the needed state is contained in the compressor struct itself.
/// Try to compress from input to output with the given [`CompressorOxide`]. /// /// # Errors /// /// Returns [`MZError::Buf`] If the size of the `output` slice is empty or no progress was made due /// to lack of expected input data, or if called without [`MZFlush::Finish`] after the compression /// was already finished. /// /// Returns [`MZError::Param`] if the compressor parameters are set wrong. /// /// Returns [`MZError::Stream`] when lower-level decompressor returns a /// [`TDEFLStatus::PutBufFailed`]; may not actually be possible. pubfn deflate(
compressor: &mut CompressorOxide,
input: &[u8],
output: &mut [u8],
flush: MZFlush,
) -> StreamResult { if output.is_empty() { return StreamResult::error(MZError::Buf);
}
let status = loop { let in_bytes; let out_bytes; let defl_status = { let res = compress(compressor, next_in, next_out, TDEFLFlush::from(flush));
in_bytes = res.1;
out_bytes = res.2;
res.0
};
// Check if we are done, or compression failed. match defl_status {
TDEFLStatus::BadParam => break Err(MZError::Param), // Don't think this can happen as we're not using a custom callback.
TDEFLStatus::PutBufFailed => break Err(MZError::Stream),
TDEFLStatus::Done => break Ok(MZStatus::StreamEnd),
_ => (),
};
// All the output space was used, so wait for more. if next_out.is_empty() { break Ok(MZStatus::Ok);
}
if next_in.is_empty() && (flush != MZFlush::Finish) { let total_changed = bytes_written > 0 || bytes_consumed > 0;
breakif (flush != MZFlush::None) || total_changed { // We wrote or consumed something, and/or did a flush (sync/partial etc.).
Ok(MZStatus::Ok)
} else { // No more input data, not flushing, and nothing was consumed or written, // so couldn't make any progress.
Err(MZError::Buf)
};
}
};
StreamResult {
bytes_consumed,
bytes_written,
status,
}
}
#[cfg(test)] mod test { usesuper::deflate; usecrate::deflate::CompressorOxide; usecrate::inflate::decompress_to_vec_zlib; usecrate::{MZFlush, MZStatus}; use alloc::boxed::Box; use alloc::vec;
#[test] fn test_state() { let data = b"Hello zlib!"; letmut compressed = vec![0; 50]; letmut compressor = Box::<CompressorOxide>::default(); let res = deflate(&mut compressor, data, &mut compressed, MZFlush::Finish); let status = res.status.expect("Failed to compress!"); let decomp =
decompress_to_vec_zlib(&compressed).expect("Failed to decompress compressed data");
assert_eq!(status, MZStatus::StreamEnd);
assert_eq!(decomp[..], data[..]);
assert_eq!(res.bytes_consumed, data.len());
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.19 Sekunden
(vorverarbeitet am 2026-06-23)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.