-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for non
#[derive]
-input items (#1)
The trick being to use the discriminant specifier of an `enum` as a channel through which to smuggle an arbitrary block, within which arbitrary items may occur.
- Loading branch information
Showing
5 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ name = "cfg_eval" | |
authors = [ | ||
"Daniel Henry-Mantilla <[email protected]>", | ||
] | ||
version = "0.1.0" | ||
version = "0.1.2" | ||
edition = "2021" | ||
rust-version = "1.61.0" | ||
|
||
|
@@ -31,10 +31,13 @@ docs-rs = [ | |
"better-docs", | ||
] | ||
|
||
# Support for any kind of item, not only derive inputs. | ||
items = [] | ||
|
||
[dependencies] | ||
proc-macro2.version = "1.0.0" | ||
quote.version = "1.0.0" | ||
syn.version = "=2.0.0" | ||
syn.version = "2.0.0" | ||
syn.default-features = false | ||
syn.features = [ | ||
"parsing", | ||
|
@@ -44,6 +47,8 @@ syn.features = [ | |
|
||
[dev-dependencies] | ||
macro_rules_attribute.version = "0.2.0" | ||
cfg_eval.path = "." | ||
cfg_eval.features = ["items"] | ||
|
||
[workspace] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
macro_rules! stringify_rhs {( | ||
const _: () = $value:tt ; | ||
) => ( | ||
stringify! $value | ||
)} | ||
|
||
macro_rules! cfg_eval_stringify {( | ||
$($body:tt)* | ||
) => ({ | ||
#[::cfg_eval::cfg_eval] | ||
#[::macro_rules_attribute::apply(stringify_rhs!)] | ||
const _: () = { $($body)* }; | ||
})} | ||
|
||
#[test] | ||
fn main() | ||
{ | ||
assert_eq!( | ||
cfg_eval_stringify! { | ||
enum _WithoutCfgEval { | ||
#[cfg(any())] | ||
NonExistingVariant, | ||
} | ||
|
||
#[cfg(all())] | ||
#[cfg_attr(all(), foo)] | ||
fn foo() | ||
{} | ||
|
||
cfg!(any()); | ||
|
||
#[cfg(all())] { | ||
42 | ||
} | ||
|
||
#[cfg(any())] { | ||
27 | ||
} | ||
}, | ||
stringify! { | ||
enum _WithoutCfgEval { | ||
} | ||
|
||
#[cfg(all())] // <- Redundantly kept. | ||
#[foo] | ||
fn foo() | ||
{} | ||
|
||
// Not expanded. | ||
cfg!(any()); | ||
|
||
#[cfg(all())] /* <- Redundantly kept. */ { | ||
42 | ||
} | ||
}, | ||
); | ||
} |