2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2018-05-23 05:58:14 +08:00
|
|
|
use rand::distributions::{Distribution, Standard};
|
|
|
|
use rand::Rng;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/// 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]
|
2021-03-01 00:52:14 +08:00
|
|
|
pub fn reject<F: FnMut(&T) -> bool, T: Arbitrary>(g: &mut Gen, f: F) -> T {
|
2016-12-05 05:44:42 +08:00
|
|
|
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]
|
2018-05-23 05:58:14 +08:00
|
|
|
pub fn reject_rand<G: Rng + ?Sized, F: FnMut(&T) -> bool, T>(g: &mut G, f: F) -> T
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
Standard: Distribution<T>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
use std::iter;
|
2018-05-23 05:58:14 +08:00
|
|
|
iter::repeat(()).map(|_| g.gen()).find(f).unwrap()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|