Struct CCtx
pub struct CCtx<'a>(/* private field */, /* private field */, /* private field */, /* private field */);
Compression context
It is recommended to allocate a single context per thread and re-use it for many compression operations.
Implementations
impl<'a> CCtx<'a>
fn try_create() -> Option<Self>Tries to create a new context.
Returns
Noneif zstd returns a NULL pointer - may happen if allocation fails.With the
with-rust-allocatorfeature enabled, the context's internal allocations go through Rust's global allocator instead of the C runtime'smalloc.fn create() -> SelfWrap
ZSTD_createCCtxPanics
If zstd returns a NULL pointer.
fn compress<C: WriteBuf + ?Sized>(&mut self, dst: &mut C, src: &[u8], compression_level: CompressionLevel) -> SafeResultWraps the
ZSTD_compressCCtx()functionfn compress2<C: WriteBuf + ?Sized>(&mut self, dst: &mut C, src: &[u8]) -> SafeResultWraps the
ZSTD_compress2()function.fn compress_using_dict<C: WriteBuf + ?Sized>(&mut self, dst: &mut C, src: &[u8], dict: &[u8], compression_level: CompressionLevel) -> SafeResultWraps the
ZSTD_compress_usingDict()function.fn compress_using_cdict<C: WriteBuf + ?Sized>(&mut self, dst: &mut C, src: &[u8], cdict: &CDict<'_>) -> SafeResultWraps the
ZSTD_compress_usingCDict()function.fn init(&mut self, compression_level: CompressionLevel) -> SafeResultInitializes the context with the given compression level.
This is equivalent to running:
reset()set_parameter(CompressionLevel, compression_level)
Fails while a multithreaded frame is left unfinished, see [
CCtx::wait_for_workers()].fn load_dictionary(&mut self, dict: &[u8]) -> SafeResultTries to load a dictionary.
The dictionary content will be copied internally and does not need to be kept alive after calling this function.
If you need to use the same dictionary for multiple contexts, it may be more efficient to create a
CDictfirst, then loads that.The dictionary will apply to all compressed frames, until a new dictionary is set.
Fails while a multithreaded frame is left unfinished, see [
CCtx::wait_for_workers()].fn ref_cdict<'b>(&mut self, cdict: &'a CDict<'b>) -> SafeResult where 'b: 'a,Wraps the
ZSTD_CCtx_refCDict()function.Dictionary must outlive the context.
Fails while a multithreaded frame is left unfinished, see [
CCtx::wait_for_workers()].fn disable_dictionary(&mut self) -> SafeResultReturn to "no-dictionary" mode.
This will disable any dictionary/prefix previously registered for future frames.
Fails while a multithreaded frame is left unfinished, see [
CCtx::wait_for_workers()].fn ref_prefix<'b>(&mut self, prefix: &'b [u8]) -> SafeResult where 'b: 'a,Use some prefix as single-use dictionary for the next compressed frame.
Just like a dictionary, decompression will need to be given the same prefix.
This is best used if the "prefix" looks like the data to be compressed.
Fails while a multithreaded frame is left unfinished, see [
CCtx::wait_for_workers()].fn compress_stream<C: WriteBuf + ?Sized>(&mut self, output: &mut OutBuffer<'_, C>, input: &mut InBuffer<'_>) -> SafeResultPerforms a step of a streaming compression operation.
This will read some data from
inputand/or write some data tooutput.Returns
A hint for the "ideal" amount of input data to provide in the next call.
This hint is only for performance purposes.
Wraps the
ZSTD_compressStream()function.fn compress_stream2<C: WriteBuf + ?Sized>(&mut self, output: &mut OutBuffer<'_, C>, input: &mut InBuffer<'_>, end_op: ZSTD_EndDirective) -> SafeResultPerforms a step of a streaming compression operation.
This will read some data from
inputand/or write some data tooutput.The
end_opdirective can be used to specify what to do after: nothing special, flush internal buffers, or end the frame.Returns
An lower bound for the amount of data that still needs to be flushed out.
This is useful when flushing or ending the frame: you need to keep calling this function until it returns 0.
Wraps the
ZSTD_compressStream2()function.fn flush_stream<C: WriteBuf + ?Sized>(&mut self, output: &mut OutBuffer<'_, C>) -> SafeResultFlush any intermediate buffer.
To fully flush, you should keep calling this function until it returns
Ok(0).Wraps the
ZSTD_flushStream()function.fn end_stream<C: WriteBuf + ?Sized>(&mut self, output: &mut OutBuffer<'_, C>) -> SafeResultEnds the stream.
You should keep calling this function until it returns
Ok(0).Wraps the
ZSTD_endStream()function.fn wait_for_workers(&mut self) -> SafeResultAbandons the current frame, if any, and waits for the worker threads to be done with it.
When a multithreaded frame (see
CParameter::NbWorkers) is left unfinished - abandoned, or interrupted by an error - its jobs may still be running, reading the dictionary. zstd only waits for them when the next frame starts or when the context is dropped. Until then, anything that would unload the dictionary or changeNbWorkersfails with astage_wrongerror instead:load_dictionary(),ref_cdict(),ref_prefix(),disable_dictionary(),init(), andreset()with parameters.Calling this first lets them succeed. Like
reset(SessionOnly), it discards the current frame and any pledged source size, and keeps parameters and dictionary. It does nothing if no multithreaded frame is unfinished.fn sizeof(&self) -> usizeReturns the size currently used by this context.
This may change over time.
fn reset(&mut self, reset: ResetDirective) -> SafeResultResets the state of the context.
Depending on the reset mode, it can reset the session, the parameters, or both.
Resetting the parameters also unloads the dictionary: this fails while a multithreaded frame is left unfinished, see [
CCtx::wait_for_workers()]. Resetting only the session never does.Wraps the
ZSTD_CCtx_reset()function.fn set_parameter(&mut self, param: CParameter) -> SafeResultSets a compression parameter.
Some of these parameters need to be set during de-compression as well.
Changing
NbWorkersfails while a multithreaded frame is left unfinished, see [CCtx::wait_for_workers()].fn set_pledged_src_size(&mut self, pledged_src_size: Option<u64>) -> SafeResultGuarantee that the input size will be this value.
If given
None, assumes the size is unknown.Unless explicitly disabled, this will cause the size to be written in the compressed frame header.
If the actual data given to compress has a different size, an error will be returned.
fn in_size() -> usizeReturns the recommended input buffer size.
Using this size may result in minor performance boost.
fn out_size() -> usizeReturns the recommended output buffer size.
Using this may result in minor performance boost.
Trait Implementations
impl Default for CCtx<'_>
fn default() -> Self
impl Send for CCtx<'_>
impl Sync for CCtx<'_>
impl<'a> Drop for CCtx<'a>
fn drop(&mut self)