Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder<W: Write> { /* private fields */ }
Available on crate feature 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.5.7’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>

Source

pub fn new(writer: W, level: i32) -> Self

Compresses at level (see compress) into writer.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_ref(&self) -> &W

The underlying writer.

Source

pub fn get_mut(&mut self) -> &mut W

The underlying writer, mutably (writing to it directly corrupts the compressed stream).

Source

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: Debug + Write> Debug for Encoder<W>

Source§

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

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

impl<W: Write> Write for Encoder<W>

Source§

fn write(&mut self, data: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<W> !RefUnwindSafe for Encoder<W>

§

impl<W> !UnwindSafe for Encoder<W>

§

impl<W> Forget for Encoder<W>

§

impl<W> Freeze for Encoder<W>
where W: Freeze,

§

impl<W> Send for Encoder<W>
where W: Send,

§

impl<W> Sync for Encoder<W>
where W: Sync,

§

impl<W> Unpin for Encoder<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for Encoder<W>
where W: UnsafeUnpin,

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.