| Crates.io | cfg-tt |
| lib.rs | cfg-tt |
| version | 0.3.0 |
| created_at | 2025-12-30 23:55:00.76343+00 |
| updated_at | 2026-01-03 15:04:58.550771+00 |
| description | A procedural macro that allows using #[cfg] anywhere and at token granularity. |
| homepage | https://github.com/OpenByteDev/cfg-tt |
| repository | https://github.com/OpenByteDev/cfg-tt |
| max_upload_size | |
| id | 2013649 |
| size | 36,229 |
cfg_tt! is a procedural macro that allows using #[cfg(...)] anywhere and at token granularity.
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.
Given the following code:
cfg_tt::cfg_tt! {
pub fn f() -> i32 {
1 #[cfg(windows)] + #[cfg(not(windows))] * 1
}
}
It (currently) expands to:
#[cfg(windows)]
pub fn f() -> i32 {
1 + 1
}
#[cfg(not(windows))]
pub fn f() -> i32 {
1 * 1
}
Within the cfg_tt! macro, #[cfg(...)] may appear anywhere.
Each #[cfg(...)] applies to exactly the next token tree, which may be:
{ ... }, ( ... ), [ ... ])foo)42, "x")+, ::)To conditionally include multiple token trees, they must be wrapped in a group:
cfg_tt::cfg_tt! {
let x =
#[cfg(not(windows))] { 10 + 20 }
#[cfg(windows)] { 1 + 2 };
}
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; } })
This project is licensed under the MIT License. See the LICENSE file for details.