From 3b9fd2348d4b4e30ad258c9dd43f73c9b0720239 Mon Sep 17 00:00:00 2001 From: Marcin Mielniczuk Date: Mon, 16 May 2022 15:06:20 +0200 Subject: [PATCH] Add support for valuable to fixed hashes --- ethereum-types/Cargo.toml | 1 + fixed-hash/Cargo.toml | 1 + fixed-hash/src/hash.rs | 39 ++++++++++++++++++++++++++++++++++++++ fixed-hash/src/lib.rs | 4 ++++ primitive-types/Cargo.toml | 1 + 5 files changed, 46 insertions(+) diff --git a/ethereum-types/Cargo.toml b/ethereum-types/Cargo.toml index 3449beccd..04315f0ab 100644 --- a/ethereum-types/Cargo.toml +++ b/ethereum-types/Cargo.toml @@ -29,3 +29,4 @@ arbitrary = ["ethbloom/arbitrary", "fixed-hash/arbitrary", "uint-crate/arbitrary rlp = ["impl-rlp", "ethbloom/rlp", "primitive-types/rlp"] codec = ["impl-codec", "ethbloom/codec", "scale-info", "primitive-types/scale-info"] num-traits = ["primitive-types/num-traits"] +valuable = ["fixed-hash/valuable"] diff --git a/fixed-hash/Cargo.toml b/fixed-hash/Cargo.toml index 27ad7d217..1263c037c 100644 --- a/fixed-hash/Cargo.toml +++ b/fixed-hash/Cargo.toml @@ -21,6 +21,7 @@ rand = { version = "0.8.0", optional = true, default-features = false } rustc-hex = { version = "2.0.1", optional = true, default-features = false } static_assertions = "1.0.0" arbitrary = { version = "1.0", optional = true } +valuable = { version = "0.1.0", optional = true } [dev-dependencies] rand_xorshift = "0.3.0" diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index 232245868..36150e236 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -316,6 +316,7 @@ macro_rules! construct_fixed_hash { impl_rustc_hex_for_fixed_hash!($name); impl_quickcheck_for_fixed_hash!($name); impl_arbitrary_for_fixed_hash!($name); + impl_valuable_for_fixed_hash!($name); } } @@ -638,6 +639,44 @@ macro_rules! impl_quickcheck_for_fixed_hash { }; } +// Implementation for disabled valuable crate support. +// +// # Note +// +// Feature guarded macro definitions instead of feature guarded impl blocks +// to work around the problems of introducing `valuable` crate feature in +// a user crate. +#[cfg(not(feature = "valuable"))] +#[macro_export] +#[doc(hidden)] +macro_rules! impl_valuable_for_fixed_hash { + ( $name:ident ) => {}; +} + +// Implementation for enabled valuable crate support. +// +// # Note +// +// Feature guarded macro definitions instead of feature guarded impl blocks +// to work around the problems of introducing `valuable` crate feature in +// a user crate. +#[cfg(feature = "valuable")] +#[macro_export] +#[doc(hidden)] +macro_rules! impl_valuable_for_fixed_hash { + ( $name:ident ) => { + impl $crate::valuable::Valuable for $name { + fn as_value(&self) -> $crate::valuable::Value<'_> { + $crate::valuable::Value::Listable(&self.0) + } + + fn visit(&self, visit: &mut dyn $crate::valuable::Visit) { + visit.visit_unnamed_fields(&[$crate::valuable::Value::String(&format!("{:?}", self))]) + } + } + }; +} + // When the `arbitrary` feature is disabled. // // # Note diff --git a/fixed-hash/src/lib.rs b/fixed-hash/src/lib.rs index 228f551e0..8838f771e 100644 --- a/fixed-hash/src/lib.rs +++ b/fixed-hash/src/lib.rs @@ -50,6 +50,10 @@ pub use quickcheck; #[doc(hidden)] pub use arbitrary; +#[cfg(feature = "valuable")] +#[doc(hidden)] +pub use valuable; + #[macro_use] mod hash; diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index e09b21215..4dc0b4561 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -30,6 +30,7 @@ rlp = ["impl-rlp"] arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"] fp-conversion = ["std"] num-traits = ["impl-num-traits"] +valuable = ["fixed-hash/valuable"] [[test]] name = "scale_info"