about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-04-22 09:00:53 -0700
committerbors <bors@rust-lang.org>2013-04-22 09:00:53 -0700
commitd0451eebc41d4eaddcc299c868b5ad983e8c8eb9 (patch)
tree92a66dcec68044cdc0aa99067fea8d5da896ed78
parent0de3e7a23c0cd677a3f369ea1b65cd71f41dcda2 (diff)
parent56679024c551c51a1eea8a03dbe8570f56687521 (diff)
downloadrust-d0451eebc41d4eaddcc299c868b5ad983e8c8eb9.tar.gz
rust-d0451eebc41d4eaddcc299c868b5ad983e8c8eb9.zip
auto merge of #5995 : huonw/rust/core-rand-impls, r=pcwalton
-rw-r--r--src/libcore/rand.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs
index a2f103fdbc9..1e33f382df5 100644
--- a/src/libcore/rand.rs
+++ b/src/libcore/rand.rs
@@ -115,6 +115,40 @@ impl Rand for bool {
     }
 }
 
+macro_rules! tuple_impl {
+    // use variables to indicate the arity of the tuple
+    ($($tyvar:ident),* ) => {
+        // the trailing commas are for the 1 tuple
+        impl<
+            $( $tyvar : Rand ),*
+            > Rand for ( $( $tyvar ),* , ) {
+
+            fn rand (_rng: @Rng) -> ( $( $tyvar ),* , ) {
+                (
+                    // use the $var's to get the appropriate number of repeats
+                    // (they're not actually needed)
+                    $(
+                        _rng.gen::<$tyvar>()
+                    ),*
+                    ,
+                )
+            }
+        }
+    }
+}
+
+impl Rand for () { fn rand(_: @Rng) -> () { () } }
+tuple_impl!{A}
+tuple_impl!{A, B}
+tuple_impl!{A, B, C}
+tuple_impl!{A, B, C, D}
+tuple_impl!{A, B, C, D, E}
+tuple_impl!{A, B, C, D, E, F}
+tuple_impl!{A, B, C, D, E, F, G}
+tuple_impl!{A, B, C, D, E, F, G, H}
+tuple_impl!{A, B, C, D, E, F, G, H, I}
+tuple_impl!{A, B, C, D, E, F, G, H, I, J}
+
 impl<T:Rand> Rand for Option<T> {
     fn rand(rng: @rand::Rng) -> Option<T> {
         if rng.gen_bool() {
@@ -125,6 +159,14 @@ impl<T:Rand> Rand for Option<T> {
     }
 }
 
+impl<T: Rand> Rand for ~T {
+    fn rand(rng: @Rng) -> ~T { ~rng.gen() }
+}
+
+impl<T: Rand> Rand for @T {
+    fn rand(rng: @Rng) -> @T { @rng.gen() }
+}
+
 #[allow(non_camel_case_types)] // runtime type
 pub enum rust_rng {}
 
@@ -927,6 +969,10 @@ mod tests {
         let _n : uint = rand::random();
         let _f : f32 = rand::random();
         let _o : Option<Option<i8>> = rand::random();
+        let _many : ((),
+                     (~uint, @int, ~Option<~(@char, ~(@bool,))>),
+                     (u8, i8, u16, i16, u32, i32, u64, i64),
+                     (f32, (f64, (float,)))) = rand::random();
     }
 }