From 8ad6e2ac7ac3945bbc16c5af186391d56f77426f Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 27 Dec 2017 11:58:57 +0000 Subject: [PATCH] Revert some breaking changes Restore default implementations of next_u64 and fill_bytes Restore new_unseeded functions (as wrappers) --- src/lib.rs | 20 ++++++++++++++++++-- src/prng/isaac.rs | 6 ++++++ src/prng/isaac64.rs | 6 ++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2c0a620f18b..9bbe904e00a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,7 +343,15 @@ pub trait Rng { fn next_u32(&mut self) -> u32; /// Return the next random u64. - fn next_u64(&mut self) -> u64; + /// + /// This function has a default implementation of `next_u32`. The + /// default implementation should not be used in wrapper types since the + /// wrapped RNG may have its own implementation which may be more efficient + /// or even produce different results. + fn next_u64(&mut self) -> u64 { + // TODO: remove default implementation once impls module is exposed + impls::next_u64_via_u32(self) + } /// Return the next random f32 selected from the half-open /// interval `[0, 1)`. @@ -398,6 +406,11 @@ pub trait Rng { } /// Fill `dest` with random data. + /// + /// This function has a default implementation in terms of `next_u64`. The + /// default implementation should not be used in wrapper types since the + /// wrapped RNG may have its own implementation which may be more efficient + /// or even produce different results. /// /// This method does *not* have a requirement to bear any fixed /// relationship to the other methods, for example, it does *not* @@ -419,7 +432,10 @@ pub trait Rng { /// thread_rng().fill_bytes(&mut v); /// println!("{:?}", &v[..]); /// ``` - fn fill_bytes(&mut self, dest: &mut [u8]); + fn fill_bytes(&mut self, dest: &mut [u8]) { + // TODO: remove default implementation once impls module is exposed + impls::fill_bytes_via_u64(self, dest) + } /// Return a random value of a `Rand` type. /// diff --git a/src/prng/isaac.rs b/src/prng/isaac.rs index 16b6f6b74a5..57e44ddca73 100644 --- a/src/prng/isaac.rs +++ b/src/prng/isaac.rs @@ -119,6 +119,12 @@ impl fmt::Debug for IsaacRng { } impl IsaacRng { + /// Create an ISAAC random number generator using the default + /// fixed seed. + pub fn new_unseeded() -> IsaacRng { + Self::new_from_u64(0) + } + /// Creates an ISAAC random number generator using an u64 as seed. /// If `seed == 0` this will produce the same stream of random numbers as /// the reference implementation when used unseeded. diff --git a/src/prng/isaac64.rs b/src/prng/isaac64.rs index 8cabca38a87..a8523d8248d 100644 --- a/src/prng/isaac64.rs +++ b/src/prng/isaac64.rs @@ -105,6 +105,12 @@ impl fmt::Debug for Isaac64Rng { } impl Isaac64Rng { + /// Create a 64-bit ISAAC random number generator using the + /// default fixed seed. + pub fn new_unseeded() -> Isaac64Rng { + Self::new_from_u64(0) + } + /// Creates an ISAAC-64 random number generator using an u64 as seed. /// If `seed == 0` this will produce the same stream of random numbers as /// the reference implementation when used unseeded.