aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lib.rs
blob: 40f02e9d3945b87a66bc426f8d0ef090cf55177c (plain)
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
124
125
126
127
128
129
130
//! Generate random data.
//!
//! # Examples
//!
//! ```
//! use generate_random::GenerateRandom;
//!
//! #[derive(GenerateRandom)]
//! enum MyEnum {
//!     A,
//!     C(bool),
//!     B {
//!         x: u8,
//!     },
//!     // Providing a weight allows changing the probabilities.
//!     // This variant is now twice as likely to be generated as the others.
//!     #[weight(2)]
//!     D,
//! }
//!
//! let mut rng = rand::thread_rng();
//! let my_value = MyEnum::generate_random(&mut rng);
//! ```

/// This derive macro provides an implementation
/// of the [`trait@GenerateRandom`] trait.
///
/// Enum variants can be given a `weight` attribute
/// to change how often it is generated.
/// By default, the weight is `1`.
/// The probability of a variants is its weight
/// divided by the sum over all variants.
pub use generate_random_macro::GenerateRandom;

/// Enable randomly generating values of a type.
///
/// This trait can be implemented using the derive
/// macro of the same name: [`macro@GenerateRandom`].
pub trait GenerateRandom {
    /// Create a new random value of this type.
    fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self;
}

macro_rules! impl_generate_random {
    ( $( $t:ty, )+ ) => {
        $(
            impl GenerateRandom for $t {
                fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
                    rng.gen()
                }
            }
        )+
    }
}

impl_generate_random! {
    bool,
    char,
    u8,
    i8,
    u16,
    i16,
    u32,
    i32,
    u64,
    i64,
    u128,
    i128,
    usize,
    isize,
    f32,
    f64,
}

impl<T: GenerateRandom> GenerateRandom for Option<T> {
    fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
        if bool::generate_random(rng) {
            Some(T::generate_random(rng))
        } else {
            None
        }
    }
}

macro_rules! impl_generate_random_tuple {
    ( $t0:ident $( $t:ident )* ) => {
        impl< $t0, $( $t, )* > GenerateRandom for ( $t0, $( $t, )* )
        where
            $t0: GenerateRandom,
            $(
                $t: GenerateRandom,
            )*
        {
            fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
                (
                    $t0::generate_random(rng),
                    $(
                        $t::generate_random(rng),
                    )*
                )
            }
        }
        impl_generate_random_tuple!( $( $t )* );
    };
    () => {
        impl GenerateRandom for () {
            fn generate_random<R: rand::Rng + ?Sized>(_rng: &mut R) -> Self {
                ()
            }
        }
    }
}

impl_generate_random_tuple!(A B C D E F G H I J K L);

#[cfg(test)]
mod tests {
    use super::*;

    fn rng() -> impl rand::Rng {
        use rand::SeedableRng;
        rand_chacha::ChaCha8Rng::from(rand_chacha::ChaCha8Core::from_seed([37; 32]))
    }

    #[test]
    fn test_u8() {
        let mut rng = rng();
        assert_eq!(u8::generate_random(&mut rng), 55);
    }
}