Skip to content

Commit

Permalink
Use feature() on nightly toolchains only.
Browse files Browse the repository at this point in the history
`feature()` is only allowed on Nightly, it's completely disallowed on stable and beta
even for already-stabilized features. So, we autodetect whether the user is using nightly
and conditionally use `feature()`. This allows the crates to Just Work on current 1.75 beta
and will also Just Work when 1.75 stable is out.

Keeping `feature()` is desirable to keep support for:

- Espressif's xtensa rustc fork. (they build from the stable branch but enable use of `feature()`, so latest xtensa rustc still requires `feature()`)
- Users of older nightlies

Once xtensa rust 1.75 is out, we can remove this (upstream nightlies that require `feature()` will be quite old by then, so dropping support for them should be OK).

I decided to not use already-made crates like `rustversion` to do this because they're quite big and do way more than what we need, so I felt badd adding another dep. The code is inspired from `rustversion`'s build.rs.
  • Loading branch information
Dirbaio committed Nov 28, 2023
1 parent 84dfc74 commit c5250aa
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 10 deletions.
18 changes: 18 additions & 0 deletions embedded-hal-async/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::ffi::OsString;
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=build/build.rs");

let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));

let output = Command::new(rustc)
.arg("--version")
.output()
.expect("failed to run `rustc --version`");

if String::from_utf8_lossy(&output.stdout).contains("nightly") {
println!("cargo:rustc-cfg=nightly");
}
}
5 changes: 2 additions & 3 deletions embedded-hal-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
// Needed to pass CI, because we deny warnings.
// We don't immediately remove them to not immediately break older nightlies.
// When all features are stable, we'll remove them.
#![allow(stable_features)]
#![allow(unknown_lints)]
#![cfg_attr(nightly, allow(stable_features, unknown_lints))]
#![cfg_attr(nightly, feature(async_fn_in_trait, impl_trait_projections))]
#![allow(async_fn_in_trait)]
#![feature(async_fn_in_trait, impl_trait_projections)]

pub mod delay;
pub mod digital;
Expand Down
18 changes: 18 additions & 0 deletions embedded-hal-bus/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::ffi::OsString;
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=build/build.rs");

let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));

let output = Command::new(rustc)
.arg("--version")
.output()
.expect("failed to run `rustc --version`");

if String::from_utf8_lossy(&output.stdout).contains("nightly") {
println!("cargo:rustc-cfg=nightly");
}
}
7 changes: 5 additions & 2 deletions embedded-hal-bus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
// Needed to pass CI, because we deny warnings.
// We don't immediately remove them to not immediately break older nightlies.
// When all features are stable, we'll remove them.
#![cfg_attr(feature = "async", allow(stable_features))]
#![cfg_attr(feature = "async", feature(async_fn_in_trait, impl_trait_projections))]
#![cfg_attr(all(feature = "async", nightly), allow(stable_features))]
#![cfg_attr(
all(feature = "async", nightly),
feature(async_fn_in_trait, impl_trait_projections)
)]

// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
#[cfg(feature = "defmt-03")]
Expand Down
18 changes: 18 additions & 0 deletions embedded-io-adapters/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::ffi::OsString;
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=build/build.rs");

let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));

let output = Command::new(rustc)
.arg("--version")
.output()
.expect("failed to run `rustc --version`");

if String::from_utf8_lossy(&output.stdout).contains("nightly") {
println!("cargo:rustc-cfg=nightly");
}
}
4 changes: 2 additions & 2 deletions embedded-io-adapters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// We don't immediately remove them to not immediately break older nightlies.
// When all features are stable, we'll remove them.
#![cfg_attr(
any(feature = "tokio-1", feature = "futures-03"),
all(any(feature = "tokio-1", feature = "futures-03"), nightly),
allow(stable_features)
)]
#![cfg_attr(
any(feature = "tokio-1", feature = "futures-03"),
all(any(feature = "tokio-1", feature = "futures-03"), nightly),
feature(async_fn_in_trait, impl_trait_projections)
)]

Expand Down
18 changes: 18 additions & 0 deletions embedded-io-async/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::ffi::OsString;
use std::process::Command;

fn main() {
println!("cargo:rerun-if-changed=build/build.rs");

let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));

let output = Command::new(rustc)
.arg("--version")
.output()
.expect("failed to run `rustc --version`");

if String::from_utf8_lossy(&output.stdout).contains("nightly") {
println!("cargo:rustc-cfg=nightly");
}
}
5 changes: 2 additions & 3 deletions embedded-io-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
// Needed to pass CI, because we deny warnings.
// We don't immediately remove them to not immediately break older nightlies.
// When all features are stable, we'll remove them.
#![allow(stable_features)]
#![allow(unknown_lints)]
#![cfg_attr(nightly, allow(stable_features, unknown_lints))]
#![cfg_attr(nightly, feature(async_fn_in_trait, impl_trait_projections))]
#![allow(async_fn_in_trait)]
#![feature(async_fn_in_trait, impl_trait_projections)]

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down

0 comments on commit c5250aa

Please sign in to comment.