Struct Encoder

struct Encoder<'a, W: Write> { ... }

An encoder that compress and forward data to another writer.

This allows to compress a stream of data (good for files or heavy network stream).

Don't forget to call finish() before dropping it!

Alternatively, you can call auto_finish() to use an AutoFinishEncoder that will finish on drop.

Note: The zstd library has its own internal input buffer (~128kb).

Implementations

impl<'a, W: Write> Encoder<'a, W>

fn with_writer(writer: Writer<W, Encoder<'a>>) -> Self

Creates a new encoder from a prepared zio writer.

fn with_encoder(writer: W, encoder: Encoder<'a>) -> Self

Creates a new encoder from the given Write and raw encoder.

fn with_context(writer: W, context: &'a mut CCtx<'static>) -> Self

Creates an encoder that uses the provided context to compress a stream.

fn with_prepared_dictionary<'b>(writer: W, dictionary: &EncoderDictionary<'b>) -> Result<Self>
where
    'b: 'a

Creates a new encoder, using an existing prepared EncoderDictionary.

(Provides better compression ratio for small files, but requires the dictionary to be present during decompression.)

fn with_ref_prefix<'b>(writer: W, level: i32, ref_prefix: &'b [u8]) -> Result<Self>
where
    'b: 'a

Creates a new encoder, using a ref prefix

fn auto_finish(self: Self) -> AutoFinishEncoder<'a, W>

Returns a wrapper around self that will finish the stream on drop.

fn on_finish<F: FnMut(io::Result<W>)>(self: Self, f: F) -> AutoFinishEncoder<'a, W, F>

Returns an encoder that will finish the stream on drop.

Calls the given callback with the result from finish(). This runs during drop so it's important that the provided callback doesn't panic.

fn get_ref(self: &Self) -> &W

Acquires a reference to the underlying writer.

fn get_mut(self: &mut Self) -> &mut W

Acquires a mutable reference to the underlying writer.

Note that mutation of the writer may result in surprising results if this encoder is continued to be used.

fn finish(self: Self) -> Result<W>

Required: Finishes the stream.

You need to finish the stream when you're done writing, either with this method or with try_finish(self).

This returns the inner writer in case you need it.

To get back self in case an error happened, use try_finish.

Note: If you don't want (or can't) call finish() manually after writing your data, consider using auto_finish() to get an AutoFinishEncoder.

fn try_finish(self: Self) -> Result<W, (Self, Error)>

Required: Attempts to finish the stream.

You need to finish the stream when you're done writing, either with this method or with finish(self).

This returns the inner writer if the finish was successful, or the object plus an error if it wasn't.

write on this object will panic after try_finish has been called, even if it fails.

fn do_finish(self: &mut Self) -> Result<()>

Attempts to finish the stream.

You need to finish the stream when you're done writing, either with this method or with finish(self).

fn recommended_input_size() -> usize

Return a recommendation for the size of data to write at once.

fn set_parameter(self: &mut Self, parameter: CParameter) -> Result<()>

Sets the given zstd compression parameter.

fn set_pledged_src_size(self: &mut Self, size: Option<u64>) -> Result<()>

Sets the expected size of the input.

This affects the compression effectiveness.

It is an error to give an incorrect size (an error will be returned when closing the stream if the size does not match what was pledged).

Giving a None size means the size is unknown (this is the default).

fn include_checksum(self: &mut Self, include_checksum: bool) -> Result<()>

Controls whether zstd should include a content checksum at the end of each frame.

fn include_dictid(self: &mut Self, include_dictid: bool) -> Result<()>

Enables or disables storing of the dict id.

Defaults to true. If false, the behaviour of decoding with a wrong dictionary is undefined.

fn include_contentsize(self: &mut Self, include_contentsize: bool) -> Result<()>

Enables or disabled storing of the contentsize.

Note that this only has an effect if the size is given with set_pledged_src_size.

fn long_distance_matching(self: &mut Self, long_distance_matching: bool) -> Result<()>

Enables or disables long-distance matching

fn set_target_cblock_size(self: &mut Self, target_size: Option<u32>) -> Result<()>

Sets the target size for compressed blocks.

A lower block size may result in slightly lower speed (~2%) and compression ratio (~0.1%), but may decrease end-to-end latency in low-bandwidth environments (time to first decompressed byte).

No value, or a value of zero, results in no contraint for the block sizes.

fn window_log(self: &mut Self, log_distance: u32) -> Result<()>

Sets the maximum back-reference distance.

The actual maximum distance is going to be 2^log_distance.

Note that decompression will need to use at least the same setting.

impl<W: Write> Encoder<'static, W>

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

Creates a new encoder.

level: compression level (1-22).

A level of 0 uses zstd's default (currently 3).

fn with_dictionary(writer: W, level: i32, dictionary: &[u8]) -> Result<Self>

Creates a new encoder, using an existing dictionary.

(Provides better compression ratio for small files, but requires the dictionary to be present during decompression.)

A level of 0 uses zstd's default (currently 3).

impl<'a, W> Freeze for Encoder<'a, W>

impl<'a, W> RefUnwindSafe for Encoder<'a, W>

impl<'a, W> Send for Encoder<'a, W>

impl<'a, W> Sync for Encoder<'a, W>

impl<'a, W> Unpin for Encoder<'a, W>

impl<'a, W> UnsafeUnpin for Encoder<'a, W>

impl<'a, W> UnwindSafe for Encoder<'a, W>

impl<'a, W: Write> Write for Encoder<'a, W>

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

impl<T> Any for Encoder<'a, W>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Encoder<'a, W>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Encoder<'a, W>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for Encoder<'a, W>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Encoder<'a, W>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Encoder<'a, W>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Encoder<'a, W>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>