pub struct Encoder<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!
Note: The zstd library has its own internal input buffer (~128kb).
Implementations
sourceimpl<W: Write> Encoder<W>
impl<W: Write> Encoder<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
).
sourcepub fn with_prepared_dictionary(
writer: W,
dictionary: &EncoderDictionary<'_>
) -> Result<Self>
pub fn with_prepared_dictionary(
writer: W,
dictionary: &EncoderDictionary<'_>
) -> Result<Self>
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<W>ⓘNotable traits for AutoFinishEncoder<W>impl<W: Write> Write for AutoFinishEncoder<W>
pub fn auto_finish(self) -> AutoFinishEncoder<W>ⓘNotable traits for AutoFinishEncoder<W>impl<W: Write> Write for AutoFinishEncoder<W>
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: 'static + FnMut(Result<W>)>(
self,
f: F
) -> AutoFinishEncoder<W>ⓘNotable traits for AutoFinishEncoder<W>impl<W: Write> Write for AutoFinishEncoder<W>
pub fn on_finish<F: 'static + FnMut(Result<W>)>(
self,
f: F
) -> AutoFinishEncoder<W>ⓘNotable traits for AutoFinishEncoder<W>impl<W: Write> Write for AutoFinishEncoder<W>
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.
Trait Implementations
sourceimpl<W: Write> Write for Encoder<W>
impl<W: Write> Write for Encoder<W>
sourcefn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
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
)