paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

Add conditional compilation support for `impl_runtime_apis!` #14709

Closed tdimitrov closed 1 year ago

tdimitrov commented 1 year ago

This PR handles cfg_attr attribute for iml_runtime_apis!. It allows conditionally to compile different runtime api version implementations based on feature flag. E.g.:

pub struct Runtime {}
decl_runtime_apis! {
pub trait ApiWithStagingMethod {
    fn stable_one(data: u64);
    #[api_version(99)]
    fn staging_one();
}

impl_runtime_apis! {
#[cfg_attr(feature = "enable-staging-api", api_version(99))]
impl self::ApiWithStagingMethod<Block> for Runtime {
    fn stable_one(_: u64) {}

    #[cfg(feature = "enable-staging-api")]
    fn staging_one() {}
}

The code above will compile version 1 (with just stable_one) by default and version 99 (with stable_one and staging_one) if enable-staging-api feature is enabled.

EDIT: modified the sample code to match the latest implementation suggested by @bkchr

bkchr commented 1 year ago

bot rebase

paritytech-processbot[bot] commented 1 year ago

Rebased

tdimitrov commented 1 year ago

bot merge