Struct compu::decoder::Decoder

source ·
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

source

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 in input
  • ouput - Pointer to start of buffer where to write result. MUST NOT be null
  • output_len - Size of buffer pointed by output
source

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.

source

pub fn decode(&mut self, input: &[u8], output: &mut [u8]) -> Decode

Decodes input into output.

source

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.

source

pub fn reset(&mut self) -> bool

Resets Decoder state to initial.

Returns true if successfully reset, otherwise false

source

pub fn describe_error(&self, error: DecodeError) -> Option<&'static str>

Returns descriptive text for error.

Trait Implementations§

source§

impl Drop for Decoder

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.