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

Convert from a fixed-sized c_char array to CString
Why do the coherence rules raise the error “the type parameter must be used as the type parameter for some local type”?
Right way to have Function pointers in struct [duplicate]
type name `int` is undefined or not in scope
How can I overload the assignment operation in Rust?
Handling TcpStream or SslStream idiomatically
Using Rust's errors inside a `loop` results in cheap blocking, but why?
Unexpected type inference failure in Rust
Rust cannot infer an appropriate lifetime [duplicate]
Build only `lib` target
Error when a trait is used as a type but not when used as a bound in a where clause
How does the Iron framework apply a tuple of modifiers to Response::with?
How can I take ownership of a value, passing a reference to an external API?
Borrowed local variable in nested lambda
Cannot set headers of an Iron framework Response
Generating Rust executable from LLVM bitcode

Categories

HOME
rust
svn
azure-functions
clearcase
activex
add-on
mousemove
embedded-resource
capistrano
sympy
cqrs
phpstorm-2017.1
mpu6050
global-variables
supervisord
node-webkit
azure-machine-learning
spring-ldap
web-analytics
pymongo
intel
plyr
uri
basex
microprocessors
unmarshalling
amazon-iam
quickcheck
gnu-classpath
polyml
orchardcms-1.8
hdiv
cloudera-sentry
transparency
type-inference
doctrine-extensions
linq-to-excel
axis
sharpdx
maven-jaxb2-plugin
scala-collections
deedle
polymaps
gmaps.js
prefetch
google-shopping-api
mifos
controllers
fragmentstatepageradapter
wicked-pdf
strpos
jemdoc
mnesia
flipclock
voronoi
odesk
perceptron
juniper-network-connect
stack-smash
chicagoboss
css-paged-media
angular-amd
clarion
prism-4
python-curses
findersync
dnsjava
qt5.4
ecos
aho-corasick
box2dweb
code-complexity
ivalueconverter
profiles
infinite
google-code-prettify
asp.net-mvc-3-areas
semantic-merge
getproperty
arrow
pageheap
struts2-s2hibernate
platform-independent
autosize
memory-pool
datacontracts
mediacontroller
asp.net-mvc-views
pyjamas
bass
raw-data
cleartype
rational-unified-process
bucket
.net-client-profile
tso
synthesizer

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App