Skip to main content

PushEncoder

Struct PushEncoder 

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

Push-style zstd compressor.

Feed input in chunks of any size with encode and drain frame bytes into buffers of any size. No I/O, no std; it needs only the alloc feature. FrameEncoder is the std::io::Write counterpart.

Frames carry a content checksum. A frame that ends before any of it was written, such as all input passed with EndOp::End, is encoded in one pass like zrip::compress: same bytes, content size in the header, and tables sized to the input. Longer streams have no content size. Hash tables, window and block buffers are allocated on first use and reused for every block and every frame.

After an error, every call returns that error until reset.

use zrip::push::{EndOp, PushEncoder, Status};

let data = b"hello, push world! ".repeat(1000);
let mut enc = PushEncoder::new(1).unwrap();
let mut frame = Vec::new();
let mut buf = [0u8; 256];
for chunk in data.chunks(100) {
    let mut input = chunk;
    while !input.is_empty() {
        let p = enc.encode(input, &mut buf, EndOp::Continue).unwrap();
        frame.extend_from_slice(&buf[..p.produced]);
        input = &input[p.consumed..];
    }
}
loop {
    let p = enc.encode(&[], &mut buf, EndOp::End).unwrap();
    frame.extend_from_slice(&buf[..p.produced]);
    if p.status == Status::Finished {
        break;
    }
}
assert_eq!(zrip::decompress(&frame).unwrap(), data);

Implementations§

Source§

impl PushEncoder

Source

pub fn new(level: i32) -> Result<Self, CompressError>

Creates an encoder at the given level (-7..=4; -8 runs as -7).

Source

pub fn with_options(level: i32, opts: &Options) -> Result<Self, CompressError>

Creates an encoder with Options for large windows and/or LDM.

Source

pub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError>

Creates an encoder with a dictionary.

Source

pub fn encode( &mut self, input: &[u8], output: &mut [u8], op: EndOp, ) -> Result<Progress, CompressError>

Consumes input and writes frame bytes into output.

Status::NeedsOutput means output filled up first: call again with the rest of the input (consumed tells how much was taken) and the same op.

Source

pub fn encode_to_vec( &mut self, input: &[u8], output: &mut Vec<u8>, op: EndOp, ) -> Result<(), CompressError>

Encodes all of input and appends the frame bytes to output. With EndOp::End, output then ends with a complete frame.

Bytes still pending from encode go out first. If they end a frame, input starts the next one.

Source

pub fn has_pending_output(&self) -> bool

Whether frame bytes are still waiting to be delivered.

Source

pub fn reset(&mut self)

Discards the current frame and any error, keeping allocations. The next call starts a new frame.

Trait Implementations§

Source§

impl Debug for PushEncoder

Source§

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

Formats the value using the given formatter. 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, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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.