impl feature gated quickcheck arbitrary for hash

This commit is contained in:
Maximilian Krüger 2018-02-16 09:56:08 +01:00
parent 59c05a4450
commit 7e3a4fb4aa
3 changed files with 29 additions and 0 deletions

View File

@ -11,8 +11,10 @@ heapsize = { version = "0.4", optional = true }
libc = { version = "0.2", optional = true, default-features = false }
rand = { version = "0.4", optional = true }
rustc-hex = { version = "1.0", optional = true }
quickcheck = { version = "0.6", optional = true }
[features]
default = ["libc"]
std = ["rustc-hex", "rand"]
heapsizeof = ["heapsize"]
impl_quickcheck_arbitrary = ["quickcheck"]

View File

@ -301,6 +301,7 @@ macro_rules! construct_hash {
impl_std_for_hash!($from, $size);
impl_heapsize_for_hash!($from);
impl_libc_for_hash!($from, $size);
impl_quickcheck_arbitrary_for_hash!($from, $size);
}
}
@ -463,3 +464,25 @@ macro_rules! impl_libc_for_hash {
}
}
}
#[cfg(feature="impl_quickcheck_arbitrary")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_quickcheck_arbitrary_for_hash {
($name: ty, $n_bytes: tt) => {
impl $crate::quickcheck::Arbitrary for $name {
fn arbitrary<G: $crate::quickcheck::Gen>(g: &mut G) -> Self {
let mut res = [0u8; $n_bytes];
g.fill_bytes(&mut res[..$n_bytes]);
res.as_ref().into()
}
}
}
}
#[cfg(not(feature="impl_quickcheck_arbitrary"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_quickcheck_arbitrary_for_hash {
($name: ty, $n_bytes: tt) => {}
}

View File

@ -32,5 +32,9 @@ pub extern crate rustc_hex;
#[doc(hidden)]
pub extern crate rand;
#[cfg(feature="impl_quickcheck_arbitrary")]
#[doc(hidden)]
pub extern crate quickcheck;
mod hash;
pub use hash::*;