From 9dff00425dc4463a3277cad739dba4d935364851 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 12 Jan 2021 00:11:51 +0100 Subject: [PATCH] refactor(lib): Use cfg(all(...)) instead of multiple cfg attributes --- src/body/body.rs | 6 ++---- src/client/conn.rs | 8 +++++--- src/client/mod.rs | 3 +-- src/common/exec.rs | 3 +-- src/common/mod.rs | 12 ++++-------- src/headers.rs | 11 ++++------- src/proto/mod.rs | 3 +-- src/server/conn.rs | 24 ++++++++---------------- src/service/mod.rs | 12 ++++-------- 9 files changed, 30 insertions(+), 52 deletions(-) diff --git a/src/body/body.rs b/src/body/body.rs index a515e4b989..1be2c1b0c2 100644 --- a/src/body/body.rs +++ b/src/body/body.rs @@ -16,8 +16,7 @@ use super::DecodedLength; #[cfg(feature = "stream")] use crate::common::sync_wrapper::SyncWrapper; use crate::common::Future; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] use crate::common::Never; use crate::common::{task, watch, Pin, Poll}; #[cfg(all(feature = "http2", any(feature = "client", feature = "server")))] @@ -74,8 +73,7 @@ struct Extra { delayed_eof: Option, } -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] type DelayEofUntil = oneshot::Receiver; enum DelayEof { diff --git a/src/client/conn.rs b/src/client/conn.rs index fd25741f0f..2799c61eff 100644 --- a/src/client/conn.rs +++ b/src/client/conn.rs @@ -51,8 +51,7 @@ use std::fmt; #[cfg(feature = "http2")] use std::marker::PhantomData; use std::sync::Arc; -#[cfg(feature = "runtime")] -#[cfg(feature = "http2")] +#[cfg(all(feature = "runtime", feature = "http2"))] use std::time::Duration; use bytes::Bytes; @@ -63,7 +62,10 @@ use tower_service::Service; use super::dispatch; use crate::body::HttpBody; -use crate::common::{task, exec::{BoxSendFuture, Exec}, Future, Pin, Poll}; +use crate::common::{ + exec::{BoxSendFuture, Exec}, + task, Future, Pin, Poll, +}; use crate::proto; use crate::rt::Executor; #[cfg(feature = "http1")] diff --git a/src/client/mod.rs b/src/client/mod.rs index 9600a7640d..e45608cd4c 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -52,8 +52,7 @@ pub use self::connect::HttpConnector; pub mod connect; -#[cfg(test)] -#[cfg(feature = "runtime")] +#[cfg(all(test, feature = "runtime"))] mod tests; cfg_feature! { diff --git a/src/common/exec.rs b/src/common/exec.rs index c52482a336..f7560efbfc 100644 --- a/src/common/exec.rs +++ b/src/common/exec.rs @@ -5,8 +5,7 @@ use std::sync::Arc; #[cfg(feature = "server")] use crate::body::{Body, HttpBody}; -#[cfg(feature = "http2")] -#[cfg(feature = "server")] +#[cfg(all(feature = "http2", feature = "server"))] use crate::proto::h2::server::H2Stream; use crate::rt::Executor; #[cfg(feature = "server")] diff --git a/src/common/mod.rs b/src/common/mod.rs index a5947315b8..050c61d15c 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -8,17 +8,14 @@ macro_rules! ready { } pub(crate) mod buf; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "server")] +#[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))] pub(crate) mod date; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "server")] +#[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))] pub(crate) mod drain; #[cfg(any(feature = "http1", feature = "http2"))] pub(crate) mod exec; pub(crate) mod io; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] mod lazy; mod never; #[cfg(feature = "stream")] @@ -26,8 +23,7 @@ pub(crate) mod sync_wrapper; pub(crate) mod task; pub(crate) mod watch; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] pub(crate) use self::lazy::{lazy, Started as Lazy}; #[cfg(any( feature = "client", diff --git a/src/headers.rs b/src/headers.rs index 68c907f9db..7fc486a80c 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -2,10 +2,9 @@ use bytes::BytesMut; use http::header::CONTENT_LENGTH; use http::header::{HeaderValue, ValueIter}; -#[cfg(feature = "http2")] -#[cfg(feature = "client")] -use http::Method; use http::HeaderMap; +#[cfg(all(feature = "http2", feature = "client"))] +use http::Method; #[cfg(feature = "http1")] pub(super) fn connection_keep_alive(value: &HeaderValue) -> bool { @@ -29,8 +28,7 @@ fn connection_has(value: &HeaderValue, needle: &str) -> bool { false } -#[cfg(feature = "http1")] -#[cfg(feature = "server")] +#[cfg(all(feature = "http1", feature = "server"))] pub(super) fn content_length_parse(value: &HeaderValue) -> Option { value.to_str().ok().and_then(|s| s.parse().ok()) } @@ -66,8 +64,7 @@ pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue> } } -#[cfg(feature = "http2")] -#[cfg(feature = "client")] +#[cfg(all(feature = "http2", feature = "client"))] pub(super) fn method_has_defined_payload_semantics(method: &Method) -> bool { match *method { Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT => false, diff --git a/src/proto/mod.rs b/src/proto/mod.rs index 0c86336b32..6f422078f6 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -37,8 +37,7 @@ pub(crate) type RequestHead = MessageHead; pub(crate) struct RequestLine(pub(crate) http::Method, pub(crate) http::Uri); /// An incoming response message. -#[cfg(feature = "http1")] -#[cfg(feature = "client")] +#[cfg(all(feature = "http1", feature = "client"))] pub(crate) type ResponseHead = MessageHead; #[derive(Debug)] diff --git a/src/server/conn.rs b/src/server/conn.rs index 50b84dd0d0..5137708fcb 100644 --- a/src/server/conn.rs +++ b/src/server/conn.rs @@ -49,8 +49,7 @@ use std::fmt; use std::marker::PhantomData; #[cfg(feature = "tcp")] use std::net::SocketAddr; -#[cfg(feature = "runtime")] -#[cfg(feature = "http2")] +#[cfg(all(feature = "runtime", feature = "http2"))] use std::time::Duration; use bytes::Bytes; @@ -63,8 +62,7 @@ use crate::common::exec::{ConnStreamExec, Exec, NewSvcExec}; #[cfg(feature = "http2")] use crate::common::io::Rewind; use crate::common::{task, Future, Pin, Poll, Unpin}; -#[cfg(feature = "http1")] -#[cfg(feature = "http2")] +#[cfg(all(feature = "http1", feature = "http2"))] use crate::error::{Kind, Parse}; use crate::proto; use crate::service::{HttpService, MakeServiceRef}; @@ -107,8 +105,7 @@ enum ConnectionMode { #[cfg(feature = "http2")] H2Only, /// Use HTTP/1 and try to upgrade to h2 when a parse error occurs. - #[cfg(feature = "http1")] - #[cfg(feature = "http2")] + #[cfg(all(feature = "http1", feature = "http2"))] Fallback, } @@ -160,8 +157,7 @@ where S: HttpService, { pub(super) conn: Option>, - #[cfg(feature = "http1")] - #[cfg(feature = "http2")] + #[cfg(all(feature = "http1", feature = "http2"))] fallback: Fallback, } @@ -186,16 +182,14 @@ where H2(#[pin] proto::h2::Server, S, B, E>), } -#[cfg(feature = "http1")] -#[cfg(feature = "http2")] +#[cfg(all(feature = "http1", feature = "http2"))] #[derive(Clone, Debug)] enum Fallback { ToHttp2(proto::h2::server::Config, E), Http1Only, } -#[cfg(feature = "http1")] -#[cfg(feature = "http2")] +#[cfg(all(feature = "http1", feature = "http2"))] impl Fallback { fn to_h2(&self) -> bool { match *self { @@ -205,8 +199,7 @@ impl Fallback { } } -#[cfg(feature = "http1")] -#[cfg(feature = "http2")] +#[cfg(all(feature = "http1", feature = "http2"))] impl Unpin for Fallback {} /// Deconstructed parts of a `Connection`. @@ -701,8 +694,7 @@ where }) } - #[cfg(feature = "http1")] - #[cfg(feature = "http2")] + #[cfg(all(feature = "http1", feature = "http2"))] fn upgrade_h2(&mut self) { trace!("Trying to upgrade connection to h2"); let conn = self.conn.take(); diff --git a/src/service/mod.rs b/src/service/mod.rs index 5f156d46cd..22f850ca47 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -39,20 +39,16 @@ pub use tower_service::Service; mod http; mod make; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))] mod oneshot; mod util; pub(super) use self::http::HttpService; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))] pub(super) use self::make::MakeConnection; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "server")] +#[cfg(all(any(feature = "http1", feature = "http2"), feature = "server"))] pub(super) use self::make::MakeServiceRef; -#[cfg(any(feature = "http1", feature = "http2"))] -#[cfg(feature = "client")] +#[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))] pub(super) use self::oneshot::{oneshot, Oneshot}; pub use self::make::make_service_fn;