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 ethbloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default = ["std", "serialize", "rustc-hex"]
std = ["fixed-hash/std", "crunchy/std"]
serialize = ["std", "impl-serde"]
rustc-hex = ["fixed-hash/rustc-hex"]
arbitrary = ["fixed-hash/arbitrary"]

[[bench]]
name = "bloom"
Expand Down
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 = ["ethbloom/arbitrary", "fixed-hash/arbitrary", "uint-crate/arbitrary"]
2 changes: 1 addition & 1 deletion fixed-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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.3", optional = true }
arbitrary = { version = "0.4", optional = true }

[dev-dependencies]
rand_xorshift = "0.2.0"
Expand Down
2 changes: 2 additions & 0 deletions fixed-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ fixed-hash = { version = "0.3", default-features = false }
- Disabled by default.
- `api-dummy`: Generate a dummy hash type for API documentation.
- Enabled by default at `docs.rs`
- `arbitrary`: Allow for creation of a hash from random unstructured input.
- Disabled by default.
8 changes: 4 additions & 4 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ macro_rules! impl_quickcheck_for_fixed_hash {
};
}

// Implementation for disabled rand crate support.
// Implementation for disabled `arbitrary` crate support.
//
// # Note
//
Expand All @@ -651,7 +651,7 @@ macro_rules! impl_arbitrary_for_fixed_hash {
( $name:ident ) => {};
}

// Implementation for enabled rand crate support.
// Implementation for enabled `arbitrary` crate support.
//
// # Note
//
Expand All @@ -665,8 +665,8 @@ 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 = [0u8; $crate::core_::mem::size_of::<Self>()];
u.fill_buffer(&mut res)?;
let mut res = Self::zero();
u.fill_buffer(&mut res.0)?;
Ok(Self::from(res))
}
}
Expand Down
1 change: 1 addition & 0 deletions primitive-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ rustc-hex = ["fixed-hash/rustc-hex"]
serde = ["std", "impl-serde"]
codec = ["impl-codec"]
rlp = ["impl-rlp"]
arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"]
1 change: 1 addition & 0 deletions uint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ qc = { package = "quickcheck", version = "0.9.0", optional = true }
rand = { version = "0.7.2", default-features = false, optional = true }
rustc-hex = { version = "2.0.1", default-features = false }
static_assertions = "1.0.0"
arbitrary = { version = "0.4", optional = true }

[features]
default = ["std"]
Expand Down
2 changes: 2 additions & 0 deletions uint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ see fuzz [README.md](fuzz/README.md)
- Enabled by default.
- `quickcheck`: Enable quickcheck-style property testing
- Use with `cargo test --release --features=quickcheck`.
- `arbitrary`: Allow for creation of an `uint` object from random unstructured input.
- Disabled by default.
4 changes: 4 additions & 0 deletions uint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub use qc;
#[doc(hidden)]
pub use rand;

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

#[doc(hidden)]
pub use static_assertions;

Expand Down
24 changes: 24 additions & 0 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,7 @@ macro_rules! construct_uint {
// `$n_words * 8` because macro expects bytes and
// uints use 64 bit (8 byte) words
$crate::impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8));
$crate::impl_arbitrary_for_uint!($name, ($n_words * 8));
}
}

Expand Down Expand Up @@ -1632,3 +1633,26 @@ macro_rules! impl_quickcheck_arbitrary_for_uint {
macro_rules! impl_quickcheck_arbitrary_for_uint {
($uint: ty, $n_bytes: tt) => {};
}


#[cfg(feature = "arbitrary")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_arbitrary_for_uint {
($uint: ty, $n_bytes: tt) => {
impl $crate::arbitrary::Arbitrary for $uint {
fn arbitrary(u: &mut $crate::arbitrary::Unstructured<'_>) -> $crate::arbitrary::Result<Self> {
let mut res = [0u8; $n_bytes];
u.fill_buffer(&mut res)?;
Ok(Self::from(res))
}
}
};
}

#[cfg(not(feature = "arbitrary"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_arbitrary_for_uint {
($uint: ty, $n_bytes: tt) => {};
}