about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSimonas Kazlauskas <git@kazlauskas.me>2014-12-23 13:55:12 +0200
committerSimonas Kazlauskas <git@kazlauskas.me>2015-01-05 13:10:27 +0200
commitf677deeab397c8672c74fa134978df9aa10e0ff3 (patch)
tree93011b840c8162b9b9104c3f209c84ac2e42d54c
parent5773bdefff2e47cc007f5cc2af3f80b30303d45a (diff)
downloadrust-f677deeab397c8672c74fa134978df9aa10e0ff3.tar.gz
rust-f677deeab397c8672c74fa134978df9aa10e0ff3.zip
Implement Clone for PRNGs
-rw-r--r--src/librand/chacha.rs4
-rw-r--r--src/librand/isaac.rs15
-rw-r--r--src/librand/lib.rs13
-rw-r--r--src/libstd/rand/mod.rs3
4 files changed, 20 insertions, 15 deletions
diff --git a/src/librand/chacha.rs b/src/librand/chacha.rs
index 71ce882e98c..79d836baece 100644
--- a/src/librand/chacha.rs
+++ b/src/librand/chacha.rs
@@ -12,7 +12,6 @@
 
 use core::prelude::*;
 use core::num::Int;
-
 use {Rng, SeedableRng, Rand};
 
 const KEY_WORDS    : uint =  8; // 8 words for the 256-bit key
@@ -28,8 +27,7 @@ const CHACHA_ROUNDS: uint = 20; // Cryptographically secure from 8 upwards as of
 ///
 /// [1]: D. J. Bernstein, [*ChaCha, a variant of
 /// Salsa20*](http://cr.yp.to/chacha.html)
-
-#[derive(Copy)]
+#[deriving(Copy, Clone)]
 pub struct ChaChaRng {
     buffer:  [u32; STATE_WORDS], // Internal buffer of output
     state:   [u32; STATE_WORDS], // Initial state
diff --git a/src/librand/isaac.rs b/src/librand/isaac.rs
index 53ae242c5e2..c8a8da0818d 100644
--- a/src/librand/isaac.rs
+++ b/src/librand/isaac.rs
@@ -179,6 +179,13 @@ impl IsaacRng {
     }
 }
 
+// Cannot be derived because [u32; 256] does not implement Clone
+impl Clone for IsaacRng {
+    fn clone(&self) -> IsaacRng {
+        *self
+    }
+}
+
 impl Rng for IsaacRng {
     #[inline]
     fn next_u32(&mut self) -> u32 {
@@ -415,6 +422,13 @@ impl Isaac64Rng {
     }
 }
 
+// Cannot be derived because [u32; 256] does not implement Clone
+impl Clone for Isaac64Rng {
+    fn clone(&self) -> Isaac64Rng {
+        *self
+    }
+}
+
 impl Rng for Isaac64Rng {
     // FIXME #7771: having next_u32 like this should be unnecessary
     #[inline]
@@ -485,6 +499,7 @@ impl Rand for Isaac64Rng {
     }
 }
 
+
 #[cfg(test)]
 mod test {
     use std::prelude::v1::*;
diff --git a/src/librand/lib.rs b/src/librand/lib.rs
index 0f8dbc78cde..d459bb83e98 100644
--- a/src/librand/lib.rs
+++ b/src/librand/lib.rs
@@ -385,6 +385,7 @@ pub trait SeedableRng<Seed>: Rng {
 /// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of
 /// Statistical Software*. Vol. 8 (Issue 14).
 #[allow(missing_copy_implementations)]
+#[deriving(Clone)]
 pub struct XorShiftRng {
     x: u32,
     y: u32,
@@ -392,17 +393,6 @@ pub struct XorShiftRng {
     w: u32,
 }
 
-impl Clone for XorShiftRng {
-    fn clone(&self) -> XorShiftRng {
-        XorShiftRng {
-            x: self.x,
-            y: self.y,
-            z: self.z,
-            w: self.w,
-        }
-    }
-}
-
 impl XorShiftRng {
     /// Creates a new XorShiftRng instance which is not seeded.
     ///
@@ -507,6 +497,7 @@ pub struct Closed01<F>(pub F);
 #[cfg(not(test))]
 mod std {
     pub use core::{option, fmt}; // panic!()
+    pub use core::clone; // derive Clone
     pub use core::kinds;
 }
 
diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs
index aa28c8266d1..cadaae5de5c 100644
--- a/src/libstd/rand/mod.rs
+++ b/src/libstd/rand/mod.rs
@@ -245,7 +245,7 @@ pub mod reader;
 
 /// The standard RNG. This is designed to be efficient on the current
 /// platform.
-#[derive(Copy)]
+#[deriving(Copy, Clone)]
 pub struct StdRng {
     rng: IsaacWordRng,
 }
@@ -322,6 +322,7 @@ static THREAD_RNG_RESEED_THRESHOLD: uint = 32_768;
 type ThreadRngInner = reseeding::ReseedingRng<StdRng, ThreadRngReseeder>;
 
 /// The thread-local RNG.
+#[deriving(Clone)]
 pub struct ThreadRng {
     rng: Rc<RefCell<ThreadRngInner>>,
 }