Skip to content

Commit

Permalink
Fix Debug/Clone support of mock and reseeding RNGs as per #13
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Nov 9, 2017
1 parent 908fda4 commit 6302f53
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ use rand_core::impls;
/// assert_eq!(my_rng.next_u32(), 2u32);
/// assert_eq!(my_rng.next_u64(), 3u64 + (4u64 << 32));
/// ```
#[derive(Debug)]
pub struct MockAddRng<T> {
#[derive(Debug, Clone)]
pub struct MockAddRng<T: Clone> {
v: w<T>,
a: w<T>,
}

impl<T> MockAddRng<T> {
impl<T: Clone> MockAddRng<T> {
/// Create a `MockAddRng`, yielding an arithmetic sequence starting with
/// `v` and incremented by `a` each time.
pub fn new(v: T, a: T) -> Self {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Rng for MockAddRng<u64> {
}
}

impl<T> SeedableRng<T> for MockAddRng<T> where
impl<T: Clone> SeedableRng<T> for MockAddRng<T> where
MockAddRng<T>: Rng,
T: From<u8>, // for 1.into()
{
Expand Down
20 changes: 13 additions & 7 deletions src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
//! A wrapper around another RNG that reseeds it after it
//! generates a certain number of random bytes.

use core::fmt::Debug;

use {Rng, SeedableRng, Error};
#[cfg(feature="std")]
use NewSeeded;
Expand All @@ -23,8 +21,12 @@ const DEFAULT_GENERATION_THRESHOLD: u64 = 32 * 1024;

/// A wrapper around any RNG which reseeds the underlying RNG after it
/// has generated a certain number of random bytes.
#[derive(Debug)]
pub struct ReseedingRng<R, Rsdr: Debug> {
///
/// This derives `Clone` if both the inner RNG `R` and the reseeder `Rsdr` do.
/// Note that reseeders using external entropy should deliberately not
/// implement `Clone`.
#[derive(Debug, Clone)]
pub struct ReseedingRng<R, Rsdr: Reseeder<R>> {
rng: R,
generation_threshold: u64,
bytes_generated: u64,
Expand Down Expand Up @@ -109,14 +111,18 @@ impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R>> SeedableRng<(Rsdr, S)> for
}

/// Something that can be used to reseed an RNG via `ReseedingRng`.
pub trait Reseeder<R: ?Sized>: Debug {
///
/// Note that implementations should support `Clone` only if reseeding is
/// deterministic (no external entropy source). This is so that a `ReseedingRng`
/// only supports `Clone` if fully deterministic.
pub trait Reseeder<R: ?Sized> {
/// Reseed the given RNG.
fn reseed(&mut self, rng: &mut R);
}

/// Reseed an RNG using `NewSeeded` to replace the current instance.
#[cfg(feature="std")]
#[derive(Clone, Copy, Debug)]
#[derive(Debug)]
pub struct ReseedWithNew;

#[cfg(feature="std")]
Expand All @@ -138,7 +144,7 @@ mod test {
use distributions::ascii_word_char;
use super::{ReseedingRng, Reseeder};

#[derive(Debug)]
#[derive(Debug, Clone)]
struct ReseedMock;
impl Reseeder<MockAddRng<u32>> for ReseedMock {
fn reseed(&mut self, rng: &mut MockAddRng<u32>) {
Expand Down

0 comments on commit 6302f53

Please sign in to comment.