1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
macro_rules! io_algo {
($impl:ident, $algo:ident($encoder:ident, $decoder:ident)) => {
pub mod $impl {
pub mod read {
pub use crate::utils::impls::$impl::read::{poll_read, to_vec};
}
pub mod bufread {
pub use crate::utils::impls::$impl::bufread::{from, AsyncBufRead};
pub use async_compression::$impl::bufread::{
$decoder as Decoder, $encoder as Encoder,
};
use crate::utils::{pin_mut, Level};
pub fn compress(input: impl AsyncBufRead) -> Vec<u8> {
pin_mut!(input);
super::read::to_vec(Encoder::with_quality(input, Level::Fastest))
}
pub fn decompress(input: impl AsyncBufRead) -> Vec<u8> {
pin_mut!(input);
super::read::to_vec(Decoder::new(input))
}
}
pub mod write {
pub use crate::utils::impls::$impl::write::to_vec;
pub use async_compression::$impl::write::{
$decoder as Decoder, $encoder as Encoder,
};
use crate::utils::Level;
pub fn compress(input: &[Vec<u8>], limit: usize) -> Vec<u8> {
to_vec(
input,
|input| Box::pin(Encoder::with_quality(input, Level::Fastest)),
limit,
)
}
pub fn decompress(input: &[Vec<u8>], limit: usize) -> Vec<u8> {
to_vec(input, |input| Box::pin(Decoder::new(input)), limit)
}
}
}
};
}
macro_rules! algos {
($(pub mod $name:ident($feat:literal, $encoder:ident, $decoder:ident) { pub mod sync { $($tt:tt)* } })*) => {
$(
#[cfg(feature = $feat)]
pub mod $name {
pub mod sync { $($tt)* }
#[cfg(feature = "futures-io")]
io_algo!(futures, $name($encoder, $decoder));
#[cfg(feature = "tokio")]
io_algo!(tokio, $name($encoder, $decoder));
}
)*
}
}
algos! {
pub mod brotli("brotli", BrotliEncoder, BrotliDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use brotli::{enc::backward_references::BrotliEncoderParams, CompressorReader};
let params = BrotliEncoderParams { quality: 1, ..Default::default() };
to_vec(CompressorReader::with_params(bytes, 0, ¶ms))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use brotli::Decompressor;
to_vec(Decompressor::new(bytes, 0))
}
}
}
pub mod bzip2("bzip2", BzEncoder, BzDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use bzip2::{bufread::BzEncoder, Compression};
to_vec(BzEncoder::new(bytes, Compression::fast()))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use bzip2::bufread::BzDecoder;
to_vec(BzDecoder::new(bytes))
}
}
}
pub mod deflate("deflate", DeflateEncoder, DeflateDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use flate2::{bufread::DeflateEncoder, Compression};
to_vec(DeflateEncoder::new(bytes, Compression::fast()))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use flate2::bufread::DeflateDecoder;
to_vec(DeflateDecoder::new(bytes))
}
}
}
pub mod zlib("zlib", ZlibEncoder, ZlibDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use flate2::{bufread::ZlibEncoder, Compression};
to_vec(ZlibEncoder::new(bytes, Compression::fast()))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use flate2::bufread::ZlibDecoder;
to_vec(ZlibDecoder::new(bytes))
}
}
}
pub mod gzip("gzip", GzipEncoder, GzipDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use flate2::{bufread::GzEncoder, Compression};
to_vec(GzEncoder::new(bytes, Compression::fast()))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use flate2::bufread::GzDecoder;
to_vec(GzDecoder::new(bytes))
}
}
}
pub mod zstd("zstd", ZstdEncoder, ZstdDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use libzstd::stream::read::Encoder;
use libzstd::DEFAULT_COMPRESSION_LEVEL;
to_vec(Encoder::new(bytes, DEFAULT_COMPRESSION_LEVEL).unwrap())
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use libzstd::stream::read::Decoder;
to_vec(Decoder::new(bytes).unwrap())
}
}
}
pub mod xz("xz", XzEncoder, XzDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use liblzma::bufread::XzEncoder;
to_vec(XzEncoder::new(bytes, 0))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use liblzma::bufread::XzDecoder;
to_vec(XzDecoder::new(bytes))
}
}
}
pub mod lzma("lzma", LzmaEncoder, LzmaDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use liblzma::bufread::XzEncoder;
use liblzma::stream::{LzmaOptions, Stream};
to_vec(XzEncoder::new_stream(
bytes,
Stream::new_lzma_encoder(&LzmaOptions::new_preset(0).unwrap()).unwrap(),
))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use liblzma::bufread::XzDecoder;
use liblzma::stream::Stream;
to_vec(XzDecoder::new_stream(
bytes,
Stream::new_lzma_decoder(u64::MAX).unwrap(),
))
}
}
}
pub mod lz4("lz4", Lz4Encoder, Lz4Decoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use std::io::Write;
use lz4::EncoderBuilder;
let mut encoder = EncoderBuilder::new().build(vec![]).unwrap();
encoder.write_all(bytes).unwrap();
let (compressed_bytes, result) = encoder.finish();
result.unwrap();
compressed_bytes
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use lz4::Decoder;
to_vec(Decoder::new(bytes).unwrap())
}
}
}
}
macro_rules! io_algo_parallel {
($impl:ident, $algo:ident($encoder:ident, $decoder:ident)) => {
pub mod $impl {
const THREADS: std::num::NonZeroU32 = std::num::NonZeroU32::new(16).unwrap();
pub mod read {
pub use crate::utils::impls::$impl::read::{poll_read, to_vec};
}
pub mod bufread {
pub use crate::utils::impls::$impl::bufread::{from, AsyncBufRead};
pub use async_compression::$impl::bufread::{
$decoder as Decoder, $encoder as Encoder,
};
use super::THREADS;
use crate::utils::{pin_mut, Level};
pub fn compress(input: impl AsyncBufRead) -> Vec<u8> {
pin_mut!(input);
super::read::to_vec(Encoder::parallel(input, Level::Fastest, THREADS))
}
pub fn decompress(input: impl AsyncBufRead) -> Vec<u8> {
pin_mut!(input);
super::read::to_vec(Decoder::parallel(input, THREADS))
}
}
pub mod write {
pub use crate::utils::impls::$impl::write::to_vec;
pub use async_compression::$impl::write::{
$decoder as Decoder, $encoder as Encoder,
};
use super::THREADS;
use crate::utils::Level;
pub fn compress(input: &[Vec<u8>], limit: usize) -> Vec<u8> {
to_vec(
input,
|input| Box::pin(Encoder::parallel(input, Level::Fastest, THREADS)),
limit,
)
}
pub fn decompress(input: &[Vec<u8>], limit: usize) -> Vec<u8> {
to_vec(
input,
|input| Box::pin(Decoder::parallel(input, THREADS)),
limit,
)
}
}
}
};
}
macro_rules! algos_parallel {
($(pub mod $name:ident($feat:literal, $encoder:ident, $decoder:ident) { pub mod sync { $($tt:tt)* } })*) => {
$(
#[cfg(feature = $feat)]
pub mod $name {
pub mod sync { $($tt)* }
#[cfg(feature = "futures-io")]
io_algo_parallel!(futures, $name($encoder, $decoder));
#[cfg(feature = "tokio")]
io_algo_parallel!(tokio, $name($encoder, $decoder));
}
)*
}
}
algos_parallel! {
pub mod xz_parallel("xz-parallel", XzEncoder, XzDecoder) {
pub mod sync {
pub use crate::utils::impls::sync::to_vec;
pub fn compress(bytes: &[u8]) -> Vec<u8> {
use liblzma::bufread::XzEncoder;
to_vec(XzEncoder::new(bytes, 0))
}
pub fn decompress(bytes: &[u8]) -> Vec<u8> {
use liblzma::bufread::XzDecoder;
to_vec(XzDecoder::new(bytes))
}
}
}
}
|