From 7e3a4fb4aa14bbc0d5c0c36a55504c00aaababb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kru=CC=88ger?= Date: Fri, 16 Feb 2018 09:56:08 +0100 Subject: [PATCH] impl feature gated quickcheck arbitrary for hash --- Cargo.toml | 2 ++ src/hash.rs | 23 +++++++++++++++++++++++ src/lib.rs | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 9b2a91e..722dd5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/hash.rs b/src/hash.rs index 990294b..156f828 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -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: &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) => {} +} diff --git a/src/lib.rs b/src/lib.rs index cf5d72f..77d9f0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*;