random_constructible/
rand_construct_enum.rs1crate::ix!();
3
4pub trait RandConstructProbabilityMapProvider<R: Eq + Hash + Sized> {
5 fn probability_map() -> Arc<HashMap<R, f64>>;
6 fn uniform_probability_map() -> Arc<HashMap<R, f64>>;
7}
8
9pub trait RandConstructEnumWithEnv: Sized + Clone + Eq + Hash {
10
11 fn random_with_env<P: RandConstructProbabilityMapProvider<Self>>() -> Self {
12 let mut rng = rand::thread_rng();
13 Self::sample_from_provider::<P,_>(&mut rng)
14 }
15
16 fn random_uniform_with_env<P: RandConstructProbabilityMapProvider<Self>>() -> Self {
17 let mut rng = rand::thread_rng();
18 Self::sample_uniformly_from_provider::<P,_>(&mut rng)
19 }
20
21 fn sample_from_provider<P: RandConstructProbabilityMapProvider<Self>, RNG: Rng + ?Sized>(rng: &mut RNG) -> Self {
23 let probs = P::probability_map();
24 sample_variants_with_probabilities(rng,&probs)
25 }
26
27 fn sample_uniformly_from_provider<P: RandConstructProbabilityMapProvider<Self>, RNG: Rng + ?Sized>(rng: &mut RNG) -> Self {
28 let probs = P::uniform_probability_map();
29 sample_variants_with_probabilities(rng,&probs)
30 }
31}
32
33pub trait RandConstructEnum: Clone + Default + Eq + Hash + Sized {
34
35 fn default_weight(&self) -> f64;
37
38 fn all_variants() -> Vec<Self>;
39
40 fn create_default_probability_map() -> Arc<HashMap<Self,f64>>;
42
43 fn random_variant() -> Self {
45 let map = Self::create_default_probability_map();
46 let mut rng = rand::thread_rng();
47 sample_variants_with_probabilities(&mut rng, &map)
48 }
49
50 fn uniform_variant() -> Self {
51 let variants = Self::all_variants();
52 let mut rng = rand::thread_rng();
53 variants.choose(&mut rng).unwrap().clone()
54 }
55
56 fn random_enum_value_with_rng<RNG: Rng + ?Sized>(rng: &mut RNG) -> Self {
59 let map = Self::create_default_probability_map();
60 sample_variants_with_probabilities(rng, &map)
61 }
62}