From 0fff0d28db4a2f207c1c825e50c746bcdc7d9ee4 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 6 Sep 2019 16:31:37 -0300 Subject: [PATCH] Implement `Maybe*` marker traits using a declarative macro (#3557) Although simple, the current `Maybe*` trait implementations are based on a manual approach that uses some code duplication. Now this duplication is removed using a declarative macro. Tested locally, the modification don't seem to affect building times. --- substrate/core/sr-primitives/src/traits.rs | 94 +++++++--------------- 1 file changed, 30 insertions(+), 64 deletions(-) diff --git a/substrate/core/sr-primitives/src/traits.rs b/substrate/core/sr-primitives/src/traits.rs index 0ac40e9942..bbcb46be3d 100644 --- a/substrate/core/sr-primitives/src/traits.rs +++ b/substrate/core/sr-primitives/src/traits.rs @@ -590,77 +590,43 @@ impl CheckEqual for super::generic::DigestItem MaybeSerializeDebugButNotDeserialize for T {} +macro_rules! impl_maybe_marker { + ( $( $(#[$doc:meta])+ $trait_name:ident: $($trait_bound:path),+ );+ ) => { + $( + $(#[$doc])+ + #[cfg(feature = "std")] + pub trait $trait_name: $($trait_bound +)+ {} + #[cfg(feature = "std")] + impl $trait_name for T {} -/// A type that implements Serialize and Debug when in std environment. -#[cfg(not(feature = "std"))] -pub trait MaybeSerializeDebugButNotDeserialize {} -#[cfg(not(feature = "std"))] -impl MaybeSerializeDebugButNotDeserialize for T {} + $(#[$doc])+ + #[cfg(not(feature = "std"))] + pub trait $trait_name {} + #[cfg(not(feature = "std"))] + impl $trait_name for T {} + )+ + } +} -/// A type that implements Serialize when in std environment. -#[cfg(feature = "std")] -pub trait MaybeSerialize: Serialize {} -#[cfg(feature = "std")] -impl MaybeSerialize for T {} +impl_maybe_marker!( + /// A type that implements Debug when in std environment. + MaybeDebug: Debug; -/// A type that implements Serialize when in std environment. -#[cfg(not(feature = "std"))] -pub trait MaybeSerialize {} -#[cfg(not(feature = "std"))] -impl MaybeSerialize for T {} + /// A type that implements Display when in std environment. + MaybeDisplay: Display; -/// A type that implements Serialize, DeserializeOwned and Debug when in std environment. -#[cfg(feature = "std")] -pub trait MaybeSerializeDebug: Serialize + DeserializeOwned + Debug {} -#[cfg(feature = "std")] -impl MaybeSerializeDebug for T {} + /// A type that implements Hash when in std environment. + MaybeHash: ::rstd::hash::Hash; -/// A type that implements Serialize, DeserializeOwned and Debug when in std environment. -#[cfg(not(feature = "std"))] -pub trait MaybeSerializeDebug {} -#[cfg(not(feature = "std"))] -impl MaybeSerializeDebug for T {} + /// A type that implements Serialize when in std environment. + MaybeSerialize: Serialize; -/// A type that implements Debug when in std environment. -#[cfg(feature = "std")] -pub trait MaybeDebug: Debug {} -#[cfg(feature = "std")] -impl MaybeDebug for T {} + /// A type that implements Serialize, DeserializeOwned and Debug when in std environment. + MaybeSerializeDebug: Debug, DeserializeOwned, Serialize; -/// A type that implements Debug when in std environment. -#[cfg(not(feature = "std"))] -pub trait MaybeDebug {} -#[cfg(not(feature = "std"))] -impl MaybeDebug for T {} - -/// A type that implements Display when in std environment. -#[cfg(feature = "std")] -pub trait MaybeDisplay: Display {} -#[cfg(feature = "std")] -impl MaybeDisplay for T {} - -/// A type that implements Display when in std environment. -#[cfg(not(feature = "std"))] -pub trait MaybeDisplay {} -#[cfg(not(feature = "std"))] -impl MaybeDisplay for T {} - -/// A type that implements Hash when in std environment. -#[cfg(feature = "std")] -pub trait MaybeHash: ::rstd::hash::Hash {} -#[cfg(feature = "std")] -impl MaybeHash for T {} - -/// A type that implements Hash when in std environment. -#[cfg(not(feature = "std"))] -pub trait MaybeHash {} -#[cfg(not(feature = "std"))] -impl MaybeHash for T {} + /// A type that implements Serialize and Debug when in std environment. + MaybeSerializeDebugButNotDeserialize: Debug, Serialize +); /// A type that provides a randomness beacon. pub trait RandomnessBeacon {