Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lib): remove lint config to allow unused code #3482

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ include = [

[dependencies]
bytes = "1"
futures-channel = "0.3"
futures-util = { version = "0.3", default-features = false }
http = "1"
http-body = "1"
pin-project-lite = "0.2.4"
tokio = { version = "1", features = ["sync"] }

# Optional

futures-channel = { version = "0.3", optional = true }
futures-util = { version = "0.3", default-features = false, optional = true }
h2 = { version = "0.4", optional = true }
http-body-util = { version = "0.1", optional = true }
httparse = { version = "1.8", optional = true }
Expand Down Expand Up @@ -74,8 +74,8 @@ full = [
]

# HTTP versions
http1 = ["dep:httparse", "dep:itoa"]
http2 = ["dep:h2"]
http1 = ["dep:futures-channel", "dep:futures-util", "dep:httparse", "dep:itoa"]
http2 = ["dep:futures-channel", "dep:futures-util", "dep:h2"]

# Client/Server
client = ["dep:want"]
Expand Down
56 changes: 51 additions & 5 deletions src/body/incoming.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
use std::fmt;
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::Bytes;
use futures_channel::mpsc;
use futures_channel::oneshot;
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
use futures_channel::{mpsc, oneshot};
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
use futures_util::{stream::FusedStream, Stream}; // for mpsc::Receiver
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
use http::HeaderMap;
use http_body::{Body, Frame, SizeHint};

#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
))]
use super::DecodedLength;
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
use crate::common::watch;
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
use crate::proto::h2::ping;

#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
type BodySender = mpsc::Sender<Result<Bytes, crate::Error>>;
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
type TrailersSender = oneshot::Sender<HeaderMap>;

/// A stream of `Bytes`, used when receiving bodies from the network.
Expand All @@ -39,8 +49,8 @@ pub struct Incoming {
}

enum Kind {
#[allow(dead_code)]
Empty,
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
Chan {
content_length: DecodedLength,
want_tx: watch::Sender,
Expand Down Expand Up @@ -72,25 +82,29 @@ enum Kind {
/// [`Body::channel()`]: struct.Body.html#method.channel
/// [`Sender::abort()`]: struct.Sender.html#method.abort
#[must_use = "Sender does nothing unless sent on"]
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
pub(crate) struct Sender {
want_rx: watch::Receiver,
data_tx: BodySender,
trailers_tx: Option<TrailersSender>,
}

#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
const WANT_PENDING: usize = 1;
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
const WANT_READY: usize = 2;

impl Incoming {
/// Create a `Body` stream with an associated sender half.
///
/// Useful when wanting to stream chunks from another thread.
#[inline]
#[allow(unused)]
#[cfg(test)]
pub(crate) fn channel() -> (Sender, Incoming) {
Self::new_channel(DecodedLength::CHUNKED, /*wanter =*/ false)
}

#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
pub(crate) fn new_channel(content_length: DecodedLength, wanter: bool) -> (Sender, Incoming) {
let (data_tx, data_rx) = mpsc::channel(0);
let (trailers_tx, trailers_rx) = oneshot::channel();
Expand Down Expand Up @@ -171,11 +185,26 @@ impl Body for Incoming {
type Error = crate::Error;

fn poll_frame(
#[cfg_attr(
not(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
)),
allow(unused_mut)
)]
mut self: Pin<&mut Self>,
#[cfg_attr(
not(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
)),
allow(unused_variables)
)]
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match self.kind {
Kind::Empty => Poll::Ready(None),
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
Kind::Chan {
content_length: ref mut len,
ref mut data_rx,
Expand Down Expand Up @@ -247,6 +276,7 @@ impl Body for Incoming {
fn is_end_stream(&self) -> bool {
match self.kind {
Kind::Empty => true,
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
Kind::Chan { content_length, .. } => content_length == DecodedLength::ZERO,
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
Kind::H2 { recv: ref h2, .. } => h2.is_end_stream(),
Expand All @@ -256,6 +286,10 @@ impl Body for Incoming {
}

fn size_hint(&self) -> SizeHint {
#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
))]
macro_rules! opt_len {
($content_length:expr) => {{
let mut hint = SizeHint::default();
Expand All @@ -270,6 +304,7 @@ impl Body for Incoming {

match self.kind {
Kind::Empty => SizeHint::with_exact(0),
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
Kind::Chan { content_length, .. } => opt_len!(content_length),
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
Kind::H2 { content_length, .. } => opt_len!(content_length),
Expand All @@ -289,13 +324,21 @@ impl fmt::Debug for Incoming {
let mut builder = f.debug_tuple("Body");
match self.kind {
Kind::Empty => builder.field(&Empty),
#[cfg(any(
all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
),
feature = "ffi"
))]
_ => builder.field(&Streaming),
};

builder.finish()
}
}

#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
impl Sender {
/// Check to see if this `Sender` can send more data.
pub(crate) fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<crate::Result<()>> {
Expand All @@ -315,11 +358,13 @@ impl Sender {
}
}

#[cfg(test)]
async fn ready(&mut self) -> crate::Result<()> {
futures_util::future::poll_fn(|cx| self.poll_ready(cx)).await
}

/// Send data on data channel when it is ready.
#[cfg(test)]
#[allow(unused)]
pub(crate) async fn send_data(&mut self, chunk: Bytes) -> crate::Result<()> {
self.ready().await?;
Expand Down Expand Up @@ -357,7 +402,7 @@ impl Sender {
.map_err(|err| err.into_inner().expect("just sent Ok"))
}

#[allow(unused)]
#[cfg(test)]
pub(crate) fn abort(mut self) {
self.send_error(crate::Error::new_body_write_aborted());
}
Expand All @@ -371,6 +416,7 @@ impl Sender {
}
}

#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
impl fmt::Debug for Sender {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[derive(Debug)]
Expand Down
8 changes: 8 additions & 0 deletions src/body/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl DecodedLength {
}

/// Converts to an Option<u64> representing a Known or Unknown length.
#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
))]
pub(crate) fn into_opt(self) -> Option<u64> {
match self {
DecodedLength::CHUNKED | DecodedLength::CLOSE_DELIMITED => None,
Expand All @@ -58,6 +62,10 @@ impl DecodedLength {
}
}

#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
))]
pub(crate) fn sub_if(&mut self, amt: u64) {
match *self {
DecodedLength::CHUNKED | DecodedLength::CLOSE_DELIMITED => (),
Expand Down
4 changes: 4 additions & 0 deletions src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub use self::incoming::Incoming;

#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
pub(crate) use self::incoming::Sender;
#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "client", feature = "server")
))]
pub(crate) use self::length::DecodedLength;

