random_constructible/
rand_construct_env.rs1crate::ix!();
2
3pub trait RandConstructEnvironment {
4 fn create_random<R>() -> R
5 where
6 R: RandConstructEnumWithEnv,
7 Self: RandConstructProbabilityMapProvider<R> + Sized,
8 {
9 R::random_with_env::<Self>()
10 }
11
12 fn create_random_uniform<R>() -> R
13 where
14 R: RandConstructEnumWithEnv,
15 Self: RandConstructProbabilityMapProvider<R> + Sized,
16 {
17 R::random_uniform_with_env::<Self>()
18 }
19}
20
21#[macro_export]
22macro_rules! rand_construct_env {
23 ($provider:ident => $enum:ty { $($variant:ident => $weight:expr),* $(,)? }) => {
24 impl $crate::RandConstructProbabilityMapProvider<$enum> for $provider {
25 fn probability_map() -> std::sync::Arc<std::collections::HashMap<$enum, f64>> {
26 use once_cell::sync::Lazy;
27 static PROBABILITY_MAP: Lazy<std::sync::Arc<std::collections::HashMap<$enum, f64>>> = Lazy::new(|| {
28 let mut map = std::collections::HashMap::new();
29 $(
30 map.insert(<$enum>::$variant, $weight);
31 )*
32 std::sync::Arc::new(map)
33 });
34 std::sync::Arc::clone(&PROBABILITY_MAP)
35 }
36
37 fn uniform_probability_map() -> std::sync::Arc<std::collections::HashMap<$enum, f64>> {
38 use once_cell::sync::Lazy;
39 static UNIFORM_PROBABILITY_MAP: Lazy<std::sync::Arc<std::collections::HashMap<$enum, f64>>> = Lazy::new(|| {
40 let mut map = std::collections::HashMap::new();
41 $(
42 map.insert(<$enum>::$variant, 1.0);
43 )*
44 std::sync::Arc::new(map)
45 });
46 std::sync::Arc::clone(&UNIFORM_PROBABILITY_MAP)
47 }
48 }
49 };
50}