Skip to content

Commit

Permalink
refactor: make it visually clear that inner param is shared (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Jul 10, 2023
1 parent 24556b1 commit 9b181f8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
20 changes: 10 additions & 10 deletions src/futures/bufread/macros/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
macro_rules! decoder {
($(#[$attr:meta])* $name:ident $({ $($inherent_methods:tt)* })*) => {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
pin_project_lite::pin_project! {
$(#[$attr])*
///
/// This structure implements an [`AsyncRead`](futures_io::AsyncRead) interface and will
/// read compressed data from an underlying stream and emit a stream of uncompressed data.
#[derive(Debug)]
pub struct $name<R> {
pub struct $name<$inner> {
#[pin]
inner: crate::futures::bufread::Decoder<R, crate::codec::$name>,
inner: crate::futures::bufread::Decoder<$inner, crate::codec::$name>,
}
}

impl<R: futures_io::AsyncBufRead> $name<R> {
impl<$inner: futures_io::AsyncBufRead> $name<$inner> {
/// Creates a new decoder which will read compressed data from the given stream and
/// emit a uncompressed stream.
pub fn new(read: R) -> $name<R> {
pub fn new(read: $inner) -> $name<$inner> {
$name {
inner: crate::futures::bufread::Decoder::new(read, crate::codec::$name::new()),
}
Expand All @@ -31,7 +31,7 @@ macro_rules! decoder {
}

/// Acquires a reference to the underlying reader that this decoder is wrapping.
pub fn get_ref(&self) -> &R {
pub fn get_ref(&self) -> &$inner {
self.inner.get_ref()
}

Expand All @@ -40,7 +40,7 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_mut(&mut self) -> &mut R {
pub fn get_mut(&mut self) -> &mut $inner {
self.inner.get_mut()
}

Expand All @@ -49,20 +49,20 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut R> {
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
self.project().inner.get_pin_mut()
}

/// Consumes this decoder returning the underlying reader.
///
/// Note that this may discard internal state of this decoder, so care should be taken
/// to avoid losing resources when this is called.
pub fn into_inner(self) -> R {
pub fn into_inner(self) -> $inner {
self.inner.into_inner()
}
}

impl<R: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<R> {
impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<$inner> {
fn poll_read(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
Expand Down
20 changes: 10 additions & 10 deletions src/futures/write/macros/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
macro_rules! decoder {
($(#[$attr:meta])* $name:ident $({ $($inherent_methods:tt)* })*) => {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
pin_project_lite::pin_project! {
$(#[$attr])*
///
/// This structure implements an [`AsyncWrite`](futures_io::AsyncWrite) interface and will
/// take in compressed data and write it uncompressed to an underlying stream.
#[derive(Debug)]
pub struct $name<W> {
pub struct $name<$inner> {
#[pin]
inner: crate::futures::write::Decoder<W, crate::codec::$name>,
inner: crate::futures::write::Decoder<$inner, crate::codec::$name>,
}
}

impl<W: futures_io::AsyncWrite> $name<W> {
impl<$inner: futures_io::AsyncWrite> $name<$inner> {
/// Creates a new decoder which will take in compressed data and write it uncompressed
/// to the given stream.
pub fn new(read: W) -> $name<W> {
pub fn new(read: $inner) -> $name<$inner> {
$name {
inner: crate::futures::write::Decoder::new(read, crate::codec::$name::new()),
}
Expand All @@ -24,7 +24,7 @@ macro_rules! decoder {
$($($inherent_methods)*)*

/// Acquires a reference to the underlying reader that this decoder is wrapping.
pub fn get_ref(&self) -> &W {
pub fn get_ref(&self) -> &$inner {
self.inner.get_ref()
}

Expand All @@ -33,7 +33,7 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_mut(&mut self) -> &mut W {
pub fn get_mut(&mut self) -> &mut $inner {
self.inner.get_mut()
}

Expand All @@ -42,20 +42,20 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut W> {
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
self.project().inner.get_pin_mut()
}

/// Consumes this decoder returning the underlying reader.
///
/// Note that this may discard internal state of this decoder, so care should be taken
/// to avoid losing resources when this is called.
pub fn into_inner(self) -> W {
pub fn into_inner(self) -> $inner {
self.inner.into_inner()
}
}

impl<W: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<W> {
impl<$inner: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<$inner> {
fn poll_write(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
Expand Down
22 changes: 11 additions & 11 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
macro_rules! algos {
(@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident<$inner:ident>
(@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
{ @enc $($encoder_methods:tt)* }
{ @dec $($decoder_methods:tt)* }
) => {
#[cfg(feature = $algo_s)]
decoder! {
#[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
#[cfg(feature = $algo_s)]
$decoder
$decoder<$inner>

{ $($decoder_methods)* }
}
Expand All @@ -26,8 +26,8 @@ macro_rules! algos {
}
};

($($mod:ident)::+<$inner:ident>) => {
algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder<$inner>
($($mod:ident)::+ <$inner:ident>) => {
algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder <$inner>
{ @enc
pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
let params = brotli::enc::backward_references::BrotliEncoderParams::default();
Expand All @@ -42,7 +42,7 @@ macro_rules! algos {
{ @dec }
);

algos!(@algo bzip2 ["bzip2"] BzDecoder BzEncoder<$inner>
algos!(@algo bzip2 ["bzip2"] BzDecoder BzEncoder <$inner>
{ @enc

pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Expand All @@ -57,7 +57,7 @@ macro_rules! algos {
{ @dec }
);

algos!(@algo deflate ["deflate"] DeflateDecoder DeflateEncoder<$inner>
algos!(@algo deflate ["deflate"] DeflateDecoder DeflateEncoder <$inner>
{ @enc
pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Self {
Expand All @@ -71,7 +71,7 @@ macro_rules! algos {
{ @dec }
);

algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder<$inner>
algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder <$inner>
{ @enc

pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Expand All @@ -86,7 +86,7 @@ macro_rules! algos {
{ @dec }
);

algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder<$inner>
algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder <$inner>
{ @enc
pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Self {
Expand All @@ -100,7 +100,7 @@ macro_rules! algos {
{ @dec }
);

algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder<$inner>
algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder <$inner>
{ @enc

pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Expand Down Expand Up @@ -175,7 +175,7 @@ macro_rules! algos {
}
);

algos!(@algo xz ["xz"] XzDecoder XzEncoder<$inner>
algos!(@algo xz ["xz"] XzDecoder XzEncoder <$inner>
{ @enc

pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Expand All @@ -190,7 +190,7 @@ macro_rules! algos {
{ @dec }
);

algos!(@algo lzma ["lzma"] LzmaDecoder LzmaEncoder<$inner>
algos!(@algo lzma ["lzma"] LzmaDecoder LzmaEncoder <$inner>
{ @enc

pub fn with_quality(inner: $inner, level: crate::Level) -> Self {
Expand Down
20 changes: 10 additions & 10 deletions src/tokio/bufread/macros/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
macro_rules! decoder {
($(#[$attr:meta])* $name:ident $({ $($inherent_methods:tt)* })*) => {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
pin_project_lite::pin_project! {
$(#[$attr])*
///
/// This structure implements an [`AsyncRead`](tokio::io::AsyncRead) interface and will
/// read compressed data from an underlying stream and emit a stream of uncompressed data.
#[derive(Debug)]
pub struct $name<R> {
pub struct $name<$inner> {
#[pin]
inner: crate::tokio::bufread::Decoder<R, crate::codec::$name>,
inner: crate::tokio::bufread::Decoder<$inner, crate::codec::$name>,
}
}

impl<R: tokio::io::AsyncBufRead> $name<R> {
impl<$inner: tokio::io::AsyncBufRead> $name<$inner> {
/// Creates a new decoder which will read compressed data from the given stream and
/// emit a uncompressed stream.
pub fn new(read: R) -> $name<R> {
pub fn new(read: $inner) -> $name<$inner> {
$name {
inner: crate::tokio::bufread::Decoder::new(read, crate::codec::$name::new()),
}
Expand All @@ -31,7 +31,7 @@ macro_rules! decoder {
}

/// Acquires a reference to the underlying reader that this decoder is wrapping.
pub fn get_ref(&self) -> &R {
pub fn get_ref(&self) -> &$inner {
self.inner.get_ref()
}

Expand All @@ -40,7 +40,7 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_mut(&mut self) -> &mut R {
pub fn get_mut(&mut self) -> &mut $inner {
self.inner.get_mut()
}

Expand All @@ -49,20 +49,20 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut R> {
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
self.project().inner.get_pin_mut()
}

/// Consumes this decoder returning the underlying reader.
///
/// Note that this may discard internal state of this decoder, so care should be taken
/// to avoid losing resources when this is called.
pub fn into_inner(self) -> R {
pub fn into_inner(self) -> $inner {
self.inner.into_inner()
}
}

impl<R: tokio::io::AsyncBufRead> tokio::io::AsyncRead for $name<R> {
impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncRead for $name<$inner> {
fn poll_read(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
Expand Down
20 changes: 10 additions & 10 deletions src/tokio/write/macros/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
macro_rules! decoder {
($(#[$attr:meta])* $name:ident $({ $($inherent_methods:tt)* })*) => {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
pin_project_lite::pin_project! {
$(#[$attr])*
///
/// This structure implements an [`AsyncWrite`](tokio::io::AsyncWrite) interface and will
/// take in compressed data and write it uncompressed to an underlying stream.
#[derive(Debug)]
pub struct $name<W> {
pub struct $name<$inner> {
#[pin]
inner: crate::tokio::write::Decoder<W, crate::codec::$name>,
inner: crate::tokio::write::Decoder<$inner, crate::codec::$name>,
}
}

impl<W: tokio::io::AsyncWrite> $name<W> {
impl<$inner: tokio::io::AsyncWrite> $name<$inner> {
/// Creates a new decoder which will take in compressed data and write it uncompressed
/// to the given stream.
pub fn new(read: W) -> $name<W> {
pub fn new(read: $inner) -> $name<$inner> {
$name {
inner: crate::tokio::write::Decoder::new(read, crate::codec::$name::new()),
}
Expand All @@ -24,7 +24,7 @@ macro_rules! decoder {
$($($inherent_methods)*)*

/// Acquires a reference to the underlying reader that this decoder is wrapping.
pub fn get_ref(&self) -> &W {
pub fn get_ref(&self) -> &$inner {
self.inner.get_ref()
}

Expand All @@ -33,7 +33,7 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_mut(&mut self) -> &mut W {
pub fn get_mut(&mut self) -> &mut $inner {
self.inner.get_mut()
}

Expand All @@ -42,20 +42,20 @@ macro_rules! decoder {
///
/// Note that care must be taken to avoid tampering with the state of the reader which
/// may otherwise confuse this decoder.
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut W> {
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
self.project().inner.get_pin_mut()
}

/// Consumes this decoder returning the underlying reader.
///
/// Note that this may discard internal state of this decoder, so care should be taken
/// to avoid losing resources when this is called.
pub fn into_inner(self) -> W {
pub fn into_inner(self) -> $inner {
self.inner.into_inner()
}
}

impl<W: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<W> {
impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
fn poll_write(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
Expand Down

0 comments on commit 9b181f8

Please sign in to comment.