pub struct Encoder<W: Write> { /* private fields */ }std only.Expand description
Compresses into a writer (std::io::Write), like the zstd crate’s
Encoder: one zstd frame, ended by finish. The
output is byte-identical to libzstd 1.6.0’s streaming compressor’s
(ZSTD_compressStream2) for the same level, writes and flushes.
use std::io::Write;
let mut enc = zstd_native::Encoder::new(Vec::new(), 3);
enc.write_all(b"hello hello hello hello")?;
let compressed = enc.finish()?;
assert_eq!(zstd_native::decompress(&compressed).unwrap(), b"hello hello hello hello");flush compresses the data written so far and writes
it out (ZSTD_e_flush), without ending the frame. Dropping the encoder
without calling finish leaves the frame
incomplete. Errors are io::ErrorKind::InvalidInput for a pledged
size that doesn’t match, io::ErrorKind::InvalidData for the others
(the source is an Error).
Implementations§
Source§impl<W: Write> Encoder<W>
impl<W: Write> Encoder<W>
Sourcepub fn with_dictionary(writer: W, level: i32, dict: &[u8]) -> Result<Self>
pub fn with_dictionary(writer: W, level: i32, dict: &[u8]) -> Result<Self>
Compresses at level with the dictionary dict (see
Compressor::set_dictionary)
into writer, like the zstd crate’s Encoder::with_dictionary.
use std::io::Write;
let dict = b"hello world, hello there";
let mut enc = zstd_native::Encoder::with_dictionary(Vec::new(), 3, dict)?;
enc.write_all(b"hello there, hello world")?;
let compressed = enc.finish()?;
let dict = zstd_native::Dictionary::new(dict).unwrap();
let mut d = zstd_native::Decompressor::with_dictionary(dict);
assert_eq!(d.decompress(&compressed).unwrap(), b"hello there, hello world");§Errors
io::ErrorKind::InvalidData if a zstd-format dictionary is
corrupt.
Sourcepub fn with_prepared_dictionary(writer: W, dict: &EncoderDictionary) -> Self
pub fn with_prepared_dictionary(writer: W, dict: &EncoderDictionary) -> Self
Compresses with a prepared dictionary, at its level, into writer
(the zstd crate’s Encoder::with_prepared_dictionary).
Sourcepub fn set_pledged_src_size(&mut self, size: Option<u64>) -> Result<()>
pub fn set_pledged_src_size(&mut self, size: Option<u64>) -> Result<()>
Declares the size of the data to be written (see
StreamEncoder::set_pledged_src_size): the frame header then
holds it. Must be called before the first write.
§Errors
io::ErrorKind::InvalidInput after the first write.
Sourcepub fn set_checksum(&mut self, on: bool) -> Result<()>
pub fn set_checksum(&mut self, on: bool) -> Result<()>
Ends the frame with a content checksum (see
StreamEncoder::set_checksum; the zstd crate’s
Encoder::include_checksum). Must be called before the first write.
use std::io::Write;
let mut enc = zstd_native::Encoder::new(Vec::new(), 3);
enc.set_checksum(true)?;
enc.write_all(b"checked, checked, checked")?;
let compressed = enc.finish()?;
assert_eq!(zstd_native::decompress(&compressed).unwrap(), b"checked, checked, checked");§Errors
io::ErrorKind::InvalidInput after the first write.
Sourcepub fn set_content_size_flag(&mut self, on: bool) -> Result<()>
pub fn set_content_size_flag(&mut self, on: bool) -> Result<()>
Writes the content size into the frame header when it is pledged
(see StreamEncoder::set_content_size_flag; the zstd crate’s
Encoder::include_contentsize). Must be called before the first
write.
§Errors
io::ErrorKind::InvalidInput after the first write.
Sourcepub fn set_dict_id_flag(&mut self, on: bool) -> Result<()>
pub fn set_dict_id_flag(&mut self, on: bool) -> Result<()>
Writes the dictionary’s ID into the frame header (see
StreamEncoder::set_dict_id_flag; the zstd crate’s
Encoder::include_dictid). Must be called before the first write.
§Errors
io::ErrorKind::InvalidInput after the first write.
Sourcepub fn set_workers(&mut self, n: u32) -> Result<()>
pub fn set_workers(&mut self, n: u32) -> Result<()>
Compresses with n worker threads (see
StreamEncoder::set_workers; the zstd crate’s
Encoder::multithread), with libzstd’s output for the same worker
count. Must be called before the first write.
use std::io::Write;
let data = b"hello hello hello hello ".repeat(100_000);
let mut enc = zstd_native::Encoder::new(Vec::new(), 3);
enc.set_workers(4)?;
enc.write_all(&data)?;
let compressed = enc.finish()?;
assert_eq!(zstd_native::decompress(&compressed).unwrap(), data);§Errors
io::ErrorKind::InvalidInput after the first write.
Sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&mut self) -> &mut W
The underlying writer, mutably (writing to it directly corrupts the compressed stream).
Sourcepub fn finish(self) -> Result<W>
pub fn finish(self) -> Result<W>
Ends the frame (ZSTD_e_end), writes out everything and returns the
writer.
§Errors
As the writer’s, or as StreamEncoder::compress.
Trait Implementations§
Source§impl<W: Write> Write for Encoder<W>
impl<W: Write> Write for Encoder<W>
Source§fn write(&mut self, data: &[u8]) -> Result<usize>
fn write(&mut self, data: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)