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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2016 bluss
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Implementation of `derive(Rand!)` for `macro_attr!{}`.
//!
//! This crate defines a macro `Rand!{}` that can be used through `macro_attr!{}`
//! to derive an implementation of the `Rand` trait (crate rand version 0.3.x).
//!
//! Using this macro also depends on crates `parse_macros` and `parse_generics_shim`,
//! which must be included in the crate that uses them.
//!
//! ## Example
//!
//! ```
//! extern crate rand;
//!
//! #[macro_use] extern crate parse_macros;
//! #[macro_use] extern crate parse_generics_shim;
//! #[macro_use] extern crate macro_attr;
//!
//! #[macro_use] extern crate rand_derive;
//!
//! macro_attr! {
//!     #[derive(Rand!, Debug)]
//!     enum TestEnum {
//!         A,
//!         B,
//!         C,
//!     }
//! }
//!
//! macro_attr! {
//!     #[derive(Rand!, Debug)]
//!     struct Point<T> {
//!         x: T,
//!         y: T,
//!     }
//! }
//!
//! fn main() {
//!     let t: TestEnum = rand::random();
//!     let p: Point<f32> = rand::random();
//! }
//! ```
//!
//! ## Known Limitations
//!
//! If the struct or enum is too complex, the compiler may run up against
//! the recursion limit when compiling your crate. This can be adjusted
//! with an attribute like `#![recursion_limit="128"]`.
//!
//! * Does not allow explicit discriminants on unitary enum variants
//! * Does not yet allow customizing which type parameters get the `T: Rand`
//!   bound applied. Right now they all get it.
#![cfg_attr(not(test), no_std)]

//#![cfg_attr(test, feature(trace_macros))]
#![recursion_limit="128"]
#[cfg(test)]
#[macro_use] extern crate parse_macros;
#[cfg(test)]
#[macro_use] extern crate parse_generics_shim;
#[cfg(test)]
#[macro_use] extern crate macro_attr;
#[cfg(test)]
extern crate rand;

/// Implementation of `derive(Rand!)` for `macro_attr!{}`.
#[macro_export]
macro_rules! Rand {
    (
        () $($tail:tt)*
    ) => {
        parse_item! {
            then Rand! { @item },
            $($tail)*
        }
    };
    // enum
    (
        @item
        enum {
            attrs: $_attrs:tt,
            vis: $_vis:tt,
            name: $name:ident,
            generics: {
                constr: [$($constr:tt)*],
                params: [$($params:tt)*],
                ltimes: $_ltimes:tt,
                tnames: [$($tnames:ident,)*],
            },
            where: {
                clause: $_clause:tt,
                preds: [$($preds:tt)*],
            },
            variants: [
                $({
                    ord: ($ord:expr, $_ord:tt),
                    attrs: [$($_vattrs:tt)*],
                    kind: $vkind:ident,
                    name: $vname:ident,
                    fields: $vfields:tt,
                    num_fields: $vnum_fields:expr,
                },)+  // + because 0 variants is explicitly unsupported
            ],
            num_variants: $num_variants:expr,
            $($_enum_tail:tt)*
        }
    ) => {
        Rand!{ @as_item
            impl<$($constr)*> ::rand::Rand for $name<$($params)*>
                where (): ::rand::Rand, $($tnames: ::rand::Rand,)* $($preds)*
            {
                fn rand<R: ::rand::Rng>(_rng: &mut R) -> Self {
                    let variant = Rand!(
                        @isone [$($vname)*]
                        0,
                        _rng.gen_range(0, $num_variants));
                    match variant {
                    $(
                        $ord => Rand!(@enum $vkind _rng $name $vname $vfields),
                    )+
                        _ => loop { }
                    }
                }
            }
        }
    };
    // @isone: test if there is exactly one tt in the list, then $e else $f
    (@isone [$_one:tt] $e:expr, $_f:expr) => { $e };
    (@isone [$($_notone:tt)*] $_e:expr, $f:expr) => { $f };
    (@enum unitary $rng:ident $name:ident $vname:ident $vfields:tt) => {
        $name::$vname
    };
    (@enum tuple $rng:ident $name:ident $vname:ident
     [$($vfield:tt,)*]
    ) => {
        $name::$vname($(Rand!(@substitute $vfield $rng.gen())),*)
    };
    (@enum record $rng:ident $name:ident $vname:ident
     [$({
         ord: $_ford:tt,
         attrs: $_fattrs:tt,
         vis: $_fvis:tt,
         ty: $_fty:ty,
         name: $fname:ident,
      },)*]
    ) => {
        $name::$vname {
            $(
                $fname: $rng.gen()
            ),*
        }
    };
    // struct
    (
        @item
        struct {
            attrs: $_attrs:tt,
            vis: $_vis:tt,
            name: $name:ident,
            generics: {
                constr: [$($constr:tt)*],
                params: [$($params:tt)*],
                ltimes: $_ltimes:tt,
                tnames: [$($tnames:ident,)*],
            },
            where: {
                clause: $_clause:tt,
                preds: [$($preds:tt)*],
            },
            kind: $kind:ident,
            fields: $fields:tt,
            $($_struct_tail:tt)*
        }
    ) => {
        Rand!{ @as_item
            impl<$($constr)*> ::rand::Rand for $name<$($params)*>
                where (): ::rand::Rand, $($tnames: ::rand::Rand,)* $($preds)*
            {
                fn rand<R: ::rand::Rng>(_rng: &mut R) -> Self {
                    Rand!{@struct $kind _rng $name $fields }
                }
            }
        }
    };
    (@struct unitary $rng:ident $name:ident $vfields:tt) => {
        $name
    };
    (@struct tuple $rng:ident $name:ident
     [$($vfield:tt,)*]
    ) => {
        $name($(Rand!(@substitute $vfield $rng.gen())),*)
    };
    (@struct record $rng:ident $name:ident
     [$({
         ord: $_ford:tt,
         attrs: $_fattrs:tt,
         vis: $_fvis:tt,
         ty: $_fty:ty,
         name: $fname:ident,
      },)*]
    ) => {
        $name {
            $(
                $fname: $rng.gen()
            ),*
        }
    };
    (@substitute $_input:tt $output:expr) => { $output };
    (@as_item $i:item) => { $i };
}

