Skip to content

Make PRNGs implement Clone instead of Copy #20199

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 13 additions & 4 deletions src/librand/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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)

#[deriving(Copy)]
#[allow(missing_copy_implementations)]
pub struct ChaChaRng {
buffer: [u32, ..STATE_WORDS], // Internal buffer of output
state: [u32, ..STATE_WORDS], // Initial state
Expand Down Expand Up @@ -85,7 +85,7 @@ impl ChaChaRng {
/// Create an ChaCha random number generator using the default
/// fixed key of 8 zero words.
pub fn new_unseeded() -> ChaChaRng {
let mut rng = EMPTY;
let mut rng = EMPTY.clone();
rng.init(&[0, ..KEY_WORDS]);
rng
}
Expand Down Expand Up @@ -157,6 +157,16 @@ impl ChaChaRng {
}
}

impl Clone for ChaChaRng {
fn clone(&self) -> ChaChaRng {
ChaChaRng {
buffer: self.buffer,
state: self.state,
index: self.index
}
}
}

impl Rng for ChaChaRng {
#[inline]
fn next_u32(&mut self) -> u32 {
Expand Down Expand Up @@ -187,7 +197,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
/// Only up to 8 words are used; if less than 8
/// words are used, the remaining are set to zero.
fn from_seed(seed: &'a [u32]) -> ChaChaRng {
let mut rng = EMPTY;
let mut rng = EMPTY.clone();
rng.reseed(seed);
rng
}
Expand All @@ -203,7 +213,6 @@ impl Rand for ChaChaRng {
}
}


#[cfg(test)]
mod test {
use std::prelude::*;
Expand Down
43 changes: 35 additions & 8 deletions src/librand/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
///
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
#[deriving(Copy)]

#[allow(missing_copy_implementations)]
pub struct IsaacRng {
cnt: u32,
rsl: [u32, ..RAND_SIZE_UINT],
Expand All @@ -51,7 +52,7 @@ impl IsaacRng {
/// Create an ISAAC random number generator using the default
/// fixed seed.
pub fn new_unseeded() -> IsaacRng {
let mut rng = EMPTY;
let mut rng = EMPTY.clone();
rng.init(false);
rng
}
Expand Down Expand Up @@ -179,6 +180,19 @@ impl IsaacRng {
}
}

impl Clone for IsaacRng {
fn clone(&self) -> IsaacRng {
IsaacRng {
cnt: self.cnt,
rsl: self.rsl,
mem: self.mem,
a: self.a,
b: self.b,
c: self.c
}
}
}

impl Rng for IsaacRng {
#[inline]
fn next_u32(&mut self) -> u32 {
Expand Down Expand Up @@ -226,15 +240,15 @@ impl<'a> SeedableRng<&'a [u32]> for IsaacRng {
/// constructed with a given seed will generate the same sequence
/// of values as all other generators constructed with that seed.
fn from_seed(seed: &'a [u32]) -> IsaacRng {
let mut rng = EMPTY;
let mut rng = EMPTY.clone();
rng.reseed(seed);
rng
}
}

impl Rand for IsaacRng {
fn rand<R: Rng>(other: &mut R) -> IsaacRng {
let mut ret = EMPTY;
let mut ret = EMPTY.clone();
unsafe {
let ptr = ret.rsl.as_mut_ptr() as *mut u8;

Expand Down Expand Up @@ -264,7 +278,7 @@ const RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
///
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
#[deriving(Copy)]
#[allow(missing_copy_implementations)]
pub struct Isaac64Rng {
cnt: uint,
rsl: [u64, .. RAND_SIZE_64],
Expand All @@ -285,7 +299,7 @@ impl Isaac64Rng {
/// Create a 64-bit ISAAC random number generator using the
/// default fixed seed.
pub fn new_unseeded() -> Isaac64Rng {
let mut rng = EMPTY_64;
let mut rng = EMPTY_64.clone();
rng.init(false);
rng
}
Expand Down Expand Up @@ -415,6 +429,19 @@ impl Isaac64Rng {
}
}

impl Clone for Isaac64Rng {
fn clone(&self) -> Isaac64Rng {
Isaac64Rng {
cnt: self.cnt,
rsl: self.rsl,
mem: self.mem,
a: self.a,
b: self.b,
c: self.c
}
}
}

impl Rng for Isaac64Rng {
// FIXME #7771: having next_u32 like this should be unnecessary
#[inline]
Expand Down Expand Up @@ -460,15 +487,15 @@ impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng {
/// constructed with a given seed will generate the same sequence
/// of values as all other generators constructed with that seed.
fn from_seed(seed: &'a [u64]) -> Isaac64Rng {
let mut rng = EMPTY_64;
let mut rng = EMPTY_64.clone();
rng.reseed(seed);
rng
}
}

impl Rand for Isaac64Rng {
fn rand<R: Rng>(other: &mut R) -> Isaac64Rng {
let mut ret = EMPTY_64;
let mut ret = EMPTY_64.clone();
unsafe {
let ptr = ret.rsl.as_mut_ptr() as *mut u8;

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,8 @@ pub mod reader;

/// The standard RNG. This is designed to be efficient on the current
/// platform.
#[deriving(Copy)]
#[allow(missing_copy_implementations)]
#[deriving(Clone)]
pub struct StdRng {
rng: IsaacWordRng,
}
Expand Down