Skip to main content

Deflater

Struct Deflater 

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

A streaming raw DEFLATE encoder (RFC 1951).

Push input, pull output, with a Flush choosing how far each call goes. Memory is fixed when the deflater is built (section 5.6 of the plan): at window bits 15 and level 6, a 64 KiB window, 128 KiB of hash chains, 64 KiB of symbols, and up to one block of pending output.

§Examples

use compress_lib::{Level, deflate::{self, Deflater, Flush}};

let mut deflater = Deflater::new(Level::DEFAULT);
let mut packed = Vec::new();
deflater.compress_vec(b"hello, hello, hello", &mut packed, Flush::Finish)?;
assert_eq!(deflate::decompress(&packed)?, b"hello, hello, hello");

Implementations§

Source§

impl Deflater

Source

pub fn new(level: Level) -> Self

A deflater at level with window bits 15.

§Examples
use compress_lib::{Level, deflate::Deflater};

let deflater = Deflater::new(Level::FAST);
assert_eq!(deflater.level(), Level::FAST);
Source

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

Sets the window to 2^bits bytes, 8 to 15: no match distance is ever longer (RFC 7692 negotiates these sizes; 8 is a real 256-byte window). Smaller windows also use smaller hash and symbol tables.

§Errors

ErrorKind::InvalidWindowBits outside 8 to 15.

§Examples
use compress_lib::{Level, deflate::Deflater};

let small = Deflater::new(Level::FAST).with_window_bits(9)?;
assert!(small.memory_in_use() < Deflater::new(Level::FAST).memory_in_use());
assert!(Deflater::new(Level::FAST).with_window_bits(7).is_err());
Source

pub fn with_memory(self, memory: MemoryConfig) -> Self

Sizes the hash table and the symbol buffer explicitly (see MemoryConfig), for many connections on little memory or for a little more ratio.

§Examples
use compress_lib::{Level, deflate::{self, Deflater, Flush, MemoryConfig}};

let lean = MemoryConfig::new().with_hash_bits(9).with_block_symbols(512);
let mut d = Deflater::new(Level::FAST).with_window_bits(9)?.with_memory(lean);
assert!(d.memory_in_use() < Deflater::new(Level::FAST).with_window_bits(9)?.memory_in_use());
let mut out = Vec::new();
d.compress_vec(b"small and lean", &mut out, Flush::Finish)?;
assert_eq!(deflate::decompress(&out)?, b"small and lean");
Source

pub fn level(&self) -> Level

The level in force.

§Examples
use compress_lib::{Level, deflate::Deflater};

assert_eq!(Deflater::default().level(), Level::DEFAULT);
Source

pub fn window_bits(&self) -> u8

The window size in bits.

§Examples
assert_eq!(compress_lib::deflate::Deflater::default().window_bits(), 15);
Source

pub fn set_level(&mut self, level: Level)

Changes the level for data not yet compressed. The current block is ended first when the change switches to or from level 0.

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

let mut deflater = Deflater::new(Level::BEST);
let mut out = Vec::new();
deflater.compress_vec(b"header ", &mut out, Flush::None)?;
deflater.set_level(Level::NONE);
deflater.compress_vec(b"body", &mut out, Flush::Finish)?;
assert_eq!(deflate::decompress(&out)?, b"header body");
Source

pub fn total_in(&self) -> u64

Uncompressed bytes taken since the deflater was built or reset.

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

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

pub fn total_out(&self) -> u64

Compressed bytes handed out since the deflater was built or reset.

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

let mut d = Deflater::default();
let mut out = Vec::new();
d.compress_vec(b"", &mut out, Flush::Finish)?;
assert_eq!(d.total_out(), out.len() as u64);
Source

pub fn memory_in_use(&self) -> usize

Heap bytes held: window, hash chains, symbols, pending output, and block scratch.

§Examples
let d = compress_lib::deflate::Deflater::default();
assert!(d.memory_in_use() > 64 * 1024);
Source

pub fn reset(&mut self)

Starts a new, unrelated stream, keeping the allocations.

§Examples
use compress_lib::deflate::{self, Deflater, Flush};

let mut d = Deflater::default();
let mut a = Vec::new();
d.compress_vec(b"first", &mut a, Flush::Finish)?;
d.reset();
let mut b = Vec::new();
d.compress_vec(b"second", &mut b, Flush::Finish)?;
assert_eq!(deflate::decompress(&b)?, b"second");
Source

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

Presets the history with dictionary (its last 2^window_bits bytes): matches may refer into it. The decoder must be given the same dictionary. Only before any input.

§Errors

ErrorKind::InvalidState after input has been given.

§Examples
use compress_lib::deflate::{Deflater, Flush, Inflater};

let dict = b"{\"type\":\"update\",\"value\":";
let mut d = Deflater::default();
d.set_dictionary(dict)?;
let mut packed = Vec::new();
d.compress_vec(b"{\"type\":\"update\",\"value\":42}", &mut packed, Flush::Finish)?;
let mut i = Inflater::new();
i.set_dictionary(dict)?;
let mut out = Vec::new();
i.finish_vec(&packed, &mut out)?;
assert_eq!(out, b"{\"type\":\"update\",\"value\":42}");
Source

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

Compresses as much of input as the window takes and writes compressed bytes to output.

With Flush::None the result is NeedsInput once all input is taken (output may lag behind), or NeedsOutput when output is full. With a flush, Done means all input is taken and the flush point is completely in output; on NeedsOutput, call again with the same flush and the input not yet consumed.

§Errors

ErrorKind::InvalidState when input is given after Finish completed (call reset for a new stream).

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

let mut d = Deflater::default();
let mut out = [0u8; 256];
let p = d.compress(b"some data", &mut out, Flush::Finish)?;
assert_eq!(p.status, Status::Done);
assert_eq!(deflate::decompress(&out[..p.produced])?, b"some data");
Source

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

Compresses input and appends to out until the flush point is written (with Flush::None, until all input is taken). Returns Done, or NeedsInput for Flush::None.

§Errors

As compress, plus ErrorKind::AllocationFailed when out cannot grow.

§Examples
use compress_lib::deflate::{self, Deflater, Flush};

let mut d = Deflater::default();
let mut out = Vec::new();
d.compress_vec(b"part one, ", &mut out, Flush::None)?;
d.compress_vec(b"part two", &mut out, Flush::Finish)?;
assert_eq!(deflate::decompress(&out)?, b"part one, part two");

Trait Implementations§

Source§

impl Debug for Deflater

Source§

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

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

impl Default for Deflater

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.