Skip to content

Commit

Permalink
Rename NewSeeded to NewRng
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jan 20, 2018
1 parent 3d4e686 commit 2f42698
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 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, NewSeeded};
use rand::{StdRng, Rng, NewRng};

#[bench]
fn rand_f32(b: &mut Bencher) {
Expand Down
23 changes: 3 additions & 20 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,10 @@ const BYTES_LEN: usize = 1024;
use std::mem::size_of;
use test::{black_box, Bencher};

use rand::{Rng, NewSeeded, StdRng, OsRng, JitterRng};
use rand::{Rng, NewRng, StdRng, OsRng, JitterRng};
use rand::{XorShiftRng, Hc128Rng, IsaacRng, Isaac64Rng, ChaChaRng};

macro_rules! gen_bytes {
($fnn:ident, $gen:ident) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng: $gen = OsRng::new().unwrap().gen();
let mut buf = [0u8; BYTES_LEN];
b.iter(|| {
for _ in 0..RAND_BENCH_N {
rng.fill_bytes(&mut buf);
black_box(buf);
}
});
b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
}
}
}

macro_rules! gen_bytes_new {
($fnn:ident, $gen:ident) => {
#[bench]
fn $fnn(b: &mut Bencher) {
Expand All @@ -51,8 +34,8 @@ gen_bytes!(gen_bytes_hc128, Hc128Rng);
gen_bytes!(gen_bytes_isaac, IsaacRng);
gen_bytes!(gen_bytes_isaac64, Isaac64Rng);
gen_bytes!(gen_bytes_chacha, ChaChaRng);
gen_bytes_new!(gen_bytes_std, StdRng);
gen_bytes_new!(gen_bytes_os, OsRng);
gen_bytes!(gen_bytes_std, StdRng);
gen_bytes!(gen_bytes_os, OsRng);


macro_rules! gen_uint {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ pub trait SeedableRng: Sized {

/// Create a new PRNG seeded from another `Rng`.
///
/// This is the recommended way to initialize PRNGs. See the `NewSeeded`
/// This is the recommended way to initialize PRNGs. See the `NewRng`
/// trait that provides a convenient `new` method using `from_rng` and a
/// good entropy source.
///
Expand Down Expand Up @@ -805,13 +805,13 @@ pub trait SeedableRng: Sized {
/// ## Example
///
/// ```
/// use rand::{StdRng, Rng, NewSeeded};
/// use rand::{StdRng, Rng, NewRng};
///
/// let mut rng = StdRng::new().unwrap();
/// println!("Random die roll: {}", rng.gen_range(1, 7));
/// ```
#[cfg(feature="std")]
pub trait NewSeeded: SeedableRng {
pub trait NewRng: SeedableRng {
/// Creates a new instance, automatically seeded with fresh entropy.
///
/// Normally this will use `OsRng`, but if that fails `JitterRng` will be
Expand All @@ -821,7 +821,7 @@ pub trait NewSeeded: SeedableRng {
}

#[cfg(feature="std")]
impl<R: SeedableRng> NewSeeded for R {
impl<R: SeedableRng> NewRng for R {
fn new() -> Result<Self, Error> {
// Note: error handling would be easier with try/catch blocks
fn new_os<T: SeedableRng>() -> Result<T, Error> {
Expand Down
6 changes: 3 additions & 3 deletions src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use {Rng, Error};
#[cfg(feature="std")]
use NewSeeded;
use NewRng;

/// A wrapper around any RNG which reseeds the underlying RNG after it
/// has generated a certain number of random bytes.
Expand Down Expand Up @@ -93,13 +93,13 @@ pub trait Reseeder<R: ?Sized> {
fn reseed(&mut self, rng: &mut R) -> Result<(), Error>;
}

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

#[cfg(feature="std")]
impl<R: Rng + NewSeeded> Reseeder<R> for ReseedWithNew {
impl<R: Rng + NewRng> Reseeder<R> for ReseedWithNew {
fn reseed(&mut self, rng: &mut R) -> Result<(), Error> {
R::new().map(|result| *rng = result)
}
Expand Down

0 comments on commit 2f42698

Please sign in to comment.