duplicate
duplicate copied to clipboard
allow cfg attributes on substitution groups
Short Description:
Allows the use of cfg
attributes on substitution groups, such that they can be enabled and disabled at build time.
Motivation:
See this code.
Here, they use two cfg_attr
s to remove one substitution group based on whether a specific feature is enabled.
This is a bit ugly, as the two invocations of duplicate
are identical except for the second substitution group not being present on the second invocation.
Therefore, the first substitution group is actually copied between the two invocations.
It would be better to be able to, within the invocation itself, be able to disable a substitution group using a method akin to cfg_attr
.
Design
This could be one way of designing it (this is using verbose syntax to match that of the above example):
duplicate_item(
[
module_type [non_blocking]
maybe_async_attr [maybe_async::must_be_async]
File [tokio::fs::File]
HttpBody [reqwest::Body]
HttpClient [reqwest::Client]
]
#[cfg_attr(feature = "non_blocking")]
[
module_type [blocking]
maybe_async_attr [maybe_async::must_be_sync]
File [std::fs::File]
HttpBody [reqwest::blocking::Body]
HttpClient [reqwest::blocking::Client]
]
))]
This will disable the second substitution group when the non_blocking
feature is disabled, exactly like the current semantics of the above example.
This would work the same way for the short syntax and global substitutions (where each attribute would work on just one substitution group or substitution).
Misc:
Open questions:
- [ ] Could this be implemented by calling
cfg_attr
directly fromduplicate
, such that it can return an empty string when thecfg
is false? - [ ] Can this be extended to any attribute macro and not just
cfg
orcfg_attr
?