pub struct Lzss<const EI: usize, const EJ: usize, const C: u8, const N: usize, const N2: usize>(_);
Expand description
A zero-sized type, the const generics specify the parameters of the compression.
Parameters
EI
- The number of bits in the offset, usually10..13
EJ
- The number of bits in the length, usually4..5
C
- The initial fill byte of the buffer, usually0x20
(space)N
- Equals1 << EI
, the size of the buffer forLzss::decompress
N2
- Equals2 << EI
(N * 2
), the size of the buffer forLzss::compress
Restrictions
EJ
must be larger than0
EI
must be larger thanEJ
EI + EJ
must be at least 8EI + EJ
must be 24 or lessN
must be equal to1 << EI
N2
must be equal to2 << EI
(N * 2
)
All parameters are checked at compile-time.
There is no runtime overhead since everything is checked during compile-time.
Limitations
Since it’s not possible to do const calculations on const generics all parameters have to be set.
Example
type MyLzss = Lzss<10, 4, 0x20, { 1 << 10 }, { 2 << 10 }>;
let input = b"Example Data";
let mut output = [0; 14];
let result = MyLzss::compress_stack(
SliceReader::new(input),
SliceWriterExact::new(&mut output),
);
assert!(result.is_ok()); // the output is exactly 14 bytes long
Implementations§
source§impl<const EI: usize, const EJ: usize, const C: u8, const N: usize, const N2: usize> Lzss<EI, EJ, C, N, N2>
impl<const EI: usize, const EJ: usize, const C: u8, const N: usize, const N2: usize> Lzss<EI, EJ, C, N, N2>
sourcepub const fn as_dyn() -> LzssDyn
pub const fn as_dyn() -> LzssDyn
Create a new LzssDyn
with the parameter from this generic type.
This is mainly useful for creating const LzssDyn
.
type MyLzss = Lzss<10, 4, 0x20, { 1 << 10 }, { 2 << 10 }>;
const MY_DYN1: LzssDyn = MyLzss::as_dyn();
// or
const MY_DYN2: LzssDyn = Lzss::<10, 4, 0x20, { 1 << 10 }, { 2 << 10 }>::as_dyn();
sourcepub fn compress<R: Read, W: Write>(
reader: R,
writer: W
) -> Result<W::Output, LzssError<R::Error, W::Error>>
👎Deprecated since 0.9.0: renamed to compress_stack
pub fn compress<R: Read, W: Write>( reader: R, writer: W ) -> Result<W::Output, LzssError<R::Error, W::Error>>
Compress the input data into the output.
The buffer, with N2
bytes, is allocated on the stack.
sourcepub fn compress_stack<R: Read, W: Write>(
reader: R,
writer: W
) -> Result<W::Output, LzssError<R::Error, W::Error>>
pub fn compress_stack<R: Read, W: Write>( reader: R, writer: W ) -> Result<W::Output, LzssError<R::Error, W::Error>>
Compress the input data into the output.
The buffer, with N2
bytes, is allocated on the stack.
sourcepub fn compress_heap<R: Read, W: Write>(
reader: R,
writer: W
) -> Result<W::Output, LzssError<R::Error, W::Error>>
Available on crate features alloc
or std
only.
pub fn compress_heap<R: Read, W: Write>( reader: R, writer: W ) -> Result<W::Output, LzssError<R::Error, W::Error>>
alloc
or std
only.Compress the input data into the output.
The buffer, with N2
bytes, is allocated on the heap.
sourcepub fn compress_with_buffer<R: Read, W: Write>(
reader: R,
writer: W,
buffer: &mut [u8; N2]
) -> Result<W::Output, LzssError<R::Error, W::Error>>
pub fn compress_with_buffer<R: Read, W: Write>( reader: R, writer: W, buffer: &mut [u8; N2] ) -> Result<W::Output, LzssError<R::Error, W::Error>>
Compress the input data into the output.
sourcepub fn decompress<R: Read, W: Write>(
reader: R,
writer: W
) -> Result<W::Output, LzssError<R::Error, W::Error>>
👎Deprecated since 0.9.0: renamed to decompress_stack
pub fn decompress<R: Read, W: Write>( reader: R, writer: W ) -> Result<W::Output, LzssError<R::Error, W::Error>>
Decompress the input data into the output.
The buffer, with N
bytes, is allocated on the stack.
sourcepub fn decompress_stack<R: Read, W: Write>(
reader: R,
writer: W
) -> Result<W::Output, LzssError<R::Error, W::Error>>
pub fn decompress_stack<R: Read, W: Write>( reader: R, writer: W ) -> Result<W::Output, LzssError<R::Error, W::Error>>
Decompress the input data into the output.
The buffer, with N
bytes, is allocated on the stack.
sourcepub fn decompress_heap<R: Read, W: Write>(
reader: R,
writer: W
) -> Result<W::Output, LzssError<R::Error, W::Error>>
Available on crate features alloc
or std
only.
pub fn decompress_heap<R: Read, W: Write>( reader: R, writer: W ) -> Result<W::Output, LzssError<R::Error, W::Error>>
alloc
or std
only.Decompress the input data into the output.
The buffer, with N
bytes, is allocated on the heap.
sourcepub fn decompress_with_buffer<R: Read, W: Write>(
reader: R,
writer: W,
buffer: &mut [u8; N]
) -> Result<W::Output, LzssError<R::Error, W::Error>>
pub fn decompress_with_buffer<R: Read, W: Write>( reader: R, writer: W, buffer: &mut [u8; N] ) -> Result<W::Output, LzssError<R::Error, W::Error>>
Decompress the input data into the output.
sourcepub fn compress_in_place(io: &mut [u8], offset: usize) -> (usize, Option<usize>)
pub fn compress_in_place(io: &mut [u8], offset: usize) -> (usize, Option<usize>)
Compress, the input and output is in the same slice.
The input is located at io[offset..]
.
When there is enough space in the slice then the result will be (size, None)
.
And the output is located at io[0..size]
.
If there is not enough space in the slice, i.e. the output (or buffer) would overwrite
the input, then the result will be (size, Some(new_offset))
, the already compressed
data is in io[0..size]
and the not yet compressed data is in io[new_offset..]
.
Even when the compression fails due to space the data is recoverable.
The minimum offset is Lzss::MIN_OFFSET
, though if the offset is Lzss::MIN_OFFSET + input_size/8
then the compression can’t fail.
sourcepub const MIN_OFFSET: usize = _
pub const MIN_OFFSET: usize = _
The minimal offset when using compress_in_place
.
It’s a little less than N
.