Skip to content

Commit

Permalink
Remove rng parameter of ReseedingRng::new
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Nov 20, 2024
1 parent 3f984dd commit 5c74555
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
- Rename `Standard` to `StandardUniform` (#1526)
- Remove impl of `Distribution<Option<T>>` for `Standard` (#1526)
- Remove first parameter (`rng`) of `ReseedingRng::new` (#1533)

## [0.9.0-alpha.1] - 2024-03-18
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
Expand Down
34 changes: 19 additions & 15 deletions src/rngs/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
/// use rand::rngs::OsRng;
/// use rand::rngs::ReseedingRng;
///
/// let prng = ChaCha20Core::from_os_rng();
/// let mut reseeding_rng = ReseedingRng::new(prng, 0, OsRng);
/// let mut reseeding_rng = ReseedingRng::<ChaCha20Core, _>::new(0, OsRng).unwrap();
///
/// println!("{}", reseeding_rng.random::<u64>());
///
Expand Down Expand Up @@ -88,8 +87,10 @@ where
/// `threshold` sets the number of generated bytes after which to reseed the
/// PRNG. Set it to zero to never reseed based on the number of generated
/// values.
pub fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self {
ReseedingRng(BlockRng::new(ReseedingCore::new(rng, threshold, reseeder)))
pub fn new(threshold: u64, reseeder: Rsdr) -> Result<Self, Rsdr::Error> {
Ok(ReseedingRng(BlockRng::new(ReseedingCore::new(
threshold, reseeder,
)?)))
}

/// Immediately reseed the generator
Expand Down Expand Up @@ -177,7 +178,10 @@ where
Rsdr: TryRngCore,
{
/// Create a new `ReseedingCore`.
fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self {
///
/// `threshold` is the maximum number of bytes produced by
/// [`BlockRngCore::generate`] before attempting reseeding.
fn new(threshold: u64, mut reseeder: Rsdr) -> Result<Self, Rsdr::Error> {
// Because generating more values than `i64::MAX` takes centuries on
// current hardware, we just clamp to that value.
// Also we set a threshold of 0, which indicates no limit, to that
Expand All @@ -190,12 +194,14 @@ where
i64::MAX
};

ReseedingCore {
inner: rng,
let inner = R::try_from_rng(&mut reseeder)?;

Ok(ReseedingCore {
inner,
reseeder,
threshold,
bytes_until_reseed: threshold,
}
})
}

/// Reseed the internal PRNG.
Expand Down Expand Up @@ -249,16 +255,15 @@ where
mod test {
use crate::rngs::mock::StepRng;
use crate::rngs::std::Core;
use crate::{Rng, SeedableRng};
use crate::Rng;

use super::ReseedingRng;

#[test]
fn test_reseeding() {
let mut zero = StepRng::new(0, 0);
let rng = Core::from_rng(&mut zero);
let zero = StepRng::new(0, 0);
let thresh = 1; // reseed every time the buffer is exhausted
let mut reseeding = ReseedingRng::new(rng, thresh, zero);
let mut reseeding = ReseedingRng::<Core, _>::new(thresh, zero).unwrap();

// RNG buffer size is [u32; 64]
// Debug is only implemented up to length 32 so use two arrays
Expand All @@ -276,9 +281,8 @@ mod test {
#[test]
#[allow(clippy::redundant_clone)]
fn test_clone_reseeding() {
let mut zero = StepRng::new(0, 0);
let rng = Core::from_rng(&mut zero);
let mut rng1 = ReseedingRng::new(rng, 32 * 4, zero);
let zero = StepRng::new(0, 0);
let mut rng1 = ReseedingRng::<Core, _>::new(32 * 4, zero).unwrap();

let first: u32 = rng1.random();
for _ in 0..10 {
Expand Down
8 changes: 3 additions & 5 deletions src/rngs/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fmt;
use std::rc::Rc;
use std::thread_local;

use rand_core::{CryptoRng, RngCore, SeedableRng};
use rand_core::{CryptoRng, RngCore};

use super::std::Core;
use crate::rngs::OsRng;
Expand Down Expand Up @@ -119,11 +119,9 @@ thread_local!(
// We require Rc<..> to avoid premature freeing when ThreadRng is used
// within thread-local destructors. See #968.
static THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>> = {
let r = Core::try_from_os_rng().unwrap_or_else(|err|
let rng = ReseedingRng::new(THREAD_RNG_RESEED_THRESHOLD,
OsRng).unwrap_or_else(|err|
panic!("could not initialize ThreadRng: {}", err));
let rng = ReseedingRng::new(r,
THREAD_RNG_RESEED_THRESHOLD,
OsRng);
Rc::new(UnsafeCell::new(rng))
}
);
Expand Down

0 comments on commit 5c74555

Please sign in to comment.