Struct zstd_safe::CCtx

source ·
pub struct CCtx<'a>(/* private fields */);
Expand description

Compression context

It is recommended to allocate a single context per thread and re-use it for many compression operations.

Implementations§

source§

impl<'a> CCtx<'a>

source

pub fn try_create() -> Option<Self>

Tries to create a new context.

Returns None if zstd returns a NULL pointer - may happen if allocation fails.

source

pub fn create() -> Self

Wrap ZSTD_createCCtx

§Panics

If zstd returns a NULL pointer.

source

pub fn compress<C: WriteBuf + ?Sized>( &mut self, dst: &mut C, src: &[u8], compression_level: CompressionLevel, ) -> SafeResult

Wraps the ZSTD_compressCCtx() function

source

pub fn compress2<C: WriteBuf + ?Sized>( &mut self, dst: &mut C, src: &[u8], ) -> SafeResult

Wraps the ZSTD_compress2() function.

source

pub fn compress_using_dict<C: WriteBuf + ?Sized>( &mut self, dst: &mut C, src: &[u8], dict: &[u8], compression_level: CompressionLevel, ) -> SafeResult

Wraps the ZSTD_compress_usingDict() function.

source

pub fn compress_using_cdict<C: WriteBuf + ?Sized>( &mut self, dst: &mut C, src: &[u8], cdict: &CDict<'_>, ) -> SafeResult

Wraps the ZSTD_compress_usingCDict() function.

source

pub fn init(&mut self, compression_level: CompressionLevel) -> SafeResult

Initializes the context with the given compression level.

This is equivalent to running:

  • reset()
  • set_parameter(CompressionLevel, compression_level)
source

pub fn load_dictionary(&mut self, dict: &[u8]) -> SafeResult

Tries 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 CDict first, then loads that.

The dictionary will apply to all compressed frames, until a new dictionary is set.

source

pub fn ref_cdict<'b>(&mut self, cdict: &CDict<'b>) -> SafeResult
where 'b: 'a,

Wraps the ZSTD_CCtx_refCDict() function.

Dictionary must outlive the context.

source

pub fn disable_dictionary(&mut self) -> SafeResult

Return to “no-dictionary” mode.

This will disable any dictionary/prefix previously registered for future frames.

source

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

source

pub fn compress_stream<C: WriteBuf + ?Sized>( &mut self, output: &mut OutBuffer<'_, C>, input: &mut InBuffer<'_>, ) -> SafeResult

Performs a step of a streaming compression operation.

This will read some data from input and/or write some data to output.

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

source

pub fn compress_stream2<C: WriteBuf + ?Sized>( &mut self, output: &mut OutBuffer<'_, C>, input: &mut InBuffer<'_>, end_op: ZSTD_EndDirective, ) -> SafeResult

Performs a step of a streaming compression operation.

This will read some data from input and/or write some data to output.

The end_op directive 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.

source

pub fn flush_stream<C: WriteBuf + ?Sized>( &mut self, output: &mut OutBuffer<'_, C>, ) -> SafeResult

Flush any intermediate buffer.

To fully flush, you should keep calling this function until it returns Ok(0).

Wraps the ZSTD_flushStream() function.

source

pub fn end_stream<C: WriteBuf + ?Sized>( &mut self, output: &mut OutBuffer<'_, C>, ) -> SafeResult

Ends the stream.

You should keep calling this function until it returns Ok(0).

Wraps the ZSTD_endStream() function.

source

pub fn sizeof(&self) -> usize

Returns the size currently used by this context.

This may change over time.

source

pub fn reset(&mut self, reset: ResetDirective) -> SafeResult

Resets the state of the context.

Depending on the reset mode, it can reset the session, the parameters, or both.

Wraps the ZSTD_CCtx_reset() function.

source

pub fn set_parameter(&mut self, param: CParameter) -> SafeResult

Sets a compression parameter.

Some of these parameters need to be set during de-compression as well.

source

pub fn set_pledged_src_size( &mut self, pledged_src_size: Option<u64>, ) -> SafeResult

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

source

pub fn in_size() -> usize

Returns the recommended input buffer size.

Using this size may result in minor performance boost.

source

pub fn out_size() -> usize

Returns the recommended output buffer size.

Using this may result in minor performance boost.

Trait Implementations§

source§

impl Default for CCtx<'_>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'a> Drop for CCtx<'a>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> Send for CCtx<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for CCtx<'a>

§

impl<'a> RefUnwindSafe for CCtx<'a>

§

impl<'a> !Sync for CCtx<'a>

§

impl<'a> Unpin for CCtx<'a>

§

impl<'a> UnwindSafe for CCtx<'a>

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>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.