Skip to content

refactor: use cfg-aliases for feature flags #3865

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
fn main() {
// Add to the list of expected config names and values that is used when checking the reachable
// cfg expressions with the unexpected_cfgs lint.
//
// See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-check-cfg
println!("cargo:rustc-check-cfg=cfg(http1)");
println!("cargo:rustc-check-cfg=cfg(http2)");
println!("cargo:rustc-check-cfg=cfg(client)");
println!("cargo:rustc-check-cfg=cfg(server)");
println!("cargo:rustc-check-cfg=cfg(ffi)");
println!("cargo:rustc-check-cfg=cfg(full)");
println!("cargo:rustc-check-cfg=cfg(nightly)");
println!("cargo:rustc-check-cfg=cfg(runtime)"); // TODO evaluate if this is needed (see below)
println!("cargo:rustc-check-cfg=cfg(tracing)");
println!("cargo:rustc-check-cfg=cfg(http_client)");
println!("cargo:rustc-check-cfg=cfg(http1_client)");
println!("cargo:rustc-check-cfg=cfg(http2_client)");
println!("cargo:rustc-check-cfg=cfg(http_server)");
println!("cargo:rustc-check-cfg=cfg(http1_server)");
println!("cargo:rustc-check-cfg=cfg(http2_server)");

// Add cfg flags that simplify using cfg expressions in the code. e.g. instead of
// `#[cfg(all(any(feature = "http1", feature = "http2"), feature = "server")]` you can use
// `#[cfg(http_server)]`
//
// See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-cfg
#[cfg(feature = "http1")]
println!("cargo:rustc-cfg=http1");

#[cfg(feature = "http2")]
println!("cargo:rustc-cfg=http2");

#[cfg(any(feature = "http1", feature = "http2"))]
println!("cargo:rustc-cfg=http");

#[cfg(feature = "client")]
println!("cargo:rustc-cfg=client");

#[cfg(feature = "server")]
println!("cargo:rustc-cfg=server");

#[cfg(feature = "ffi")]
println!("cargo:rustc-cfg=ffi");

#[cfg(feature = "full")]
println!("cargo:rustc-cfg=full");

#[cfg(feature = "nightly")]
println!("cargo:rustc-cfg=nightly");

// TODO: this feature doesn't actually exist in the cargo.toml
// this condition was added to simplify the conditions in src/mock.rs, but I'm not sure if those
// conditions were actually working as intended
// #[cfg(feature = "runtime")]
// println!("cargo:rustc-cfg=runtime");

#[cfg(feature = "tracing")]
println!("cargo:rustc-cfg=tracing");

#[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))]
println!("cargo:rustc-cfg=http_client");

#[cfg(all(feature = "http1", feature = "client"))]
println!("cargo:rustc-cfg=http1_client");

#[cfg(all(feature = "http2", feature = "client"))]
println!("cargo:rustc-cfg=http2_client");

#[cfg(all(any(feature = "http1", feature = "http2"), feature = "server"))]
println!("cargo:rustc-cfg=http_server");

#[cfg(all(feature = "http1", feature = "server"))]
println!("cargo:rustc-cfg=http1_server");

#[cfg(all(feature = "http2", feature = "server"))]
println!("cargo:rustc-cfg=http2_server");
}
8 changes: 4 additions & 4 deletions examples/hello-http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use http_body_util::Full;
use hyper::body::Bytes;
#[cfg(feature = "server")]
#[cfg(server)]
use hyper::server::conn::http2;
use hyper::service::service_fn;
use hyper::{Request, Response};
Expand All @@ -19,7 +19,7 @@ use support::TokioIo;

// An async function that consumes a request, does nothing with it and returns a
// response.
#[cfg(feature = "server")]
#[cfg(server)]
async fn hello(_: Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
}
Expand All @@ -42,7 +42,7 @@ where
}
}

#[cfg(feature = "server")]
#[cfg(server)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
pretty_env_logger::init();
Expand Down Expand Up @@ -83,7 +83,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
}

#[cfg(not(feature = "server"))]
#[cfg(not(server))]
fn main() {
panic!("This example requires the 'server' feature to be enabled");
}
Loading