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
impl PushEncoder
Sourcepub fn new(level: i32) -> Result<Self, CompressError>
pub fn new(level: i32) -> Result<Self, CompressError>
Creates an encoder at the given level (-7..=4; -8 runs as -7).
Sourcepub fn with_options(level: i32, opts: &Options) -> Result<Self, CompressError>
pub fn with_options(level: i32, opts: &Options) -> Result<Self, CompressError>
Creates an encoder with Options for large
windows and/or LDM.
Sourcepub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError>
pub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError>
Creates an encoder with a dictionary.
Sourcepub fn encode(
&mut self,
input: &[u8],
output: &mut [u8],
op: EndOp,
) -> Result<Progress, CompressError>
pub fn encode( &mut self, input: &[u8], output: &mut [u8], op: EndOp, ) -> Result<Progress, CompressError>
Consumes input and writes frame bytes into output.
EndOp::Continue: returnsStatus::NeedsInputonce all input is consumed and every full block is delivered.EndOp::Flush: also ends the current block; returnsStatus::NeedsInputonce it is delivered.EndOp::End: also ends the frame; returnsStatus::Finishedonce its last byte is delivered. The next call starts a new frame.
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.
Sourcepub fn encode_to_vec(
&mut self,
input: &[u8],
output: &mut Vec<u8>,
op: EndOp,
) -> Result<(), CompressError>
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.
Sourcepub fn has_pending_output(&self) -> bool
pub fn has_pending_output(&self) -> bool
Whether frame bytes are still waiting to be delivered.