Skip to content

Commit

Permalink
Merge pull request rust-random#265 from dhardy/rng-core
Browse files Browse the repository at this point in the history
RngCore and extension trait Rng
  • Loading branch information
dhardy authored Feb 19, 2018
2 parents d3fee23 + 301105d commit 18e8e91
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 81 deletions.
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod distributions;

use std::mem::size_of;
use test::{black_box, Bencher};
use rand::{StdRng, Rng, NewRng};
use rand::{StdRng, RngCore, NewRng};

#[bench]
fn rand_f32(b: &mut Bencher) {
Expand Down
2 changes: 1 addition & 1 deletion benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BYTES_LEN: usize = 1024;
use std::mem::size_of;
use test::{black_box, Bencher};

use rand::{Rng, NewRng, StdRng, OsRng, JitterRng, EntropyRng};
use rand::{RngCore, Rng, NewRng, StdRng, OsRng, JitterRng, EntropyRng};
use rand::{XorShiftRng, Hc128Rng, IsaacRng, Isaac64Rng, ChaChaRng};
use rand::reseeding::ReseedingRng;

Expand Down
4 changes: 2 additions & 2 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ fn ziggurat<R: Rng, P, Z>(

#[cfg(test)]
mod tests {
use {Rng, Rand};
use {Rng, RngCore, Rand};
use impls;
use super::{RandSample, WeightedChoice, Weighted, Sample, IndependentSample};

Expand All @@ -293,7 +293,7 @@ mod tests {

// 0, 1, 2, 3, ...
struct CountingRng { i: u32 }
impl Rng for CountingRng {
impl RngCore for CountingRng {
fn next_u32(&mut self) -> u32 {
self.i += 1;
self.i - 1
Expand Down
14 changes: 7 additions & 7 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Helper functions for implementing `Rng` functions.
//! Helper functions for implementing `RngCore` functions.
//!
//! For cross-platform reproducibility, these functions all use Little Endian:
//! least-significant part first. For example, `next_u64_via_u32` takes `u32`
Expand All @@ -27,10 +27,10 @@ use core::ptr::copy_nonoverlapping;
use core::slice;
use core::cmp::min;
use core::mem::size_of;
use Rng;
use RngCore;

/// Implement `next_u64` via `next_u32`, little-endian order.
pub fn next_u64_via_u32<R: Rng+?Sized>(rng: &mut R) -> u64 {
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
// Use LE; we explicitly generate one value before the next.
let x = rng.next_u32() as u64;
let y = rng.next_u32() as u64;
Expand Down Expand Up @@ -59,12 +59,12 @@ macro_rules! fill_bytes_via {
}

/// Implement `fill_bytes` via `next_u32`, little-endian order.
pub fn fill_bytes_via_u32<R: Rng+?Sized>(rng: &mut R, dest: &mut [u8]) {
pub fn fill_bytes_via_u32<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
fill_bytes_via!(rng, next_u32, 4, dest)
}

/// Implement `fill_bytes` via `next_u64`, little-endian order.
pub fn fill_bytes_via_u64<R: Rng+?Sized>(rng: &mut R, dest: &mut [u8]) {
pub fn fill_bytes_via_u64<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
fill_bytes_via!(rng, next_u64, 8, dest)
}

Expand Down Expand Up @@ -158,12 +158,12 @@ pub fn fill_via_u64_chunks(src: &[u64], dest: &mut [u8]) -> (usize, usize) {
}

/// Implement `next_u32` via `fill_bytes`, little-endian order.
pub fn next_u32_via_fill<R: Rng+?Sized>(rng: &mut R) -> u32 {
pub fn next_u32_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u32 {
impl_uint_from_fill!(rng, u32, 4)
}

/// Implement `next_u64` via `fill_bytes`, little-endian order.
pub fn next_u64_via_fill<R: Rng+?Sized>(rng: &mut R) -> u64 {
pub fn next_u64_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
impl_uint_from_fill!(rng, u64, 8)
}

Expand Down
4 changes: 2 additions & 2 deletions src/jitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Non-physical true random number generator based on timing jitter.

use {Rng, Error, ErrorKind, impls};
use {RngCore, Error, ErrorKind, impls};

use core::{fmt, mem, ptr};
#[cfg(feature="std")]
Expand Down Expand Up @@ -746,7 +746,7 @@ fn black_box<T>(dummy: T) -> T {
}
}

impl Rng for JitterRng {
impl RngCore for JitterRng {
fn next_u32(&mut self) -> u32 {
// We want to use both parts of the generated entropy
if self.data_half_used {
Expand Down
Loading

0 comments on commit 18e8e91

Please sign in to comment.