Is there a way to use cfg (feature) validation for multiple statements?

Is there a way to minimize the following feature checks?

#[cfg(feature = "eugene")]
pub mod eugene_set_steam_id;
#[cfg(feature = "eugene")]
pub mod eugene_balance;
#[cfg(feature = "eugene")]
pub mod eugene_force_map;
#[cfg(feature = "eugene")]
pub mod eugene_rating;
#[cfg(feature = "eugene")]
pub mod eugene_stat;
#[cfg(feature = "eugene")]
pub mod eugene_steam_id;
#[cfg(feature = "eugene")]
pub mod eugene_top;

Sort of:

#[cfg(feature = "eugene")] {
    pub mod eugene_set_steam_id;
    pub mod eugene_balance;
    pub mod eugene_force_map;
    pub mod eugene_rating;
    pub mod eugene_stat;
    pub mod eugene_steam_id;
    pub mod eugene_top;
}

It will better convey meaning and become more ergonomic.

+12
source share
2 answers

cfg-if crate provides a macro cfg-if!that should do what you want:

#[macro_use]
extern crate cfg_if;

cfg_if! {
    if #[cfg(feature = "eugene")] {
        pub mod eugene_set_steam_id;
        pub mod eugene_balance;
        pub mod eugene_force_map;
        pub mod eugene_rating;
        pub mod eugene_stat;
        pub mod eugene_steam_id;
        pub mod eugene_top;
    } else {
    }
}

In fact, he even describes himself using your words:

Macro for ergonomic determination of an element depending on a large number of parameters #[cfg]. Structured as an if-else chain, the first matching branch is the element that is emitted.

+10
source

, , :

#[cfg(feature = "eugene")]
#[path = ""]
mod reexport_eugene_modules {
    pub mod eugene_set_steam_id;
    pub mod eugene_balance;
    pub mod eugene_force_map;
    pub mod eugene_rating;
    pub mod eugene_stat;
    pub mod eugene_steam_id;
    pub mod eugene_top;
}

#[cfg(feature = "eugene")]
pub use reexport_eugene_modules::*;

#[cfg] .

+11

Source: https://habr.com/ru/post/1016112/


All Articles