@@ -115,6 +115,40 @@ impl Rand for bool {
115
115
}
116
116
}
117
117
118
+ macro_rules! tuple_impl {
119
+ // use variables to indicate the arity of the tuple
120
+ ( $( $tyvar: ident) ,* ) => {
121
+ // the trailing commas are for the 1 tuple
122
+ impl <
123
+ $( $tyvar : Rand ) ,*
124
+ > Rand for ( $( $tyvar ) ,* , ) {
125
+
126
+ fn rand ( _rng: @Rng ) -> ( $( $tyvar ) ,* , ) {
127
+ (
128
+ // use the $var's to get the appropriate number of repeats
129
+ // (they're not actually needed)
130
+ $(
131
+ _rng. gen :: <$tyvar>( )
132
+ ) ,*
133
+ ,
134
+ )
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ impl Rand for ( ) { fn rand ( _: @Rng ) -> ( ) { ( ) } }
141
+ tuple_impl ! { A }
142
+ tuple_impl ! { A , B }
143
+ tuple_impl ! { A , B , C }
144
+ tuple_impl ! { A , B , C , D }
145
+ tuple_impl ! { A , B , C , D , E }
146
+ tuple_impl ! { A , B , C , D , E , F }
147
+ tuple_impl ! { A , B , C , D , E , F , G }
148
+ tuple_impl ! { A , B , C , D , E , F , G , H }
149
+ tuple_impl ! { A , B , C , D , E , F , G , H , I }
150
+ tuple_impl ! { A , B , C , D , E , F , G , H , I , J }
151
+
118
152
impl < T : Rand > Rand for Option < T > {
119
153
fn rand ( rng : @rand:: Rng ) -> Option < T > {
120
154
if rng. gen_bool ( ) {
@@ -125,6 +159,14 @@ impl<T:Rand> Rand for Option<T> {
125
159
}
126
160
}
127
161
162
+ impl < T : Rand > Rand for ~T {
163
+ fn rand ( rng : @Rng ) -> ~T { ~rng. gen ( ) }
164
+ }
165
+
166
+ impl < T : Rand > Rand for @T {
167
+ fn rand ( rng : @Rng ) -> @T { @rng. gen ( ) }
168
+ }
169
+
128
170
#[ allow( non_camel_case_types) ] // runtime type
129
171
pub enum rust_rng { }
130
172
@@ -927,6 +969,10 @@ mod tests {
927
969
let _n : uint = rand:: random ( ) ;
928
970
let _f : f32 = rand:: random ( ) ;
929
971
let _o : Option < Option < i8 > > = rand:: random ( ) ;
972
+ let _many : ( ( ) ,
973
+ ( ~uint , @int , ~Option < ~( @char , ~( @bool , ) ) > ) ,
974
+ ( u8 , i8 , u16 , i16 , u32 , i32 , u64 , i64 ) ,
975
+ ( f32 , ( f64 , ( float , ) ) ) ) = rand:: random ( ) ;
930
976
}
931
977
}
932
978
0 commit comments