diff --git a/src/mock.rs b/src/mock.rs index a3dc7229ac8..e070818a2ef 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -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 { +#[derive(Debug, Clone)] +pub struct MockAddRng { v: w, a: w, } -impl MockAddRng { +impl MockAddRng { /// Create a `MockAddRng`, yielding an arithmetic sequence starting with /// `v` and incremented by `a` each time. pub fn new(v: T, a: T) -> Self { @@ -90,7 +90,7 @@ impl Rng for MockAddRng { } } -impl SeedableRng for MockAddRng where +impl SeedableRng for MockAddRng where MockAddRng: Rng, T: From, // for 1.into() { diff --git a/src/reseeding.rs b/src/reseeding.rs index 28f6fd37d5a..8d217462f15 100644 --- a/src/reseeding.rs +++ b/src/reseeding.rs @@ -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; @@ -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 { +/// +/// 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> { rng: R, generation_threshold: u64, bytes_generated: u64, @@ -109,14 +111,18 @@ impl, Rsdr: Reseeder> SeedableRng<(Rsdr, S)> for } /// Something that can be used to reseed an RNG via `ReseedingRng`. -pub trait Reseeder: 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 { /// 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")] @@ -138,7 +144,7 @@ mod test { use distributions::ascii_word_char; use super::{ReseedingRng, Reseeder}; - #[derive(Debug)] + #[derive(Debug, Clone)] struct ReseedMock; impl Reseeder> for ReseedMock { fn reseed(&mut self, rng: &mut MockAddRng) {