cfg-tt

Crates.iocfg-tt
lib.rscfg-tt
version0.3.0
created_at2025-12-30 23:55:00.76343+00
updated_at2026-01-03 15:04:58.550771+00
descriptionA procedural macro that allows using #[cfg] anywhere and at token granularity.
homepagehttps://github.com/OpenByteDev/cfg-tt
repositoryhttps://github.com/OpenByteDev/cfg-tt
max_upload_size
id2013649
size36,229
OpenByte (OpenByteDev)

documentation

https://docs.rs/cfg-tt

README

cfg-tt

CI crates.io Documentation dependency status MIT

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::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
}

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::cfg_tt! {
    let x =
        #[cfg(not(windows))] { 10 + 20 }
        #[cfg(windows)] { 1 + 2 };
}

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.

Commit count: 0

cargo fmt