mod incoming;
Expand Down
5 changes: 5 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(all(
any(feature = "client", feature = "server"),
any(feature = "http1", feature = "http2")
))]
macro_rules! ready {
($e:expr) => {
match $e {
Expand All @@ -18,4 +22,5 @@ pub(crate) mod task;
all(any(feature = "client", feature = "server"), feature = "http2"),
))]
pub(crate) mod time;
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
pub(crate) mod watch;
44 changes: 44 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub(super) enum Kind {
/// A pending item was dropped before ever being processed.
Canceled,
/// Indicates a channel (client or body sender) is closed.
#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
all(feature = "http2", feature = "client")
))]
ChannelClosed,
/// An `io::Error` that occurred while trying to read or write to a network stream.
#[cfg(all(
Expand Down Expand Up @@ -121,6 +125,10 @@ pub(super) enum User {
))]
Body,
/// The user aborted writing of the outgoing body.
#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
feature = "ffi"
))]
BodyWriteAborted,
/// Error from future of user's Service.
#[cfg(any(
Expand Down Expand Up @@ -192,6 +200,16 @@ impl Error {

/// Returns true if a sender's channel is closed.
pub fn is_closed(&self) -> bool {
#[cfg(not(any(
all(feature = "http1", any(feature = "client", feature = "server")),
all(feature = "http2", feature = "client")
)))]
return false;

#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
all(feature = "http2", feature = "client")
))]
matches!(self.inner.kind, Kind::ChannelClosed)
}

Expand All @@ -202,6 +220,16 @@ impl Error {

/// Returns true if the body write was aborted.
pub fn is_body_write_aborted(&self) -> bool {
#[cfg(not(any(
all(feature = "http1", any(feature = "client", feature = "server")),
feature = "ffi"
)))]
return false;

#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
feature = "ffi"
))]
matches!(self.inner.kind, Kind::User(User::BodyWriteAborted))
}

Expand Down Expand Up @@ -280,6 +308,10 @@ impl Error {
Error::new(Kind::Io).with(cause)
}

#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
all(feature = "http2", feature = "client")
))]
pub(super) fn new_closed() -> Error {
Error::new(Kind::ChannelClosed)
}
Expand All @@ -300,6 +332,10 @@ impl Error {
Error::new(Kind::BodyWrite).with(cause)
}

#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
feature = "ffi"
))]
pub(super) fn new_body_write_aborted() -> Error {
Error::new(Kind::User(User::BodyWriteAborted))
}
Expand Down Expand Up @@ -407,6 +443,10 @@ impl Error {
Kind::IncompleteMessage => "connection closed before message completed",
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
Kind::UnexpectedMessage => "received unexpected message from connection",
#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
all(feature = "http2", feature = "client")
))]
Kind::ChannelClosed => "channel closed",
Kind::Canceled => "operation was canceled",
#[cfg(all(feature = "http1", feature = "server"))]
Expand Down Expand Up @@ -436,6 +476,10 @@ impl Error {
any(feature = "http1", feature = "http2")
))]
Kind::User(User::Body) => "error from user's Body stream",
#[cfg(any(
all(feature = "http1", any(feature = "client", feature = "server")),
feature = "ffi"
))]
Kind::User(User::BodyWriteAborted) => "user body write aborted",
#[cfg(any(
all(any(feature = "client", feature = "server"), feature = "http1"),
Expand Down