Add conditional compilation support for iml_runtime_apis! (#14709)

* Handle `cfg_attr` in `decl_runtime_api`

* Integration tests

* UI tests

* Enable staging api tests on CI

* docs

* Comments and minor style fixes

* Fix doc comments

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Fix formatting and a compilation error

* Fix a doc comment

* Combine the errors from `ApiImplItem`

* Fix a typo in UI test

* Use attribute span when reporting multiple conditional `api_version` error

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Don't use `cfg_attr` for methods. Use simple feature gate instead

* Remove a stale comment

* Update primitives/api/proc-macro/src/impl_runtime_apis.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Revert "Update primitives/api/proc-macro/src/impl_runtime_apis.rs"

This reverts commit 4da20a79bd762580ebf502e9f807c2d7e5876106.

* Code review feedback

* Style improvements

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: parity-processbot <>
This commit is contained in:
Tsvetomir Dimitrov
2023-08-24 11:06:07 +03:00
committed by GitHub
parent cd464f9cfd
commit dbb48a5ba2
5 changed files with 301 additions and 18 deletions
+48
View File
@@ -344,6 +344,54 @@ pub use sp_api_proc_macro::decl_runtime_apis;
/// ```
/// In this case `Balance` api version 3 is being implemented for `Runtime`. The `impl` block
/// must contain all methods declared in version 3 and below.
///
/// # Conditional version implementation
///
/// `impl_runtime_apis!` supports `cfg_attr` attribute for conditional compilation. For example
/// let's say you want to implement a staging version of the runtime api and put it behind a
/// feature flag. You can do it this way:
/// ```ignore
/// pub struct Runtime {}
/// sp_api::decl_runtime_apis! {
/// pub trait ApiWithStagingMethod {
/// fn stable_one(data: u64);
///
/// #[api_version(99)]
/// fn staging_one();
/// }
/// }
///
/// sp_api::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() {}
/// }
/// }
/// ```
///
/// [`decl_runtime_apis!`] declares two version of the api - 1 (the default one, which is
/// considered stable in our example) and 99 (which is considered staging). In
/// `impl_runtime_apis!` a `cfg_attr` attribute is attached to the `ApiWithStagingMethod`
/// implementation. If the code is compiled with `enable-staging-api` feature a version 99 of
/// the runtime api will be built which will include `staging_one`. Note that `staging_one`
/// implementation is feature gated by `#[cfg(feature = ... )]` attribute.
///
/// If the code is compiled without `enable-staging-api` version 1 (the default one) will be
/// built which doesn't include `staging_one`.
///
/// `cfg_attr` can also be used together with `api_version`. For the next snippet will build
/// version 99 if `enable-staging-api` is enabled and version 2 otherwise because both
/// `cfg_attr` and `api_version` are attached to the impl block:
/// ```ignore
/// #[cfg_attr(feature = "enable-staging-api", api_version(99))]
/// #[api_version(2)]
/// impl self::ApiWithStagingAndVersionedMethods<Block> for Runtime {
/// // impl skipped
/// }
/// ```
pub use sp_api_proc_macro::impl_runtime_apis;
/// Mocks given trait implementations as runtime apis.