Struct compu::encoder::Encoder

source ·
pub struct Encoder { /* private fields */ }
Expand description

Encoder

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 encoding.

use compu::{Encoder, EncodeStatus, EncodeOp};

fn compress(encoder: &mut Encoder, input: &[&[u8]], output: &mut Vec<u8>) {
   for chunk in input {
     let spare_capacity = output.spare_capacity_mut();
     let output_len = spare_capacity.len();
     let result = encoder.encode_uninit(chunk, spare_capacity, EncodeOp::Flush);

     assert_eq!(result.input_remain, 0);
     assert_ne!(result.status, EncodeStatus::Error);
     assert_eq!(result.status, EncodeStatus::Continue);
     unsafe {
         output.set_len(output.len() + output_len - result.output_remain);
     }
   }

   let spare_capacity = output.spare_capacity_mut();
   let output_len = spare_capacity.len();
   let result = encoder.encode_uninit(&[], spare_capacity, EncodeOp::Finish);
   assert_eq!(result.status, EncodeStatus::Finished);

   unsafe {
       output.set_len(output.len() + output_len - result.output_remain);
   }
   //Make sure to reset state, if you want to re-use encoder.
   encoder.reset();
}

let mut output = Vec::with_capacity(100);
let mut encoder = compu::encoder::Interface::brotli_c(Default::default()).expect("to create brotli encoder");
compress(&mut encoder, &[&[1, 2, 3, 4], &[5, 6, 7 ,8], &[9, 10]], &mut output);
assert!(output.len() > 0);

output.truncate(0);
let mut encoder = compu::encoder::Interface::zstd(Default::default()).expect("to create zstd encoder");
compress(&mut encoder, &[&[1, 2, 3, 4], &[5, 6, 7 ,8], &[9, 10]], &mut output);
assert!(output.len() > 0);

output.truncate(0);
let mut encoder = compu::encoder::Interface::zlib_ng(Default::default()).expect("to create zlib-ng encoder");
compress(&mut encoder, &[&[1, 2, 3, 4], &[5, 6, 7 ,8], &[9, 10]], &mut output);
assert!(output.len() > 0);

Implementations§

source§

impl Encoder

source

pub unsafe fn raw_encode( &mut self, input: *const u8, input_len: usize, output: *mut u8, output_len: usize, op: EncodeOp ) -> Encode

Raw encoding 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
  • op - Encoding operation to perform.
source

pub fn encode_uninit( &mut self, input: &[u8], output: &mut [MaybeUninit<u8>], op: EncodeOp ) -> Encode

Encodes input into uninit output.

Encode 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 encode( &mut self, input: &[u8], output: &mut [u8], op: EncodeOp ) -> Encode

Encodes input into output.

source

pub fn encode_vec( &mut self, input: &[u8], output: &mut Vec<u8>, op: EncodeOp ) -> Encode

Encodes input into spare space in output.

Function require user to alloc spare capacity himself.

Encode::output_remain will be relatieve to spare capacity length.

source

pub fn reset(&mut self) -> bool

Resets Encoder state to initial.

Returns true if successfully reset, otherwise false

Trait Implementations§

source§

impl Drop for Encoder

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.