Skip to content

Commit

Permalink
refactor: simplify PR
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Jul 9, 2023
1 parent 0d28ed5 commit 044ee63
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 125 deletions.
24 changes: 11 additions & 13 deletions src/futures/bufread/macros/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
macro_rules! decoder {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
($(#[$attr:meta])* $name: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<$inner> {
pub struct $name<R> {
#[pin]
inner: crate::futures::bufread::Decoder<$inner, crate::codec::$name>,
inner: crate::futures::bufread::Decoder<R, crate::codec::$name>,
}
}

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

$(
$($constructor)*
)*
$($($inherent_methods)*)*

/// Configure multi-member/frame decoding, if enabled this will reset the decoder state
/// when reaching the end of a compressed member/frame and expect either EOF or another
Expand All @@ -33,7 +31,7 @@ macro_rules! decoder {
}

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

Expand All @@ -42,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 $inner {
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut()
}

Expand All @@ -51,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 $inner> {
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut R> {
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) -> $inner {
pub fn into_inner(self) -> R {
self.inner.into_inner()
}
}

impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<$inner> {
impl<R: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<R> {
fn poll_read(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
Expand Down
4 changes: 2 additions & 2 deletions src/futures/bufread/macros/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! encoder {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
pin_project_lite::pin_project! {
$(#[$attr])*
///
Expand All @@ -17,7 +17,7 @@ macro_rules! encoder {
/// Creates a new encoder which will read uncompressed data from the given stream
/// and emit a compressed stream.
///
$($constructor)*
$($inherent_methods)*
)*

/// Acquires a reference to the underlying reader that this encoder is wrapping.
Expand Down
2 changes: 1 addition & 1 deletion src/futures/bufread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ mod generic;

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

algos!(futures::bufread<R1, R2>);
algos!(futures::bufread<R>);
24 changes: 11 additions & 13 deletions src/futures/write/macros/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
macro_rules! decoder {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
($(#[$attr:meta])* $name: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<$inner> {
pub struct $name<W> {
#[pin]
inner: crate::futures::write::Decoder<$inner, crate::codec::$name>,
inner: crate::futures::write::Decoder<W, crate::codec::$name>,
}
}

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

$(
$($constructor)*
)*
$($($inherent_methods)*)*

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

Expand All @@ -35,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 $inner {
pub fn get_mut(&mut self) -> &mut W {
self.inner.get_mut()
}

Expand All @@ -44,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 $inner> {
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut W> {
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) -> $inner {
pub fn into_inner(self) -> W {
self.inner.into_inner()
}
}

impl<$inner: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<$inner> {
impl<W: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<W> {
fn poll_write(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
Expand Down
4 changes: 2 additions & 2 deletions src/futures/write/macros/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! encoder {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($constructor:tt)* })*) => {
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
pin_project_lite::pin_project! {
$(#[$attr])*
///
Expand All @@ -17,7 +17,7 @@ macro_rules! encoder {
/// Creates a new encoder which will take in uncompressed data and write it
/// compressed to the given stream.
///
$($constructor)*
$($inherent_methods)*
)*

/// Acquires a reference to the underlying writer that this encoder is wrapping.
Expand Down
2 changes: 1 addition & 1 deletion src/futures/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ use self::{
generic::{Decoder, Encoder},
};

algos!(futures::write<W1, W2>);
algos!(futures::write<W>);
Loading

0 comments on commit 044ee63

Please sign in to comment.