rust


Is there a way to use the cfg(feature) check on multiple statements?


Is there a way to minimize the following feature check?
#[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;
To something like:
#[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;
}
This would better convey meaning and be more ergonomic.
The cfg-if crate provides the cfg-if! macro 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, it even describes itself using your words:
A macro to ergonomically define an item depending on a large number of #[cfg] parameters. Structured like an if-else chain, the first matching branch is the item that gets emitted.
You could create a private module that import all the files, and then let the parent module re-export everything from that private module:
#[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::*;
You still need to write that #[cfg] line twice though.

Related Links

Destructure immutable reference and bind mutably in parameter list
Implementing Nested Traits
Is there a command to update cargo to the latest official release?
How to interleave stderr and stdin to a std::process::Command?
Why does my prime number sieve include the number 49?
How to implement a &mut iterator for a Binary Search Tree?
What does _ mean in type errors in Rust?
rust E0061: Some(A) is not the same as Some(match … =>A)
Class subtyping in Rust
Troubles deriving Show when updating very old code [duplicate]
Is it recommended to use traits to implement utility functions for structs in external crate?
Box type and method syntax in Rust [duplicate]
How to make mutable pointer to field of node of tree and mutate it?
Assigning *mut to a LocalKey
How to tell the Rust compiler that a borrow has ended?
Weird behaviour of argument repetitions in Rust macro

Categories

HOME
cocoapods
powerbi
weblogic12c
barcode-scanner
key
devexpress
tcsh
theano
apk
histogram
celery
implicit-conversion
ui-automation
tfs2010
swarm
nexus3
alljoyn
watch-os-3
vimeo
android-contacts
google-sites
nose
nullreferenceexception
scheduled-tasks
amazon-mws
unity-container
kamailio
maven-surefire-plugin
acl
ini
exchange-server-2010
morris.js
uilocalnotification
laravel-4.2
boolean-logic
fusion
autodesk-model-derivative
windowsiot
progid
actframework
lxml
nppexec
ms-dos
gitolite
opal-framework
web-inspector
flume-twitter
windows-media-player
webalizer
image-registration
pen
deedle
msxml
emf-compare
ltrace
prefetch
perl-data-structures
nsdata
uibinder
accessibilityservice
flipclock
largenumber
directx-9
extend
jnlp
modern.ie
dbmigrate
html-escape-characters
debug-symbols
tkx
nanomsg
operands
comctl32
utf8-decode
squeezebox
git-reset
redpitaya
spring-social-linkedin
nosql-aggregation
compiler-flags
insertonsubmit
codahale-metrics
hints
mongo-jackson-mapper
oscilloscope
jquery-cycle
session-0-isolation
office-app
ckeditor.net
viewpagerindicator
thejit
server-name
zotonic
prefuse
dropshadow
django-pagination
dip
pyjamas
downgrade
hungarian-notation
3-tier
3270
downcasting
xap
rakudo
objectinstantiation
rendering-engine

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App