pub struct Encoder<'a, W: Write> { /* private fields */ }
Expand description
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
sourceimpl<W: Write> Encoder<'static, W>
impl<W: Write> Encoder<'static, W>
sourcepub fn new(writer: W, level: i32) -> Result<Self>
pub fn new(writer: W, level: i32) -> Result<Self>
Creates a new encoder.
level
: compression level (1-21).
A level of 0
uses zstd’s default (currently 3
).
sourcepub fn with_dictionary(writer: W, level: i32, dictionary: &[u8]) -> Result<Self>
pub 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
).
sourceimpl<'a, W: Write> Encoder<'a, W>
impl<'a, W: Write> Encoder<'a, W>
sourcepub fn with_prepared_dictionary<'b>(
writer: W,
dictionary: &EncoderDictionary<'b>
) -> Result<Self> where
'b: 'a,
pub 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.)
sourcepub fn auto_finish(self) -> AutoFinishEncoder<'a, W>ⓘNotable traits for AutoFinishEncoder<'_, W, F>impl<W: Write, F: FnMut(Result<W>)> Write for AutoFinishEncoder<'_, W, F>
pub fn auto_finish(self) -> AutoFinishEncoder<'a, W>ⓘNotable traits for AutoFinishEncoder<'_, W, F>impl<W: Write, F: FnMut(Result<W>)> Write for AutoFinishEncoder<'_, W, F>
Returns a wrapper around self
that will finish the stream on drop.
Panic
Panics on drop if an error happens when finishing the stream.
sourcepub fn on_finish<F: FnMut(Result<W>)>(self, f: F) -> AutoFinishEncoder<'a, W, F>ⓘNotable traits for AutoFinishEncoder<'_, W, F>impl<W: Write, F: FnMut(Result<W>)> Write for AutoFinishEncoder<'_, W, F>
pub fn on_finish<F: FnMut(Result<W>)>(self, f: F) -> AutoFinishEncoder<'a, W, F>ⓘNotable traits for AutoFinishEncoder<'_, W, F>impl<W: Write, F: FnMut(Result<W>)> Write for AutoFinishEncoder<'_, W, F>
Returns an encoder that will finish the stream on drop.
Calls the given callback with the result from finish()
.
sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&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.
sourcepub fn finish(self) -> Result<W>
pub fn finish(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
.
sourcepub fn try_finish(self) -> Result<W, (Self, Error)>
pub fn try_finish(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.
sourcepub fn do_finish(&mut self) -> Result<()>
pub fn do_finish(&mut self) -> Result<()>
Attemps to finish the stream.
You need to finish the stream when you’re done writing, either with
this method or with finish(self)
.
sourcepub fn recommended_input_size() -> usize
pub fn recommended_input_size() -> usize
Return a recommendation for the size of data to write at once.
sourcepub fn set_parameter(&mut self, parameter: CParameter) -> Result<()>
pub fn set_parameter(&mut self, parameter: CParameter) -> Result<()>
Sets the given zstd compression parameter.
sourcepub fn set_pledged_src_size(&mut self, size: Option<u64>) -> Result<()>
pub fn set_pledged_src_size(&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).
sourcepub fn include_checksum(&mut self, include_checksum: bool) -> Result<()>
pub fn include_checksum(&mut self, include_checksum: bool) -> Result<()>
Controls whether zstd should include a content checksum at the end of each frame.
sourcepub fn multithread(&mut self, n_workers: u32) -> Result<()>
Available on crate feature zstdmt
only.
pub fn multithread(&mut self, n_workers: u32) -> Result<()>
zstdmt
only.Enables multithreaded compression
- If
n_workers == 0
(default), then multithreaded will be disabled. - If
n_workers >= 1
, then compression will be done in separate threads.
So even n_workers = 1
may increase performance by separating
IO and compression.
Note: This is only available if the zstdmt
cargo feature is activated.
sourcepub fn include_dictid(&mut self, include_dictid: bool) -> Result<()>
pub fn include_dictid(&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.
sourcepub fn include_contentsize(&mut self, include_contentsize: bool) -> Result<()>
pub fn include_contentsize(&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
.
sourcepub fn long_distance_matching(
&mut self,
long_distance_matching: bool
) -> Result<()>
pub fn long_distance_matching(
&mut self,
long_distance_matching: bool
) -> Result<()>
Enables or disables long-distance matching
sourcepub fn window_log(&mut self, log_distance: u32) -> Result<()>
pub fn window_log(&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.
sourcepub fn include_magicbytes(&mut self, include_magicbytes: bool) -> Result<()>
Available on crate feature experimental
only.
pub fn include_magicbytes(&mut self, include_magicbytes: bool) -> Result<()>
experimental
only.Enables or disable the magic bytes at the beginning of each frame.
If disabled, include_magicbytes must also be called on the decoder.
Only available with the experimental
feature.
Note that decompression will need to use the same setting.
Trait Implementations
sourceimpl<'a, W: Write> Write for Encoder<'a, W>
impl<'a, W: Write> Write for Encoder<'a, W>
sourcefn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this writer, returning how many bytes were written. Read more
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations
impl<'a, W> RefUnwindSafe for Encoder<'a, W> where
W: RefUnwindSafe,
impl<'a, W> Send for Encoder<'a, W> where
W: Send,
impl<'a, W> !Sync for Encoder<'a, W>
impl<'a, W> Unpin for Encoder<'a, W> where
W: Unpin,
impl<'a, W> UnwindSafe for Encoder<'a, W> where
W: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more