From 5c74555066c2cb34daf742afc09fd081596a9ed7 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 20 Nov 2024 12:01:11 +0000 Subject: [PATCH] Remove rng parameter of ReseedingRng::new --- CHANGELOG.md | 1 + src/rngs/reseeding.rs | 34 +++++++++++++++++++--------------- src/rngs/thread.rs | 8 +++----- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44603eeb53..32919ded61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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>` 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) diff --git a/src/rngs/reseeding.rs b/src/rngs/reseeding.rs index a0076f959a..570d04eeba 100644 --- a/src/rngs/reseeding.rs +++ b/src/rngs/reseeding.rs @@ -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::::new(0, OsRng).unwrap(); /// /// println!("{}", reseeding_rng.random::()); /// @@ -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 { + Ok(ReseedingRng(BlockRng::new(ReseedingCore::new( + threshold, reseeder, + )?))) } /// Immediately reseed the generator @@ -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 { // 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 @@ -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. @@ -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::::new(thresh, zero).unwrap(); // RNG buffer size is [u32; 64] // Debug is only implemented up to length 32 so use two arrays @@ -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::::new(32 * 4, zero).unwrap(); let first: u32 = rng1.random(); for _ in 0..10 { diff --git a/src/rngs/thread.rs b/src/rngs/thread.rs index 64ca0e1f76..38446759ab 100644 --- a/src/rngs/thread.rs +++ b/src/rngs/thread.rs @@ -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; @@ -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>> = { - 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)) } );