From e7e13116f7febf153d7d0c32912249195f74e996 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 30 May 2023 13:15:13 +0100 Subject: [PATCH] app_crypto: macro improvements (#14263) * app_crypto: macro improvements During `app_crypto` macro expansion the `cfg` feature gate was injected into the macro client crate. This resulted in compilation error if `serde` or `std` was not defined in client crate. This PR fixes this problem. For reference, the error was: ``` error: cannot find macro `format` in this scope --> /home/miszka/parity/10-genesis-config/substrate-2/primitives/consensus/aura/src/lib.rs:32:3 | 32 | app_crypto!(sr25519, AURA); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ... error[E0433]: failed to resolve: use of undeclared type `String` --> /home/miszka/parity/10-genesis-config/substrate-2/primitives/consensus/aura/src/lib.rs:32:3 | 32 | app_crypto!(sr25519, AURA); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `String` ``` * app_crypto: cleanup --- .../primitives/application-crypto/src/lib.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/substrate/primitives/application-crypto/src/lib.rs b/substrate/primitives/application-crypto/src/lib.rs index 11be54e29c..46f59719f9 100644 --- a/substrate/primitives/application-crypto/src/lib.rs +++ b/substrate/primitives/application-crypto/src/lib.rs @@ -32,9 +32,6 @@ pub use sp_core::{ crypto::{ByteArray, CryptoType, Derive, IsWrappedBy, Public, UncheckedFrom, Wraps}, RuntimeDebug, }; -#[doc(hidden)] -#[cfg(all(not(feature = "std"), feature = "serde"))] -pub use sp_std::alloc::{format, string::String}; #[doc(hidden)] pub use codec; @@ -328,6 +325,14 @@ macro_rules! app_crypto_public_common { }; } +#[doc(hidden)] +pub mod module_format_string_prelude { + #[cfg(all(not(feature = "std"), feature = "serde"))] + pub use sp_std::alloc::{format, string::String}; + #[cfg(feature = "std")] + pub use std::{format, string::String}; +} + /// Implements traits for the public key type if `feature = "serde"` is enabled. #[cfg(feature = "serde")] #[doc(hidden)] @@ -365,9 +370,7 @@ macro_rules! app_crypto_public_common_if_serde { where D: $crate::serde::Deserializer<'de>, { - use $crate::Ss58Codec; - #[cfg(all(not(feature = "std"), feature = "serde"))] - use $crate::{format, String}; + use $crate::{module_format_string_prelude::*, Ss58Codec}; Public::from_ss58check(&String::deserialize(deserializer)?) .map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e)))