@@ -230,3 +230,98 @@ algos! {
230230 }
231231 }
232232}
233+
234+ macro_rules! io_algo_parallel {
235+ ( $impl: ident, $algo: ident( $encoder: ident, $decoder: ident) ) => {
236+ pub mod $impl {
237+ const THREADS : std:: num:: NonZeroU32 = std:: num:: NonZeroU32 :: new( 16 ) . unwrap( ) ;
238+
239+ pub mod read {
240+ pub use crate :: utils:: impls:: $impl:: read:: { poll_read, to_vec} ;
241+ }
242+
243+ pub mod bufread {
244+ pub use crate :: utils:: impls:: $impl:: bufread:: { from, AsyncBufRead } ;
245+ pub use async_compression:: $impl:: bufread:: {
246+ $decoder as Decoder , $encoder as Encoder ,
247+ } ;
248+
249+ use super :: THREADS ;
250+ use crate :: utils:: { pin_mut, Level } ;
251+
252+ pub fn compress( input: impl AsyncBufRead ) -> Vec <u8 > {
253+ pin_mut!( input) ;
254+ super :: read:: to_vec( Encoder :: parallel( input, Level :: Fastest , THREADS ) )
255+ }
256+
257+ pub fn decompress( input: impl AsyncBufRead ) -> Vec <u8 > {
258+ pin_mut!( input) ;
259+ super :: read:: to_vec( Decoder :: parallel( input, THREADS ) )
260+ }
261+ }
262+
263+ pub mod write {
264+ pub use crate :: utils:: impls:: $impl:: write:: to_vec;
265+ pub use async_compression:: $impl:: write:: {
266+ $decoder as Decoder , $encoder as Encoder ,
267+ } ;
268+
269+ use super :: THREADS ;
270+ use crate :: utils:: Level ;
271+
272+ pub fn compress( input: & [ Vec <u8 >] , limit: usize ) -> Vec <u8 > {
273+ to_vec(
274+ input,
275+ |input| Box :: pin( Encoder :: parallel( input, Level :: Fastest , THREADS ) ) ,
276+ limit,
277+ )
278+ }
279+
280+ pub fn decompress( input: & [ Vec <u8 >] , limit: usize ) -> Vec <u8 > {
281+ to_vec(
282+ input,
283+ |input| Box :: pin( Decoder :: parallel( input, THREADS ) ) ,
284+ limit,
285+ )
286+ }
287+ }
288+ }
289+ } ;
290+ }
291+
292+ macro_rules! algos_parallel {
293+ ( $( pub mod $name: ident( $feat: literal, $encoder: ident, $decoder: ident) { pub mod sync { $( $tt: tt) * } } ) * ) => {
294+ $(
295+ #[ cfg( feature = $feat) ]
296+ pub mod $name {
297+ pub mod sync { $( $tt) * }
298+
299+ #[ cfg( feature = "futures-io" ) ]
300+ io_algo_parallel!( futures, $name( $encoder, $decoder) ) ;
301+
302+ #[ cfg( feature = "tokio" ) ]
303+ io_algo_parallel!( tokio, $name( $encoder, $decoder) ) ;
304+ }
305+ ) *
306+ }
307+ }
308+
309+ algos_parallel ! {
310+ pub mod xz_parallel( "xz-parallel" , XzEncoder , XzDecoder ) {
311+ pub mod sync {
312+ pub use crate :: utils:: impls:: sync:: to_vec;
313+
314+ pub fn compress( bytes: & [ u8 ] ) -> Vec <u8 > {
315+ use liblzma:: bufread:: XzEncoder ;
316+
317+ to_vec( XzEncoder :: new( bytes, 0 ) )
318+ }
319+
320+ pub fn decompress( bytes: & [ u8 ] ) -> Vec <u8 > {
321+ use liblzma:: bufread:: XzDecoder ;
322+
323+ to_vec( XzDecoder :: new( bytes) )
324+ }
325+ }
326+ }
327+ }
0 commit comments