#[cfg(test)]
mod tests {
    //trace_macros!(true);
    use rand::random;
    macro_attr! {
        #[derive(Rand!, Debug)]
        enum Test {
            A, B, C,
        }
    }
    /*
       // Does not compile with 0 variants
    macro_attr! {
        #[derive(Rand!, Debug)]
        pub enum Test2 {
        }
    }
    */

    macro_attr! {
        #[derive(Rand!, Debug)]
        enum Test1 {
            A,
        }
    }
    macro_attr! {
        #[derive(Rand!, Debug)]
        enum Test2 {
            A,
            B,
        }
    }

    #[test]
    fn it_works() {
        let t: Test = random();
        println!("{:?}", t);
        let t1: Test1 = random();
        println!("{:?}", t1);
        let t2: Test2 = random();
        println!("{:?}", t2);
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        enum Test3 {
            A(i8),
            B(Test2),
        }
    }
    #[test]
    fn enum_tuplevar() {
        let t: Test3 = random();
        println!("{:?}", t);
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        enum TestS {
            A { x: u8, y: u8 },
            B { x: u8, y: u8, z: u8 },
            C { },
        }
    }
    macro_attr! {
        #[derive(Rand!, Debug)]
        enum TestS2 {
            A { },
        }
    }
    #[test]
    fn enum_structvar() {
        let t: TestS = random();
        println!("{:?}", t);
        let s: TestS2 = random();
        println!("{:?}", s);
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        enum TestGeneric1<T> where T: ::rand::Rand {
            A { x: T },
            B { x: u8, y: u8, z: u8 },
        }
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        enum TestGeneric2<T> {
            A { x: T },
            B { x: u8, y: u8, z: u8 },
        }
    }

    #[test]
    fn enum_generic() {
        let t: TestGeneric1<TestS> = random();
        println!("{:?}", t);
        let s: TestGeneric2<()> = random();
        println!("{:?}", s);
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        struct TestStruct;
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        struct TestStruct2 {
            x: u8,
            y: (),
        }
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        struct TestStruct3(u8, Test1);
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        struct TestStruct4<T, U> where T: 'static {
            x: T,
            y: U,
        }
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        struct TestStruct5 {}
    }


    #[test]
    fn struct_simple() {
        let t: TestStruct = random();
        println!("{:?}", t);
        let s: TestStruct2 = random();
        println!("{:?}", s);
        let u: TestStruct3 = random();
        println!("{:?}", u);
        let v: TestStruct4<TestStruct, TestStruct2> = random();
        println!("{:?}", v);
        let w: TestStruct5 = random();
        println!("{:?}", w);
    }

    macro_attr! {
        #[derive(Rand!, Debug)]
        struct BigStruct<T> {
            a: T,
            b: (),
            c: i32,
            d: i32,
            e: i32,
            f: u8,
            g: u8,
            h: u8,
            i: f32,
            j: f32,
            k: f32,
            l: f32,
            m: f32,
            n: f64,
            o: Test,
            p: Test1,
            q: TestStruct,
            r: u8,
            s: u8,
            t: u8,
            u: u8,
            v: u8,
            x: u8,
            y: u8,
            z: u8,
        }
    }

    #[test]
    fn struct_big() {
        let t: BigStruct<i32> = random();
        println!("{:?}", t);
    }
}