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