Skip to content

Commit

Permalink
RngCore: remove default implementations of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Mar 14, 2018
1 parent 1e7edd4 commit 0876648
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
18 changes: 3 additions & 15 deletions rand-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ pub trait RngCore {
/// function is not implemented directly, the default implementation will
/// generate values via `next_u32` in little-endian fashion, or this
/// function can be implemented via `fill_bytes`.
///
/// Types wrapping an inner RNG must not use the default implementation,
/// since the inner RNG's implementation may produce different values.
fn next_u64(&mut self) -> u64 {
impls::next_u64_via_u32(self)
}
fn next_u64(&mut self) -> u64;

/// Fill `dest` with random data.
///
Expand All @@ -123,16 +118,11 @@ pub trait RngCore {
/// a requirement of portability for reproducible generators which implies
/// that any seedable generator must fix endianness when generating bytes.
///
/// Types wrapping an inner RNG must not use the default implementation,
/// since the inner RNG's implementation may produce different values.
///
/// This method should guarantee that `dest` is entirely filled
/// with new data, and may panic if this is impossible
/// (e.g. reading past the end of a file that is being used as the
/// source of randomness).
fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_u64(self, dest)
}
fn fill_bytes(&mut self, dest: &mut [u8]);

/// Fill `dest` entirely with random data.
///
Expand All @@ -146,9 +136,7 @@ pub trait RngCore {
/// has a default implementation simply wrapping [`fill_bytes`].
///
/// [`fill_bytes`]: trait.RngCore.html#method.fill_bytes
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;
}

/// A marker trait for an `Rng` which may be considered for use in
Expand Down
4 changes: 4 additions & 0 deletions src/jitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ impl RngCore for JitterRng {
// themselves via `fill_bytes`.
impls::fill_bytes_via_u32(self, dest)
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
}

impl CryptoRng for JitterRng {}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ mod test {
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.inner.fill_bytes(dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.inner.try_fill_bytes(dest)
}
}

pub fn rng(seed: u64) -> TestRng<StdRng> {
Expand Down
7 changes: 5 additions & 2 deletions src/prng/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! The ChaCha random number generator.

use core::fmt;
use rand_core::{RngCore, CryptoRng, SeedableRng, impls, le};
use rand_core::{RngCore, CryptoRng, SeedableRng, Error, impls, le};

const SEED_WORDS: usize = 8; // 8 words for the 256-bit key
const STATE_WORDS: usize = 16;
Expand Down Expand Up @@ -234,7 +234,6 @@ impl RngCore for ChaChaRng {
impls::next_u64_via_u32(self)
}


fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
while read_len < dest.len() {
Expand All @@ -250,6 +249,10 @@ impl RngCore for ChaChaRng {
read_len += filled_u8;
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
}

impl CryptoRng for ChaChaRng {}
Expand Down
6 changes: 5 additions & 1 deletion src/prng/hc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! The HC-128 random number generator.

use core::fmt;
use rand_core::{RngCore, CryptoRng, SeedableRng, impls, le};
use rand_core::{RngCore, CryptoRng, SeedableRng, Error, impls, le};

const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv

Expand Down Expand Up @@ -391,6 +391,10 @@ impl RngCore for Hc128Rng {
read_len += filled_u8;
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
}

impl CryptoRng for Hc128Rng {}
Expand Down
5 changes: 4 additions & 1 deletion src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ impl RngCore for IsaacRng {
impls::next_u64_via_u32(self)
}


fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
while read_len < dest.len() {
Expand All @@ -249,6 +248,10 @@ impl RngCore for IsaacRng {
read_len += filled_u8;
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
}

/// Creates a new ISAAC random number generator.
Expand Down
4 changes: 4 additions & 0 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ impl RngCore for Isaac64Rng {
read_len += filled_u8;
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
}

/// Creates a new ISAAC-64 random number generator.
Expand Down
4 changes: 4 additions & 0 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl RngCore for XorShiftRng {
fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_u32(self, dest)
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
}

impl SeedableRng for XorShiftRng {
Expand Down

0 comments on commit 0876648

Please sign in to comment.