2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
use quickcheck::{Arbitrary, Gen};
|
|
|
|
use rand::{Rand, Rng};
|
|
|
|
|
|
|
|
/// Simple helper function for rejection sampling
|
2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
pub fn reject<G: Gen, F: FnMut(&T) -> bool, T: Arbitrary>(g: &mut G, f: F) -> T {
|
|
|
|
use std::iter;
|
2018-02-02 19:26:35 +08:00
|
|
|
iter::repeat(())
|
|
|
|
.map(|_| Arbitrary::arbitrary(g))
|
|
|
|
.find(f)
|
|
|
|
.unwrap()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
pub fn reject_rand<G: Rng, F: FnMut(&T) -> bool, T: Rand>(g: &mut G, f: F) -> T {
|
|
|
|
use std::iter;
|
|
|
|
iter::repeat(()).map(|_| Rand::rand(g)).find(f).unwrap()
|
|
|
|
}
|