pub struct Compressor<'a> { /* private fields */ }
Expand description
This struct provides a means of initializing and performing a ZX0 compression operation by leveraging the builder pattern.
By calling Compressor::new
a new Compressor
will be instantiated using the following
default values:
- No prefix/suffix skipping
- Quick mode disabled
- Backwards mode disabled
- Classic mode disabled
After constructing a Compressor
instance the method compress
is available to compress
u8
slices. The Compressor
can be resued again afterwards.
In contrast to the original C implementation, compression using the Rust ZX0 compressor is thread-safe, and can therefore be used to compress several slices in parallel.
Implementations§
Source§impl<'a> Compressor<'a>
impl<'a> Compressor<'a>
Sourcepub fn new() -> Self
pub fn new() -> Self
Instantiate a new Compressor
using the following default values:
- No prefix/suffix skipping
- Quick mode disabled
- Backwards mode disabled
- Classic mode disabled
Sourcepub fn quick_mode(&mut self, quick_mode: bool) -> &mut Self
pub fn quick_mode(&mut self, quick_mode: bool) -> &mut Self
Change the value for the quick mode setting. When enabled, this will cause the ZX0 compressor to use a smaller dictionary size, at the cost of a less efficient compression ratio.
Enabling this setting can be useful when producing debug assets where a short feedback loop is more important than getting a good compression ratio.
Sourcepub fn backwards_mode(&mut self, backwards_mode: bool) -> &mut Self
pub fn backwards_mode(&mut self, backwards_mode: bool) -> &mut Self
Change the value for the backwards compression mode setting. This will cause the ZX0 compressor to create compressed data that should be decompressed back-to-front. This can be useful in situations where in-place decompression is desired, and the end of the compressed data overlaps with the end of the region that the uncompressed data should be positioned in.
Please refer to the original C implementation’s readme for an in-depth explanation.
Sourcepub fn classic_mode(&mut self, classic_mode: bool) -> &mut Self
pub fn classic_mode(&mut self, classic_mode: bool) -> &mut Self
Change the value for the classic compression mode setting. Enabling this will cause the ZX0 compressor to output compressed data in its legacy V1 file format. This can be useful when compressing for one of the platforms that only provides a V1 decompression routine.
Sourcepub fn progress_callback<C: FnMut(f32) + 'a>(
&mut self,
progress_callback: C,
) -> &mut Self
pub fn progress_callback<C: FnMut(f32) + 'a>( &mut self, progress_callback: C, ) -> &mut Self
Set a progress callback. When providing a closure to this function, that closure will be
called repeatedly during compression. The closure will be called with a progress value
between 0.0
and 1.0
. Note that due to the nature of the compression algorithm, this
value is not increasing linearly with time, and thus should be interpreted as a rough
estimate.
Sourcepub fn skip(&mut self, skip: usize) -> &mut Self
pub fn skip(&mut self, skip: usize) -> &mut Self
Set the number of prefix/suffix bytes to skip during compression. This will cause the compressor to create a dictionary based on data that will already be in memory before the compressed data during decompression. Of course, for this to work the prefix (or suffix in case of backwards mode) in the file must be 100% identical to the data that is in memory before or after the block of compressed data when attempting to decompress it.
Please refer to the original C implementation’s readme for an in-depth explanation.
Sourcepub fn compress(&mut self, input: &[u8]) -> CompressionResult
pub fn compress(&mut self, input: &[u8]) -> CompressionResult
Compress the provided slice.
This returns a CompressionResult
struct containing both the compressed data as well as
metadata related to the compression operation.
The Compressor
does not have to be discarded after calling this method. It does not
contain any state (only the configuration) and thus can be reused again for compressing
additional data.