Skip to content

Commit 91130d7

Browse files
committed
Use a macro to generate all implementations for a wrapper type
1 parent 49c37a3 commit 91130d7

26 files changed

+154
-604
lines changed

src/bufread/brotli.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/bufread/bzip.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/bufread/deflate.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/bufread/gzip.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/bufread/macros/encoder.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
macro_rules! encoder {
2-
($(#[$attr:meta])* $name:ident) => {
2+
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
33
$(#[$attr])*
44
#[pin_project::pin_project]
55
#[derive(Debug)]
66
///
77
/// This structure implements an [`AsyncRead`](futures_io::AsyncRead) interface and will
88
/// read uncompressed data from an underlying stream and emit a stream of compressed data.
9-
pub struct $name<R: futures_io::AsyncBufRead> {
9+
pub struct $name<$inner: futures_io::AsyncBufRead> {
1010
#[pin]
11-
inner: crate::bufread::Encoder<R, crate::codec::$name>,
11+
inner: crate::bufread::Encoder<$inner, crate::codec::$name>,
1212
}
1313

14-
impl<R: futures_io::AsyncBufRead> $name<R> {
14+
impl<$inner: futures_io::AsyncBufRead> $name<$inner> {
15+
$(
16+
/// Creates a new encoder which will read uncompressed data from the given stream
17+
/// and emit a compressed stream.
18+
///
19+
$($constructor)*
20+
)*
21+
1522
/// Acquires a reference to the underlying reader that this encoder is wrapping.
16-
pub fn get_ref(&self) -> &R {
23+
pub fn get_ref(&self) -> &$inner {
1724
self.inner.get_ref()
1825
}
1926

@@ -22,7 +29,7 @@ macro_rules! encoder {
2229
///
2330
/// Note that care must be taken to avoid tampering with the state of the reader which
2431
/// may otherwise confuse this encoder.
25-
pub fn get_mut(&mut self) -> &mut R {
32+
pub fn get_mut(&mut self) -> &mut $inner {
2633
self.inner.get_mut()
2734
}
2835

@@ -31,20 +38,20 @@ macro_rules! encoder {
3138
///
3239
/// Note that care must be taken to avoid tampering with the state of the reader which
3340
/// may otherwise confuse this encoder.
34-
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut R> {
41+
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
3542
self.project().inner.get_pin_mut()
3643
}
3744

3845
/// Consumes this encoder returning the underlying reader.
3946
///
4047
/// Note that this may discard internal state of this encoder, so care should be taken
4148
/// to avoid losing resources when this is called.
42-
pub fn into_inner(self) -> R {
49+
pub fn into_inner(self) -> $inner {
4350
self.inner.into_inner()
4451
}
4552
}
4653

47-
impl<R: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<R> {
54+
impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<$inner> {
4855
fn poll_read(
4956
self: std::pin::Pin<&mut Self>,
5057
cx: &mut std::task::Context<'_>,

src/bufread/mod.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
//! Types which operate over [`AsyncBufRead`](futures_io::AsyncBufRead) streams, both encoders and
22
//! decoders for various formats.
33
4-
mod generic;
54
#[macro_use]
65
mod macros;
7-
8-
#[cfg(feature = "brotli")]
9-
mod brotli;
10-
#[cfg(feature = "bzip")]
11-
mod bzip;
12-
#[cfg(feature = "deflate")]
13-
mod deflate;
14-
#[cfg(feature = "gzip")]
15-
mod gzip;
16-
#[cfg(feature = "zlib")]
17-
mod zlib;
18-
#[cfg(feature = "zstd")]
19-
mod zstd;
6+
mod generic;
207

218
pub(crate) use generic::{Decoder, Encoder};
229

23-
#[cfg(feature = "brotli")]
24-
pub use self::brotli::{BrotliDecoder, BrotliEncoder};
25-
#[cfg(feature = "bzip")]
26-
pub use self::bzip::{BzDecoder, BzEncoder};
27-
#[cfg(feature = "deflate")]
28-
pub use self::deflate::{DeflateDecoder, DeflateEncoder};
29-
#[cfg(feature = "gzip")]
30-
pub use self::gzip::{GzipDecoder, GzipEncoder};
31-
#[cfg(feature = "zlib")]
32-
pub use self::zlib::{ZlibDecoder, ZlibEncoder};
33-
#[cfg(feature = "zstd")]
34-
pub use self::zstd::{ZstdDecoder, ZstdEncoder};
10+
algos!(bufread<R>);

src/bufread/zlib.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/bufread/zstd.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
missing_debug_implementations
7676
)]
7777

78+
#[macro_use]
79+
mod macros;
7880
mod codec;
7981

8082
#[cfg(feature = "bufread")]

src/macros.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
macro_rules! algos {
2+
(@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
3+
#[cfg(feature = $algo_s)]
4+
decoder! {
5+
/// A
6+
#[doc = $algo_s]
7+
/// decoder, or decompressor.
8+
#[cfg_attr(docsrs, doc(cfg(feature = $algo_s)))]
9+
$decoder
10+
}
11+
12+
#[cfg(feature = $algo_s)]
13+
encoder! {
14+
/// A
15+
#[doc = $algo_s]
16+
/// encoder, or compressor.
17+
#[cfg_attr(docsrs, doc(cfg(feature = $algo_s)))]
18+
$encoder<$inner> $({ $($constructor)* })*
19+
}
20+
};
21+
22+
($mod:ident<$inner:ident>) => {
23+
algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder<$inner> {
24+
/// The `level` argument here is typically 0-11.
25+
pub fn new(reader: $inner, level: u32) -> Self {
26+
let mut params = brotli2::CompressParams::new();
27+
params.quality(level);
28+
Self::from_params(reader, &params)
29+
}
30+
} {
31+
pub fn from_params(inner: $inner, params: &brotli2::CompressParams) -> Self {
32+
Self {
33+
inner: crate::$mod::generic::Encoder::new(
34+
inner,
35+
crate::codec::BrotliEncoder::new(params),
36+
),
37+
}
38+
}
39+
});
40+
41+
algos!(@algo bzip ["bzip"] BzDecoder BzEncoder<$inner> {
42+
pub fn new(inner: $inner, level: bzip2::Compression) -> Self {
43+
Self {
44+
inner: crate::$mod::generic::Encoder::new(
45+
inner,
46+
crate::codec::BzEncoder::new(level, 30),
47+
),
48+
}
49+
}
50+
});
51+
52+
algos!(@algo deflate ["deflate"] DeflateDecoder DeflateEncoder<$inner> {
53+
pub fn new(inner: $inner, level: flate2::Compression) -> Self {
54+
Self {
55+
inner: crate::$mod::generic::Encoder::new(
56+
inner,
57+
crate::codec::DeflateEncoder::new(level),
58+
),
59+
}
60+
}
61+
});
62+
63+
algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder<$inner> {
64+
pub fn new(inner: $inner, level: flate2::Compression) -> Self {
65+
Self {
66+
inner: crate::$mod::generic::Encoder::new(
67+
inner,
68+
crate::codec::GzipEncoder::new(level),
69+
),
70+
}
71+
}
72+
});
73+
74+
algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder<$inner> {
75+
pub fn new(inner: $inner, level: flate2::Compression) -> Self {
76+
Self {
77+
inner: crate::$mod::generic::Encoder::new(
78+
inner,
79+
crate::codec::ZlibEncoder::new(level),
80+
),
81+
}
82+
}
83+
});
84+
85+
algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder<$inner> {
86+
/// The `level` argument here can range from 1-21. A level of `0` will use zstd's default, which is `3`.
87+
pub fn new(inner: $inner, level: i32) -> Self {
88+
Self {
89+
inner: crate::$mod::generic::Encoder::new(
90+
inner,
91+
crate::codec::ZstdEncoder::new(level),
92+
),
93+
}
94+
}
95+
});
96+
}
97+
}

0 commit comments

Comments
 (0)