File tree Expand file tree Collapse file tree 2 files changed +41
-12
lines changed Expand file tree Collapse file tree 2 files changed +41
-12
lines changed Original file line number Diff line number Diff line change @@ -63,12 +63,11 @@ fn impl_rand_derive(ast: &syn::MacroInput) -> quote::Tokens {
63
63
}
64
64
65
65
let len = body. len ( ) ;
66
- let mut variants = body
66
+ let mut arms = body
67
67
. iter ( )
68
- . enumerate ( )
69
- . map ( |( index, variant) | {
68
+ . map ( |variant| {
70
69
let ident = & variant. ident ;
71
- let arm = match variant. data {
70
+ match variant. data {
72
71
syn:: VariantData :: Struct ( ref body) => {
73
72
let fields = body
74
73
. iter ( )
@@ -85,13 +84,24 @@ fn impl_rand_derive(ast: &syn::MacroInput) -> quote::Tokens {
85
84
quote ! { #name:: #ident ( #( #fields) , * ) }
86
85
} ,
87
86
syn:: VariantData :: Unit => quote ! { #name:: #ident }
88
- } ;
87
+ }
88
+ } ) ;
89
89
90
- quote ! { #index => #arm }
91
- } )
92
- . collect :: < Vec < _ > > ( ) ;
93
- variants. push ( quote ! { _ => unreachable!( ) } ) ;
94
- quote ! { match __rng. gen_range( 0 , #len) { #( #variants, ) * } }
90
+ match len {
91
+ 1 => quote ! { #( #arms) * } ,
92
+ 2 => {
93
+ let ( a, b) = ( arms. next ( ) , arms. next ( ) ) ;
94
+ quote ! { if __rng. gen ( ) { #a } else { #b } }
95
+ } ,
96
+ _ => {
97
+ let mut variants = arms
98
+ . enumerate ( )
99
+ . map ( |( index, arm) | quote ! { #index => #arm } )
100
+ . collect :: < Vec < _ > > ( ) ;
101
+ variants. push ( quote ! { _ => unreachable!( ) } ) ;
102
+ quote ! { match __rng. gen_range( 0 , #len) { #( #variants, ) * } }
103
+ } ,
104
+ }
95
105
}
96
106
} ;
97
107
Original file line number Diff line number Diff line change @@ -19,7 +19,23 @@ struct Tuple(i16, Option<f64>);
19
19
struct Unit ;
20
20
21
21
#[ derive( Rand ) ]
22
- enum Enum {
22
+ enum EnumUnit {
23
+ X ,
24
+ }
25
+
26
+ #[ derive( Rand ) ]
27
+ enum Enum1 {
28
+ X ( u8 , f32 ) ,
29
+ }
30
+
31
+ #[ derive( Rand ) ]
32
+ enum Enum2 {
33
+ X ( bool ) ,
34
+ Y ,
35
+ }
36
+
37
+ #[ derive( Rand ) ]
38
+ enum Enum3 {
23
39
X { x : u8 , y : isize } ,
24
40
Y ( [ bool ; 4 ] ) ,
25
41
Z ,
@@ -34,6 +50,9 @@ fn smoke() {
34
50
let _ = rng. gen :: < Struct > ( ) ;
35
51
let _ = rng. gen :: < Tuple > ( ) ;
36
52
let _ = rng. gen :: < Unit > ( ) ;
37
- let _ = rng. gen :: < Enum > ( ) ;
53
+ let _ = rng. gen :: < EnumUnit > ( ) ;
54
+ let _ = rng. gen :: < Enum1 > ( ) ;
55
+ let _ = rng. gen :: < Enum2 > ( ) ;
56
+ let _ = rng. gen :: < Enum3 > ( ) ;
38
57
}
39
58
}
You can’t perform that action at this time.
0 commit comments