Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

Stateful encoder that reuses internal hash-table buffers across calls.

The free functions encode, encode_better, encode_best, and encode_snappy allocate a fresh hash table on every invocation. For hot loops compressing many small blocks, allocating and zero-filling that buffer every time is a measurable fraction of the call cost (≈ 50–80 % for ≤ 1 KB inputs).

An Encoder holds these buffers and grows them lazily on first use, so repeated calls in the same mode pay only the in-cache memset cost instead of going through the system allocator. Output is bit-for-bit identical to the equivalent free function for every input.

§Example

use minlz::Encoder;

let mut enc = Encoder::new();
let mut compressed_blocks: Vec<Vec<u8>> = (0..1000)
    .map(|i| enc.encode(&[i as u8; 256]))
    .collect();

Implementations§

Source§

impl Encoder

Source

pub fn new() -> Self

Create a new encoder with no buffers allocated yet.

Source

pub fn encode(&mut self, src: &[u8]) -> Vec<u8>

Encode src using the standard (fast) algorithm. Equivalent to the free encode() function but reuses internal hash-table storage across calls.

Source

pub fn encode_better(&mut self, src: &[u8]) -> Vec<u8>

Encode src using the better-compression algorithm. Equivalent to the free encode_better() function with internal buffer reuse.

Source

pub fn encode_best(&mut self, src: &[u8]) -> Vec<u8>

Encode src using the best-compression algorithm. Equivalent to the free encode_best() function with internal buffer reuse. Buffer reuse matters most here because the best-mode hash tables are 4.5 MiB total.

Source

pub fn encode_snappy(&mut self, src: &[u8]) -> Vec<u8>

Encode src in Snappy-compatible format. Equivalent to the free encode_snappy() function with internal buffer reuse.

Trait Implementations§

Source§

impl Default for Encoder

Source§

fn default() -> Encoder

Returns the “default value” for a 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>,

Source§

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>,

Source§

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.