Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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"?

like image 291
jschw Avatar asked Jun 10 '20 09:06

jschw


1 Answers

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)
)]
like image 191
Peter Hall Avatar answered Nov 15 '22 04:11

Peter Hall