duplicate icon indicating copy to clipboard operation
duplicate copied to clipboard

allow cfg attributes on substitution groups

Open Emoun opened this issue 7 months ago • 3 comments

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_attrs 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 from duplicate, such that it can return an empty string when the cfg is false?
  • [ ] Can this be extended to any attribute macro and not just cfg or cfg_attr?

Emoun avatar Jul 20 '24 08:07 Emoun