Skip to content

Commit 935a3e3

Browse files
authored
Remove rng parameter of ReseedingRng::new (#1533)
The `rng` parameter seems redundant in `ReseedingRng::new` (aside from the type specification).
1 parent 3f984dd commit 935a3e3

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
3232
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
3333
- Rename `Standard` to `StandardUniform` (#1526)
3434
- Remove impl of `Distribution<Option<T>>` for `Standard` (#1526)
35+
- Remove first parameter (`rng`) of `ReseedingRng::new` (#1533)
3536

3637
## [0.9.0-alpha.1] - 2024-03-18
3738
- Add the `Slice::num_choices` method to the Slice distribution (#1402)

benches/benches/generators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub fn reseeding_bytes(c: &mut Criterion) {
201201
fn bench(g: &mut BenchmarkGroup<WallTime>, thresh: u64) {
202202
let name = format!("chacha20_{}k", thresh);
203203
g.bench_function(name.as_str(), |b| {
204-
let mut rng = ReseedingRng::new(ChaCha20Core::from_os_rng(), thresh * 1024, OsRng);
204+
let mut rng = ReseedingRng::<ChaCha20Core, _>::new(thresh * 1024, OsRng).unwrap();
205205
let mut buf = [0u8; 1024 * 1024];
206206
b.iter(|| {
207207
rng.fill_bytes(&mut buf);

src/rngs/reseeding.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
5959
/// use rand::rngs::OsRng;
6060
/// use rand::rngs::ReseedingRng;
6161
///
62-
/// let prng = ChaCha20Core::from_os_rng();
63-
/// let mut reseeding_rng = ReseedingRng::new(prng, 0, OsRng);
62+
/// let mut reseeding_rng = ReseedingRng::<ChaCha20Core, _>::new(0, OsRng).unwrap();
6463
///
6564
/// println!("{}", reseeding_rng.random::<u64>());
6665
///
@@ -88,8 +87,10 @@ where
8887
/// `threshold` sets the number of generated bytes after which to reseed the
8988
/// PRNG. Set it to zero to never reseed based on the number of generated
9089
/// values.
91-
pub fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self {
92-
ReseedingRng(BlockRng::new(ReseedingCore::new(rng, threshold, reseeder)))
90+
pub fn new(threshold: u64, reseeder: Rsdr) -> Result<Self, Rsdr::Error> {
91+
Ok(ReseedingRng(BlockRng::new(ReseedingCore::new(
92+
threshold, reseeder,
93+
)?)))
9394
}
9495

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

193-
ReseedingCore {
194-
inner: rng,
197+
let inner = R::try_from_rng(&mut reseeder)?;
198+
199+
Ok(ReseedingCore {
200+
inner,
195201
reseeder,
196202
threshold,
197203
bytes_until_reseed: threshold,
198-
}
204+
})
199205
}
200206

201207
/// Reseed the internal PRNG.
@@ -249,16 +255,15 @@ where
249255
mod test {
250256
use crate::rngs::mock::StepRng;
251257
use crate::rngs::std::Core;
252-
use crate::{Rng, SeedableRng};
258+
use crate::Rng;
253259

254260
use super::ReseedingRng;
255261

256262
#[test]
257263
fn test_reseeding() {
258-
let mut zero = StepRng::new(0, 0);
259-
let rng = Core::from_rng(&mut zero);
264+
let zero = StepRng::new(0, 0);
260265
let thresh = 1; // reseed every time the buffer is exhausted
261-
let mut reseeding = ReseedingRng::new(rng, thresh, zero);
266+
let mut reseeding = ReseedingRng::<Core, _>::new(thresh, zero).unwrap();
262267

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

283287
let first: u32 = rng1.random();
284288
for _ in 0..10 {

src/rngs/thread.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::fmt;
1313
use std::rc::Rc;
1414
use std::thread_local;
1515

16-
use rand_core::{CryptoRng, RngCore, SeedableRng};
16+
use rand_core::{CryptoRng, RngCore};
1717

1818
use super::std::Core;
1919
use crate::rngs::OsRng;
@@ -119,11 +119,9 @@ thread_local!(
119119
// We require Rc<..> to avoid premature freeing when ThreadRng is used
120120
// within thread-local destructors. See #968.
121121
static THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>> = {
122-
let r = Core::try_from_os_rng().unwrap_or_else(|err|
122+
let rng = ReseedingRng::new(THREAD_RNG_RESEED_THRESHOLD,
123+
OsRng).unwrap_or_else(|err|
123124
panic!("could not initialize ThreadRng: {}", err));
124-
let rng = ReseedingRng::new(r,
125-
THREAD_RNG_RESEED_THRESHOLD,
126-
OsRng);
127125
Rc::new(UnsafeCell::new(rng))
128126
}
129127
);

0 commit comments

Comments
 (0)