Skip to main content

Encoder

Struct Encoder 

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

A streaming zlib encoder.

§Examples

use compress_lib::{Level, deflate::Flush, zlib};

let mut enc = zlib::Encoder::new(Level::DEFAULT);
let mut packed = Vec::new();
enc.compress_vec(b"part one, ", &mut packed, Flush::None)?;
enc.compress_vec(b"part two", &mut packed, Flush::Finish)?;
assert_eq!(zlib::decompress(&packed)?, b"part one, part two");

Implementations§

Source§

impl Encoder

Source

pub fn new(level: Level) -> Self

An encoder at level with window bits 15.

§Examples
let enc = compress_lib::zlib::Encoder::new(compress_lib::Level::FAST);
Source

pub fn with_window_bits(self, bits: u8) -> Result<Self, Error>

Sets the window to 2^bits bytes, 8 to 15 (the header names it; zlib’s own encoder rounds 8 up to 9, this one does not).

§Errors

ErrorKind::InvalidWindowBits outside 8 to 15.

§Examples
use compress_lib::{Level, deflate::Flush, zlib};

let mut enc = zlib::Encoder::new(Level::DEFAULT).with_window_bits(10)?;
let mut packed = Vec::new();
enc.compress_vec(b"x", &mut packed, Flush::Finish)?;
assert_eq!(packed[0], 0x28);
Source

pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<(), Error>

Presets a dictionary; the header then names it (FDICT) and the decoder must be given the same one. Only before any input.

§Errors

ErrorKind::InvalidState after input has been given.

§Examples
let mut enc = compress_lib::zlib::Encoder::default();
enc.set_dictionary(b"dictionary")?;
Source

pub fn reset(&mut self)

Starts a new stream, keeping allocations (and no dictionary).

§Examples
let mut enc = compress_lib::zlib::Encoder::default();
enc.reset();
Source

pub fn total_in(&self) -> u64

Uncompressed bytes taken.

§Examples
use compress_lib::{deflate::Flush, zlib};

let mut enc = zlib::Encoder::default();
enc.compress_vec(b"abc", &mut Vec::new(), Flush::Finish)?;
assert_eq!(enc.total_in(), 3);
Source

pub fn memory_in_use(&self) -> usize

Heap bytes held.

§Examples
assert!(compress_lib::zlib::Encoder::default().memory_in_use() > 0);
Source

pub fn compress( &mut self, input: &[u8], output: &mut [u8], flush: Flush, ) -> Result<Progress, Error>

As Deflater::compress, with the header written first and the Adler-32 trailer after Finish.

§Errors

As Deflater::compress.

§Examples
use compress_lib::{deflate::{Flush, Status}, zlib};

let mut enc = zlib::Encoder::default();
let mut out = [0u8; 64];
let p = enc.compress(b"abc", &mut out, Flush::Finish)?;
assert_eq!(p.status, Status::Done);
assert_eq!(zlib::decompress(&out[..p.produced])?, b"abc");
Source

pub fn compress_vec( &mut self, input: &[u8], out: &mut Vec<u8>, flush: Flush, ) -> Result<Progress, Error>

As compress, appending to out until the flush point is written.

§Errors

As compress, plus AllocationFailed.

§Examples
use compress_lib::{deflate::Flush, zlib};

let mut out = Vec::new();
zlib::Encoder::default().compress_vec(b"abc", &mut out, Flush::Finish)?;
assert_eq!(zlib::decompress(&out)?, b"abc");

Trait Implementations§

Source§

impl Debug for Encoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Encoder

Source§

fn default() -> Self

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.