cfg-tt
cfg_tt! is a procedural macro that allows using #[cfg(...)] anywhere and at token granularity.
Why
Standard #[cfg] attributes are constrained by Rust’s grammar and are applied only after parsing.
As a result, conditional compilation is limited to syntactically valid positions.
cfg_tt! operates directly on raw tokens, allowing conditional inclusion or exclusion in places where Rust normally disallows #[cfg], such as within expressions, generics, where clauses, etc.
How it works
Given the following code:
cfg_tt!
It (currently) expands to:
Usage
Within the cfg_tt! macro, #[cfg(...)] may appear anywhere.
Each #[cfg(...)] applies to exactly the next token tree, which may be:
- a group (
{ ... },( ... ),[ ... ]) - an identifier (e.g.
foo) - a literal (e.g.
42,"x") - a punctuation token (e.g.
+,::)
To conditionally include multiple token trees, they must be wrapped in a group:
cfg_tt!
Limitations
The following usages are not (yet) supported:
-
Stacked
#[cfg]attributes (e.g.#[cfg(x)] #[cfg(x)]) -
Nested overlapping
#[cfg]s
(e.g.#[cfg(any(x, y))] { #[cfg(x)] { pub struct Foo; } })
License
This project is licensed under the MIT License. See the LICENSE file for details.