Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support paste in cfg feature context #56

Closed
frol opened this issue Oct 27, 2020 · 1 comment · Fixed by #57
Closed

Support paste in cfg feature context #56

frol opened this issue Oct 27, 2020 · 1 comment · Fixed by #57

Comments

@frol
Copy link

frol commented Oct 27, 2020

I wonder if there is a way to express the following macro:

#[macro_export]
macro_rules! checked_feature {
    ($feature:ident, $current_protocol_version:expr) => {{
        paste! {
            #[cfg(feature = "protocol_feature_" $feature:snake)]
            let is_feature_enabled = PROTOCOL_FEATURES_TO_VERSION_MAPPING[&ProtocolFeature::$feature] >= $current_protocol_version;
            #[cfg(not(feature = "protocol_feature_" $feature:snake))]
            let is_feature_enabled = false;
            is_feature_enabled
        }
    }};
}

This gives me quite a cryptic message when use:

let q = checked_feature!(ForwardChunkParts, _protocol_version);
error: expected `,`, found `ForwardChunkParts`
   --> chain/chunks/src/lib.rs:654:34
    |
654 |                 checked_feature!(ForwardChunkParts, _protocol_version);
    |                 -----------------^^^^^^^^^^^^^^^^^--------------------
    |                 |                |
    |                 |                unexpected token
    |                 expected `,`

I have tried various combinations of stringify! and it seems that Rust just does not like anything but plain string literal there. I assume that the #[doc] support might be quite close to what I want here for the features.

Even if I use #[cfg(feature = stringify!($feature))] I get:

error: expected unsuffixed literal or identifier, found `stringify`
@scalexm
Copy link
Contributor

scalexm commented Oct 29, 2020

I think we could go further and try to support paste inside arbitrary attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants