Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Support paste in cfg feature context #56

@frol

Description

@frol

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`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions