Skip to content
Merged
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.3", optional = true }
Comment thread
ordian marked this conversation as resolved.
Outdated

[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 rand crate support.
Comment thread
kirk-baird marked this conversation as resolved.
Outdated
//
// # 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 rand crate support.
Comment thread
kirk-baird marked this conversation as resolved.
Outdated
//
// # 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 = [0u8; $crate::core_::mem::size_of::<Self>()];
Comment thread
kirk-baird marked this conversation as resolved.
Outdated
u.fill_buffer(&mut res)?;
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