Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for valuable to fixed hashes #642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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