1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
use crate::{Dummy, Faker}; use rand::distributions::{Distribution, Uniform}; use rand::Rng; use std::ops; macro_rules! faker_impl { ($typ:ty) => { impl Dummy<$typ> for $typ { fn dummy(t: &$typ) -> Self { t.clone() } fn dummy_with_rng<R: Rng + ?Sized>(t: &$typ, _rng: &mut R) -> Self { t.clone() } } impl Dummy<Faker> for $typ { fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self { rng.gen() } } }; } macro_rules! range_impl { ($typ:ident) => { impl Dummy<ops::Range<Self>> for $typ { fn dummy_with_rng<R: Rng + ?Sized>(range: &ops::Range<Self>, rng: &mut R) -> Self { rng.gen_range(range.start..range.end) } } impl Dummy<ops::RangeFrom<Self>> for $typ { fn dummy_with_rng<R: Rng + ?Sized>(range: &ops::RangeFrom<Self>, rng: &mut R) -> Self { let u = Uniform::new_inclusive(range.start, std::$typ::MAX); u.sample(rng) } } impl Dummy<ops::RangeFull> for $typ { fn dummy_with_rng<R: Rng + ?Sized>(_: &ops::RangeFull, rng: &mut R) -> Self { let u = Uniform::new_inclusive(std::$typ::MIN, std::$typ::MAX); u.sample(rng) } } impl Dummy<ops::RangeInclusive<Self>> for $typ { fn dummy_with_rng<R: Rng + ?Sized>( range: &ops::RangeInclusive<Self>, rng: &mut R, ) -> Self { let u = Uniform::new_inclusive(range.start(), range.end()); u.sample(rng) } } impl Dummy<ops::RangeTo<Self>> for $typ { fn dummy_with_rng<R: Rng + ?Sized>(range: &ops::RangeTo<Self>, rng: &mut R) -> Self { rng.gen_range(std::$typ::MIN..range.end) } } impl Dummy<ops::RangeToInclusive<Self>> for $typ { fn dummy_with_rng<R: Rng + ?Sized>( range: &ops::RangeToInclusive<Self>, rng: &mut R, ) -> Self { let u = Uniform::new_inclusive(std::$typ::MIN, range.end); u.sample(rng) } } }; } macro_rules! number_impl { ($typ:ident) => { impl Dummy<Uniform<Self>> for $typ { fn dummy_with_rng<R: Rng + ?Sized>(dist: &Uniform<Self>, rng: &mut R) -> Self { dist.sample(rng) } } }; } macro_rules! integer_impl { ($typ:ident) => { faker_impl!($typ); number_impl!($typ); range_impl!($typ); }; } macro_rules! float_impl { ($typ:ident) => { faker_impl!($typ); number_impl!($typ); range_impl!($typ); }; } faker_impl!(()); faker_impl!(bool); faker_impl!(char); integer_impl!(u8); integer_impl!(u16); integer_impl!(u32); integer_impl!(u64); #[cfg(not(target_os = "emscripten"))] integer_impl!(u128); integer_impl!(usize); integer_impl!(i8); integer_impl!(i16); integer_impl!(i32); integer_impl!(i64); #[cfg(not(target_os = "emscripten"))] integer_impl!(i128); integer_impl!(isize); float_impl!(f32); float_impl!(f64);