random_constructible/
prim_traits.rs

1// ---------------- [ File: random-constructible/src/prim_traits.rs ]
2crate::ix!();
3
4// Macro to implement RandConstruct for floating-point types
5macro_rules! impl_rand_construct_for_float {
6    ($($t:ty),*) => {
7        $(
8            impl RandConstruct for $t {
9                fn random() -> Self {
10                    rand::random::<$t>()
11                }
12                fn uniform() -> Self {
13                    let mut rng = rand::thread_rng();
14                    rng.gen_range(0.0 as $t .. 1.0 as $t)
15                }
16                fn random_with_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
17                    rng.gen::<$t>()
18                }
19            }
20        )*
21    };
22}
23
24impl_rand_construct_for_float!{f32, f64}
25
26// Macro to implement RandConstruct for integer types
27macro_rules! impl_rand_construct_for_integer {
28    ($($t:ty),*) => {
29        $(
30            impl RandConstruct for $t {
31                fn random() -> Self {
32                    rand::random::<$t>()
33                }
34                fn uniform() -> Self {
35                    let mut rng = rand::thread_rng();
36                    rng.gen_range(<$t>::MIN..=<$t>::MAX)
37                }
38                fn random_with_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
39                    rng.gen::<$t>()
40                }
41            }
42        )*
43    };
44}
45
46impl_rand_construct_for_integer!{i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize}