Skip to content

Implement Clone for PRNGs #20483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/librand/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -283,5 +281,15 @@ mod test {
0x11cfa18e, 0xd3c50049, 0x75c775f6, 0x434c6530,
0x2c5bad8f, 0x898881dc, 0x5f1c86d9, 0xc1f8e7f4));
}

#[test]
fn test_rng_clone() {
let seed : &[_] = &[0u32, ..8];
let mut rng: ChaChaRng = SeedableRng::from_seed(seed);
let mut clone = rng.clone();
for _ in range(0u, 16) {
assert_eq!(rng.next_u64(), clone.next_u64());
}
}
}

25 changes: 25 additions & 0 deletions src/librand/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -485,6 +499,7 @@ impl Rand for Isaac64Rng {
}
}


#[cfg(test)]
mod test {
use std::prelude::v1::*;
Expand Down Expand Up @@ -594,4 +609,14 @@ mod test {
596345674630742204, 9947027391921273664, 11788097613744130851,
10391409374914919106));
}

#[test]
fn test_rng_clone() {
let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng: Isaac64Rng = SeedableRng::from_seed(seed);
let mut clone = rng.clone();
for _ in range(0u, 16) {
assert_eq!(rng.next_u64(), clone.next_u64());
}
}
}
13 changes: 2 additions & 11 deletions src/librand/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,24 +385,14 @@ 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,
z: u32,
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.
///
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/libstd/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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>>,
}
Expand Down