pub struct Decoder { /* private fields */ }
Expand description
Decoder
Use Interface to instantiate decoder.
Under hood, in order to avoid generics, implemented as vtable with series of function pointers.
§Example
Brief example for chunked decoding.
use compu::{Decoder, DecodeStatus, Encoder, EncodeOp, EncodeStatus};
fn decompress(decoder: &mut Decoder, input: core::slice::Chunks<'_, u8>, output: &mut Vec<u8>) {
for chunk in input {
let result = decoder.decode_vec(chunk, output);
assert_eq!(result.input_remain, 0);
let status = result.status.expect("success");
if status == DecodeStatus::Finished {
break;
}
}
//Make sure to reset state, if you want to re-use decoder.
decoder.reset();
}
fn prepare_compressed(encoder: &mut Encoder, data: &[u8], compressed: &mut Vec<u8>) {
let result = encoder.encode_vec(DATA, compressed, EncodeOp::Finish);
assert_eq!(result.status, EncodeStatus::Finished);
}
const DATA: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let mut output = Vec::with_capacity(100);
let mut compressed = Vec::with_capacity(100);
let mut encoder = compu::encoder::Interface::brotli_c(Default::default()).expect("to create brotli encoder");
prepare_compressed(&mut encoder, DATA, &mut compressed);
let mut decoder = compu::decoder::Interface::brotli_c().expect("to create brotli decoder");
decompress(&mut decoder, compressed.chunks(4), &mut output);
assert_eq!(output, DATA);
output.truncate(0);
compressed.truncate(0);
let mut compressed = Vec::with_capacity(100);
let mut encoder = compu::encoder::Interface::zstd(Default::default()).expect("to create zstd encoder");
prepare_compressed(&mut encoder, DATA, &mut compressed);
let mut decoder = compu::decoder::Interface::zstd(Default::default()).expect("to create zstd decoder");
decompress(&mut decoder, compressed.chunks(4), &mut output);
assert_eq!(output, DATA);
output.truncate(0);
compressed.truncate(0);
let mut compressed = Vec::with_capacity(100);
let mut encoder = compu::encoder::Interface::zlib_ng(Default::default()).expect("to create zlib-ng encoder");
prepare_compressed(&mut encoder, DATA, &mut compressed);
let mut decoder = compu::decoder::Interface::zlib_ng(Default::default()).expect("to create zlib-ng decoder");
decompress(&mut decoder, compressed.chunks(4), &mut output);
assert_eq!(output, DATA);
output.truncate(0);
compressed.truncate(0);
Implementations§
source§impl Decoder
impl Decoder
sourcepub unsafe fn raw_decode(
&mut self,
input: *const u8,
input_len: usize,
output: *mut u8,
output_len: usize
) -> Decode
pub unsafe fn raw_decode( &mut self, input: *const u8, input_len: usize, output: *mut u8, output_len: usize ) -> Decode
Raw decoding function, with no checks.
Intended to be used as building block of higher level interfaces
Arguments
input
- Pointer to start of input to process. MUST NOT be null.input_len
- Size of data to process ininput
ouput
- Pointer to start of buffer where to write result. MUST NOT be nulloutput_len
- Size of buffer pointed byoutput
sourcepub fn decode_uninit(
&mut self,
input: &[u8],
output: &mut [MaybeUninit<u8>]
) -> Decode
pub fn decode_uninit( &mut self, input: &[u8], output: &mut [MaybeUninit<u8>] ) -> Decode
Decodes input
into uninit output
.
Decode
will contain number of bytes written into output
. This number always indicates
number of bytes written hence which can be assumed initialized.
sourcepub fn decode_vec(&mut self, input: &[u8], output: &mut Vec<u8>) -> Decode
pub fn decode_vec(&mut self, input: &[u8], output: &mut Vec<u8>) -> Decode
Decodes input
into spare space in output
.
Function require user to alloc spare capacity himself.
Decode::output_remain
will be relatieve to spare capacity length.
sourcepub fn reset(&mut self) -> bool
pub fn reset(&mut self) -> bool
Resets Decoder
state to initial.
Returns true
if successfully reset, otherwise false
sourcepub fn describe_error(&self, error: DecodeError) -> Option<&'static str>
pub fn describe_error(&self, error: DecodeError) -> Option<&'static str>
Returns descriptive text for error.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Decoder
impl RefUnwindSafe for Decoder
impl !Send for Decoder
impl !Sync for Decoder
impl Unpin for Decoder
impl UnwindSafe for Decoder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more