jschw
jschw

Reputation: 570

How can I conditionally enable a Rust feature based on cfg?

I want to use #![feature(custom_test_frameworks)], but if possible only conditionally enable it via #[cfg(not(target_os = "custom_os_name"))]. I would still prefer to have the option to run some tests directly on my host system using rusts libtest, but apparently I'm not allowed to modify features via cfg:

Error Message:

note: inner attributes, like `#![no_std]`, annotate the item enclosing them, 
and are usually found at the beginning of source files. Outer attributes, like 
`#[test]`, annotate the item following them.

Is there any other way to conditionally enable an "inner attribute"?

Upvotes: 8

Views: 3398

Answers (1)

Peter Hall
Peter Hall

Reputation: 58785

You can use #[cfg_attr] to apply an attribute based on some other features.

In your case, you can do:

#![cfg_attr(
    not(target_os = "custom_os_name"),
    feature(custom_test_frameworks)
)]

Upvotes: 11

Related Questions