dtolnay / rustversion

Conditional compilation according to rustc compiler version
Apache License 2.0
334 stars 15 forks source link

Disable unstable feature #8

Closed CodeSandwich closed 4 years ago

CodeSandwich commented 4 years ago

I'd like to use an unstable feature only when crate is compiled with nightly. Is it possible to annotate the #![feature(unstable_feature)] line so it's visible only for nightly compiler?

dtolnay commented 4 years ago

I think you would need to add a build.rs script:

#[rustversion::nightly]
fn main() {
    println!("cargo:rustc-cfg=unstable_feature");
}

#[rustversion::not(nightly)]
fn main() {}

and then do:

#![cfg_attr(unstable_feature, feature(unstable_feature))]
WorldSEnder commented 2 months ago

A bit nicer to read with rustversion::cfg! macro now (which isn't directly mentioned in the readme)

fn main() {
    if rustversion::cfg!(nightly) {
        println!("cargo:rustc-cfg=unstable_feature");
    }
}