Skip to content

allow cfg attributes on substitution groups #63

@Emoun

Description

@Emoun

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    D-discussionA decision (D) has not been made yet and is open to discussionI-featureThis issue (I) regards a (potential) feature in the projectT-acceptedTriage (T): Initial review accepted issue/PR as valid

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions