Skip to content

Commit

Permalink
refactor(lib): Use cfg(all(...)) instead of multiple cfg attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored and seanmonstar committed Jan 28, 2021
1 parent 2c8121f commit 9dff004
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 52 deletions.
6 changes: 2 additions & 4 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down Expand Up @@ -74,8 +73,7 @@ struct Extra {
delayed_eof: Option<DelayEof>,
}

#[cfg(any(feature = "http1", feature = "http2"))]
#[cfg(feature = "client")]
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
type DelayEofUntil = oneshot::Receiver<Never>;

enum DelayEof {
Expand Down
8 changes: 5 additions & 3 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand Down
3 changes: 1 addition & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
3 changes: 1 addition & 2 deletions src/common/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
12 changes: 4 additions & 8 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ 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")]
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",
Expand Down
11 changes: 4 additions & 7 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<u64> {
value.to_str().ok().and_then(|s| s.parse().ok())
}
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ pub(crate) type RequestHead = MessageHead<RequestLine>;
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<http::StatusCode>;

#[derive(Debug)]
Expand Down
24 changes: 8 additions & 16 deletions src/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -160,8 +157,7 @@ where
S: HttpService<Body>,
{
pub(super) conn: Option<ProtoServer<T, S::ResBody, S, E>>,
#[cfg(feature = "http1")]
#[cfg(feature = "http2")]
#[cfg(all(feature = "http1", feature = "http2"))]
fallback: Fallback<E>,
}

Expand All @@ -186,16 +182,14 @@ where
H2(#[pin] proto::h2::Server<Rewind<T>, S, B, E>),
}

#[cfg(feature = "http1")]
#[cfg(feature = "http2")]
#[cfg(all(feature = "http1", feature = "http2"))]
#[derive(Clone, Debug)]
enum Fallback<E> {
ToHttp2(proto::h2::server::Config, E),
Http1Only,
}

#[cfg(feature = "http1")]
#[cfg(feature = "http2")]
#[cfg(all(feature = "http1", feature = "http2"))]
impl<E> Fallback<E> {
fn to_h2(&self) -> bool {
match *self {
Expand All @@ -205,8 +199,7 @@ impl<E> Fallback<E> {
}
}

#[cfg(feature = "http1")]
#[cfg(feature = "http2")]
#[cfg(all(feature = "http1", feature = "http2"))]
impl<E> Unpin for Fallback<E> {}

/// Deconstructed parts of a `Connection`.
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 4 additions & 8 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9dff004

Please sign in to comment.