async_compression/
zstd.rs

1//! This module contains zstd-specific types for async-compression.
2
3use libzstd::stream::raw::CParameter::*;
4
5/// A compression parameter for zstd. This is a stable wrapper around zstd's own `CParameter`
6/// type, to abstract over different versions of the zstd library.
7///
8/// See the [zstd documentation](https://facebook.github.io/zstd/zstd_manual.html) for more
9/// information on these parameters.
10#[derive(Copy, Clone, Debug, PartialEq, Eq)]
11pub struct CParameter(libzstd::stream::raw::CParameter);
12
13impl CParameter {
14    /// Window size in bytes (as a power of two)
15    pub fn window_log(value: u32) -> Self {
16        Self(WindowLog(value))
17    }
18
19    /// Size of the initial probe table in 4-byte entries (as a power of two)
20    pub fn hash_log(value: u32) -> Self {
21        Self(HashLog(value))
22    }
23
24    /// Size of the multi-probe table in 4-byte entries (as a power of two)
25    pub fn chain_log(value: u32) -> Self {
26        Self(ChainLog(value))
27    }
28
29    /// Number of search attempts (as a power of two)
30    pub fn search_log(value: u32) -> Self {
31        Self(SearchLog(value))
32    }
33
34    /// Minimum size of matches searched for
35    pub fn min_match(value: u32) -> Self {
36        Self(MinMatch(value))
37    }
38
39    /// Strategy-dependent length modifier
40    pub fn target_length(value: u32) -> Self {
41        Self(TargetLength(value))
42    }
43
44    /// Enable long-distance matching mode to look for and emit long-distance references.
45    ///
46    /// This increases the default window size.
47    pub fn enable_long_distance_matching(value: bool) -> Self {
48        Self(EnableLongDistanceMatching(value))
49    }
50
51    /// Size of the long-distance matching table (as a power of two)
52    pub fn ldm_hash_log(value: u32) -> Self {
53        Self(LdmHashLog(value))
54    }
55
56    /// Minimum size of long-distance matches searched for
57    pub fn ldm_min_match(value: u32) -> Self {
58        Self(LdmMinMatch(value))
59    }
60
61    /// Size of each bucket in the LDM hash table for collision resolution (as a power of two)
62    pub fn ldm_bucket_size_log(value: u32) -> Self {
63        Self(LdmBucketSizeLog(value))
64    }
65
66    /// Frequency of using the LDM hash table (as a power of two)
67    pub fn ldm_hash_rate_log(value: u32) -> Self {
68        Self(LdmHashRateLog(value))
69    }
70
71    /// Emit the size of the content (default: true).
72    pub fn content_size_flag(value: bool) -> Self {
73        Self(ContentSizeFlag(value))
74    }
75
76    /// Emit a checksum (default: false).
77    pub fn checksum_flag(value: bool) -> Self {
78        Self(ChecksumFlag(value))
79    }
80
81    /// Emit a dictionary ID when using a custom dictionary (default: true).
82    pub fn dict_id_flag(value: bool) -> Self {
83        Self(DictIdFlag(value))
84    }
85
86    /// Number of threads to spawn.
87    ///
88    /// If set to 0, compression functions will block; if set to 1 or more, compression will
89    /// run in background threads and `flush` pushes bytes through the compressor.
90    ///
91    /// # Panics
92    ///
93    /// This parameter requires feature `zstdmt` to be enabled, otherwise it will cause a panic
94    /// when used in `ZstdEncoder::with_quality_and_params()` calls.
95    //
96    // TODO: make this a normal feature guarded fn on next breaking release
97    #[cfg_attr(docsrs, doc(cfg(feature = "zstdmt")))]
98    pub fn nb_workers(value: u32) -> Self {
99        Self(NbWorkers(value))
100    }
101
102    /// Number of bytes given to each worker.
103    ///
104    /// If set to 0, zstd selects a job size based on compression parameters.
105    pub fn job_size(value: u32) -> Self {
106        Self(JobSize(value))
107    }
108
109    pub(crate) fn as_zstd(&self) -> libzstd::stream::raw::CParameter {
110        self.0
111    }
112}