I have a file with some benchmarks and tests and would like to test against stable, beta and nightly. However, either I don\'t use the benchmark or stable/beta complain. Is ther
Is there a way to hide all the benchmark parts when using stable/beta?
Yes, and you can do it automatically using a build script, so it is not necessary to specify --features
when executing cargo. In the build script you can detect the version of the Rust compiler and define a feature ("nightly"
for example). Then, in the source code you can group your benchmarks and enable them if the feature was defined.
Cargo.toml
[package]
build = "build.rs"
[features]
nightly = []
[build-dependencies]
rustc_version = "0.1.*"
build.rs
extern crate rustc_version;
use rustc_version::{version_meta, Channel};
fn main() {
if version_meta().channel == Channel::Nightly {
println!("cargo:rustc-cfg=feature=\"nightly\"");
}
}
src/lib.rs
#![cfg_attr(all(feature = "nightly", test), feature(test))]
#[cfg(all(feature = "nightly", test))]
extern crate test;
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
// tests
}
#[cfg(all(feature = "nightly", test))]
mod benchs {
use test::Bencher;
// benchs
}