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 arbitrary trait implementation #378

Merged
merged 11 commits into from
Apr 23, 2020
1 change: 1 addition & 0 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ serde_json = "1.0.41"
default = ["std", "serialize"]
std = ["uint-crate/std", "fixed-hash/std", "ethbloom/std", "primitive-types/std"]
serialize = ["std", "impl-serde", "primitive-types/serde", "ethbloom/serialize"]
arbitrary = ["std", "serialize", "fixed-hash/arbitrary"]
ordian marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions fixed-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ quickcheck = { version = "0.9.0", optional = true }
rand = { version = "0.7.2", optional = true, default-features = false }
rustc-hex = { version = "2.0.1", optional = true, default-features = false }
static_assertions = "1.0.0"
arbitrary = { version = "0.4", optional = true }

[dev-dependencies]
rand_xorshift = "0.2.0"
Expand Down
37 changes: 37 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_cmp_for_fixed_hash!($name);
impl_rustc_hex_for_fixed_hash!($name);
impl_quickcheck_for_fixed_hash!($name);
impl_arbitrary_for_fixed_hash!($name);
}
}

Expand Down Expand Up @@ -636,6 +637,42 @@ macro_rules! impl_quickcheck_for_fixed_hash {
};
}

// Implementation for disabled `arbitrary` crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `arbitrary` crate feature in
// a user crate.
#[cfg(not(feature = "arbitrary"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_arbitrary_for_fixed_hash {
( $name:ident ) => {};
}

// Implementation for enabled `arbitrary` crate support.
kirk-baird marked this conversation as resolved.
Show resolved Hide resolved
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `arbitrary` crate feature in
// a user crate.
#[cfg(feature = "arbitrary")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_arbitrary_for_fixed_hash {
( $name:ident ) => {
impl $crate::arbitrary::Arbitrary for $name {
fn arbitrary(u: &mut $crate::arbitrary::Unstructured<'_>) -> $crate::arbitrary::Result<Self> {
let mut res = Self::zero();
u.fill_buffer(&mut res.0)?;
Ok(Self::from(res))
}
}
};
}

#[macro_export]
#[doc(hidden)]
macro_rules! impl_ops_for_hash {
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 @@ -46,6 +46,10 @@ pub use rand;
#[doc(hidden)]
pub use quickcheck;

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

#[macro_use]
mod hash;

Expand Down