async_compression_issue_150_workaround/
lib.rs

1//! Adaptors between compression crates and Rust's modern asynchronous IO types.
2//!
3
4//! # Feature Organization
5//!
6//! This crate is divided up along two axes, which can each be individually selected via Cargo
7//! features.
8//!
9//! All features are disabled by default, you should enable just the ones you need from the lists
10//! below.
11//!
12//! If you want to pull in everything there are three group features defined:
13//!
14
15//!  Feature | Does
16//! ---------|------
17//!  `all`   | Activates all implementations and algorithms.
18//!  `all-implementations` | Activates all implementations, needs to be paired with a selection of algorithms
19//!  `all-algorithms` | Activates all algorithms, needs to be paired with a selection of implementations
20//!
21
22//! ## IO implementation
23//!
24//! The first division is which underlying asynchronous IO trait will be wrapped, these are
25//! available as separate features that have corresponding top-level modules:
26//!
27
28//!  Feature | Type
29//! ---------|------
30// TODO: Kill rustfmt on this section, `#![rustfmt::skip::attributes(cfg_attr)]` should do it, but
31// that's unstable
32#![cfg_attr(
33    feature = "futures-io",
34    doc = "[`futures-io`](crate::futures) | [`futures::io::AsyncBufRead`](futures_io::AsyncBufRead), [`futures::io::AsyncWrite`](futures_io::AsyncWrite)"
35)]
36#![cfg_attr(
37    not(feature = "futures-io"),
38    doc = "`futures-io` (*inactive*) | `futures::io::AsyncBufRead`, `futures::io::AsyncWrite`"
39)]
40#![cfg_attr(
41    feature = "futures-bufread",
42    doc = "`futures-bufread` | (*deprecated*, use `futures-io`)"
43)]
44#![cfg_attr(
45    feature = "futures-write",
46    doc = "`futures-write` | (*deprecated*, use `futures-io`)"
47)]
48#![cfg_attr(
49    feature = "stream",
50    doc = "[`stream`] | (*deprecated*, see [`async-compression:stream`](crate::stream) docs for migration)"
51)]
52#![cfg_attr(
53    not(feature = "stream"),
54    doc = "`stream` (*inactive*) | (*deprecated*, see `async-compression::stream` docs for migration)"
55)]
56#![cfg_attr(
57    feature = "tokio-02",
58    doc = "[`tokio-02`](crate::tokio_02) | [`tokio::io::AsyncBufRead`](::tokio_02::io::AsyncBufRead), [`tokio::io::AsyncWrite`](::tokio_02::io::AsyncWrite)"
59)]
60#![cfg_attr(
61    not(feature = "tokio-02"),
62    doc = "`tokio-02` (*inactive*) | `tokio::io::AsyncBufRead`, `tokio::io::AsyncWrite`"
63)]
64#![cfg_attr(
65    feature = "tokio-03",
66    doc = "[`tokio-03`](crate::tokio_03) | [`tokio::io::AsyncBufRead`](::tokio_03::io::AsyncBufRead), [`tokio::io::AsyncWrite`](::tokio_03::io::AsyncWrite)"
67)]
68#![cfg_attr(
69    not(feature = "tokio-03"),
70    doc = "`tokio-03` (*inactive*) | `tokio::io::AsyncBufRead`, `tokio::io::AsyncWrite`"
71)]
72#![cfg_attr(
73    feature = "tokio",
74    doc = "[`tokio`](crate::tokio) | [`tokio::io::AsyncBufRead`](::tokio::io::AsyncBufRead), [`tokio::io::AsyncWrite`](::tokio::io::AsyncWrite)"
75)]
76#![cfg_attr(
77    not(feature = "tokio"),
78    doc = "`tokio` (*inactive*) | `tokio::io::AsyncBufRead`, `tokio::io::AsyncWrite`"
79)]
80//!
81
82//! ## Compression algorithm
83//!
84//! The second division is which compression schemes to support, there are currently a few
85//! available choices, these determine which types will be available inside the above modules:
86//!
87
88//!  Feature | Types
89//! ---------|------
90#![cfg_attr(
91    feature = "brotli",
92    doc = "`brotli` | [`BrotliEncoder`](?search=BrotliEncoder), [`BrotliDecoder`](?search=BrotliDecoder)"
93)]
94#![cfg_attr(
95    not(feature = "brotli"),
96    doc = "`brotli` (*inactive*) | `BrotliEncoder`, `BrotliDecoder`"
97)]
98#![cfg_attr(
99    feature = "bzip2",
100    doc = "`bzip2` | [`BzEncoder`](?search=BzEncoder), [`BzDecoder`](?search=BzDecoder)"
101)]
102#![cfg_attr(
103    not(feature = "bzip2"),
104    doc = "`bzip2` (*inactive*) | `BzEncoder`, `BzDecoder`"
105)]
106#![cfg_attr(
107    feature = "deflate",
108    doc = "`deflate` | [`DeflateEncoder`](?search=DeflateEncoder), [`DeflateDecoder`](?search=DeflateDecoder)"
109)]
110#![cfg_attr(
111    not(feature = "deflate"),
112    doc = "`deflate` (*inactive*) | `DeflateEncoder`, `DeflateDecoder`"
113)]
114#![cfg_attr(
115    feature = "gzip",
116    doc = "`gzip` | [`GzipEncoder`](?search=GzipEncoder), [`GzipDecoder`](?search=GzipDecoder)"
117)]
118#![cfg_attr(
119    not(feature = "gzip"),
120    doc = "`gzip` (*inactive*) | `GzipEncoder`, `GzipDecoder`"
121)]
122#![cfg_attr(
123    feature = "lzma",
124    doc = "`lzma` | [`LzmaEncoder`](?search=LzmaEncoder), [`LzmaDecoder`](?search=LzmaDecoder)"
125)]
126#![cfg_attr(
127    not(feature = "lzma"),
128    doc = "`lzma` (*inactive*) | `LzmaEncoder`, `LzmaDecoder`"
129)]
130#![cfg_attr(
131    feature = "xz",
132    doc = "`xz` | [`XzEncoder`](?search=XzEncoder), [`XzDecoder`](?search=XzDecoder)"
133)]
134#![cfg_attr(
135    not(feature = "xz"),
136    doc = "`xz` (*inactive*) | `XzEncoder`, `XzDecoder`"
137)]
138#![cfg_attr(
139    feature = "zlib",
140    doc = "`zlib` | [`ZlibEncoder`](?search=ZlibEncoder), [`ZlibDecoder`](?search=ZlibDecoder)"
141)]
142#![cfg_attr(
143    not(feature = "zlib"),
144    doc = "`zlib` (*inactive*) | `ZlibEncoder`, `ZlibDecoder`"
145)]
146#![cfg_attr(
147    feature = "zstd",
148    doc = "`zstd` | [`ZstdEncoder`](?search=ZstdEncoder), [`ZstdDecoder`](?search=ZstdDecoder)"
149)]
150#![cfg_attr(
151    not(feature = "zstd"),
152    doc = "`zstd` (*inactive*) | `ZstdEncoder`, `ZstdDecoder`"
153)]
154//!
155
156#![cfg_attr(docsrs, feature(doc_cfg))]
157#![warn(rust_2018_idioms)]
158#![cfg_attr(not(all), allow(unused))]
159
160#[macro_use]
161mod macros;
162pub mod codec;
163
164#[cfg(feature = "futures-io")]
165#[cfg_attr(docsrs, doc(cfg(feature = "futures-io")))]
166pub mod futures;
167#[cfg(feature = "stream")]
168#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
169pub mod stream;
170#[cfg(feature = "tokio")]
171#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
172pub mod tokio;
173#[cfg(feature = "tokio-02")]
174#[cfg_attr(docsrs, doc(cfg(feature = "tokio-02")))]
175pub mod tokio_02;
176#[cfg(feature = "tokio-03")]
177#[cfg_attr(docsrs, doc(cfg(feature = "tokio-03")))]
178pub mod tokio_03;
179
180mod unshared;
181pub mod util;
182
183#[cfg(feature = "brotli")]
184use brotli::enc::backward_references::BrotliEncoderParams;
185
186/// Level of compression data should be compressed with.
187#[non_exhaustive]
188#[derive(Clone, Copy, Debug)]
189pub enum Level {
190    /// Fastest quality of compression, usually produces bigger size.
191    Fastest,
192    /// Best quality of compression, usually produces the smallest size.
193    Best,
194    /// Default quality of compression defined by the selected compression algorithm.
195    Default,
196    /// Precise quality based on the underlying compression algorithms'
197    /// qualities. The interpretation of this depends on the algorithm chosen
198    /// and the specific implementation backing it.
199    /// Qualities are implicitly clamped to the algorithm's maximum.
200    Precise(u32),
201}
202
203impl Level {
204    #[cfg(feature = "brotli")]
205    fn into_brotli(self, mut params: BrotliEncoderParams) -> BrotliEncoderParams {
206        match self {
207            Self::Fastest => params.quality = 0,
208            Self::Best => params.quality = 11,
209            Self::Precise(quality) => params.quality = quality.min(11) as i32,
210            Self::Default => (),
211        }
212
213        params
214    }
215
216    #[cfg(feature = "bzip2")]
217    fn into_bzip2(self) -> bzip2::Compression {
218        match self {
219            Self::Fastest => bzip2::Compression::fast(),
220            Self::Best => bzip2::Compression::best(),
221            Self::Precise(quality) => bzip2::Compression::new(quality.max(1).min(9)),
222            Self::Default => bzip2::Compression::default(),
223        }
224    }
225
226    #[cfg(feature = "flate2")]
227    fn into_flate2(self) -> flate2::Compression {
228        match self {
229            Self::Fastest => flate2::Compression::fast(),
230            Self::Best => flate2::Compression::best(),
231            Self::Precise(quality) => flate2::Compression::new(quality.min(10)),
232            Self::Default => flate2::Compression::default(),
233        }
234    }
235
236    #[cfg(feature = "zstd")]
237    fn into_zstd(self) -> i32 {
238        match self {
239            Self::Fastest => 1,
240            Self::Best => 21,
241            Self::Precise(quality) => quality.min(21) as i32,
242            Self::Default => 0,
243        }
244    }
245
246    #[cfg(feature = "xz2")]
247    fn into_xz2(self) -> u32 {
248        match self {
249            Self::Fastest => 0,
250            Self::Best => 9,
251            Self::Precise(quality) => quality.min(9),
252            Self::Default => 5,
253        }
254    }
255}