Skip to content

Commit

Permalink
Add support for valuable to fixed hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
marmistrz committed May 16, 2022
1 parent 436cb08 commit 3b9fd23
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 1 addition & 0 deletions fixed-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 39 additions & 0 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions fixed-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub use quickcheck;
#[doc(hidden)]
pub use arbitrary;

#[cfg(feature = "valuable")]
#[doc(hidden)]
pub use valuable;

#[macro_use]
mod hash;

Expand Down
1 change: 1 addition & 0 deletions primitive-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3b9fd23

Please sign in to comment.