Skip to content

Commit

Permalink
refactor: clean up unused warnings with features (seanmonstar#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar authored and Nutomic committed Nov 7, 2024
1 parent 29bd44b commit 689c4f7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/async_impl/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pin_project! {
}

/// Converts any `impl Body` into a `impl Stream` of just its DATA frames.
#[cfg(any(feature = "stream", feature = "multipart",))]
pub(crate) struct DataStream<B>(pub(crate) B);

impl Body {
Expand Down Expand Up @@ -434,6 +435,7 @@ where

// ===== impl DataStream =====

#[cfg(any(feature = "stream", feature = "multipart",))]
impl<B> futures_core::Stream for DataStream<B>
where
B: HttpBody<Data = Bytes> + Unpin,
Expand Down
67 changes: 62 additions & 5 deletions src/async_impl/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::fmt;
#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate"
))]
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
Expand All @@ -15,9 +21,16 @@ use async_compression::tokio::bufread::ZstdDecoder;
#[cfg(feature = "deflate")]
use async_compression::tokio::bufread::ZlibDecoder;

use bytes::Bytes;
#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate",
feature = "blocking",
))]
use futures_core::Stream;
use futures_util::stream::Peekable;

use bytes::Bytes;
use http::HeaderMap;
use hyper::body::Body as HttpBody;
use hyper::body::Frame;
Expand All @@ -38,7 +51,6 @@ use tokio_util::codec::{BytesCodec, FramedRead};
use tokio_util::io::StreamReader;

use super::body::ResponseBody;
use crate::error;

#[derive(Clone, Copy, Debug)]
pub(super) struct Accepts {
Expand Down Expand Up @@ -74,7 +86,13 @@ pub(crate) struct Decoder {
inner: Inner,
}

type PeekableIoStream = Peekable<IoStream>;
#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate"
))]
type PeekableIoStream = futures_util::stream::Peekable<IoStream>;

#[cfg(any(
feature = "gzip",
Expand Down Expand Up @@ -114,11 +132,30 @@ enum Inner {
Pending(Pin<Box<Pending>>),
}

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate"
))]
/// A future attempt to poll the response body for EOF so we know whether to use gzip or not.
struct Pending(PeekableIoStream, DecoderType);

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate",
feature = "blocking",
))]
pub(crate) struct IoStream<B = ResponseBody>(B);

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate"
))]
enum DecoderType {
#[cfg(feature = "gzip")]
Gzip,
Expand Down Expand Up @@ -376,11 +413,24 @@ impl HttpBody for Decoder {
}
}

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate",
feature = "blocking",
))]
fn empty() -> ResponseBody {
use http_body_util::{combinators::BoxBody, BodyExt, Empty};
BoxBody::new(Empty::new().map_err(|never| match never {}))
}

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate"
))]
impl Future for Pending {
type Output = Result<Inner, std::io::Error>;

Expand Down Expand Up @@ -429,6 +479,13 @@ impl Future for Pending {
}
}

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate",
feature = "blocking",
))]
impl<B> Stream for IoStream<B>
where
B: HttpBody<Data = Bytes> + Unpin,
Expand All @@ -447,7 +504,7 @@ where
continue;
}
}
Some(Err(err)) => Poll::Ready(Some(Err(error::into_io(err.into())))),
Some(Err(err)) => Poll::Ready(Some(Err(crate::error::into_io(err.into())))),
None => Poll::Ready(None),
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ pub(crate) fn upgrade<E: Into<BoxError>>(e: E) -> Error {

// io::Error helpers

#[cfg(any(
feature = "gzip",
feature = "zstd",
feature = "brotli",
feature = "deflate",
feature = "blocking",
))]
pub(crate) fn into_io(e: BoxError) -> io::Error {
io::Error::new(io::ErrorKind::Other, e)
}
Expand Down

0 comments on commit 689c4f7

Please sign in to comment.