]> git.lizzy.rs Git - rust.git/commitdiff
Implement Clone for PRNGs
authorSimonas Kazlauskas <git@kazlauskas.me>
Tue, 23 Dec 2014 11:55:12 +0000 (13:55 +0200)
committerSimonas Kazlauskas <git@kazlauskas.me>
Mon, 5 Jan 2015 11:10:27 +0000 (13:10 +0200)
src/librand/chacha.rs
src/librand/isaac.rs
src/librand/lib.rs
src/libstd/rand/mod.rs

index 71ce882e98ca99c2f5d4a602f788221aad9ae53c..79d836baece9542c10845921b13c700850559f51 100644 (file)
@@ -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 @@
 ///
 /// [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
index 53ae242c5e245e1050fa85d965b04e7a2fbbdcfd..c8a8da0818db84da71bb91f27655edc4b0f043e5 100644 (file)
@@ -179,6 +179,13 @@ macro_rules! rngstepn(
     }
 }
 
+// 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 @@ macro_rules! rngstepn(
     }
 }
 
+// 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 @@ fn rand<R: Rng>(other: &mut R) -> Isaac64Rng {
     }
 }
 
+
 #[cfg(test)]
 mod test {
     use std::prelude::v1::*;
index 0f8dbc78cde3272c8ee62d2da768f1e16abeb65f..d459bb83e98b5565a1f765eef3eed2981eaf4275 100644 (file)
@@ -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 @@ fn rand<R: Rng>(rng: &mut R) -> XorShiftRng {
 #[cfg(not(test))]
 mod std {
     pub use core::{option, fmt}; // panic!()
+    pub use core::clone; // derive Clone
     pub use core::kinds;
 }
 
index aa28c8266d193ff059562f9c7509b933ee454c92..cadaae5de5c206a4e0b60d0e7a0e23df70aa87ca 100644 (file)
 
 /// 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 @@ fn reseed(&mut self, rng: &mut StdRng) {
 type ThreadRngInner = reseeding::ReseedingRng<StdRng, ThreadRngReseeder>;
 
 /// The thread-local RNG.
+#[deriving(Clone)]
 pub struct ThreadRng {
     rng: Rc<RefCell<ThreadRngInner>>,
 }