Skip to main content

Encoder

Struct Encoder 

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

A streaming gzip encoder (one member).

§Examples

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

let mut enc = gzip::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!(gzip::decompress(&packed)?, b"part one, part two");

Implementations§

Source§

impl Encoder

Source

pub fn new(level: Level) -> Self

An encoder at level with the plain header of GzipHeader::new.

§Examples
let enc = compress_lib::gzip::Encoder::new(compress_lib::Level::BEST);
Source

pub fn with_header(self, header: GzipHeader) -> Self

Writes header instead of the plain one.

§Examples
use compress_lib::{Level, gzip::{Encoder, GzipHeader}};

let enc = Encoder::new(Level::DEFAULT).with_header(GzipHeader::new().with_name(b"a.txt"));
Source

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

Sets the window to 2^bits bytes, 8 to 15.

§Errors

ErrorKind::InvalidWindowBits outside 8 to 15.

§Examples
let enc = compress_lib::gzip::Encoder::default().with_window_bits(12)?;
Source

pub fn reset(&mut self)

Starts a new member, keeping allocations and the header.

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

pub fn total_in(&self) -> u64

Uncompressed bytes taken.

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

let mut enc = gzip::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::gzip::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 CRC-32 and length after Finish.

§Errors

As Deflater::compress.

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

let mut enc = gzip::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!(gzip::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, gzip};

let mut out = Vec::new();
gzip::Encoder::default().compress_vec(b"abc", &mut out, Flush::Finish)?;
assert_eq!(gzip::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.