Skip to content

Commit 977f74e

Browse files
committed
Rename BlockRngCore -> Generator
1 parent e34c1f3 commit 977f74e

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/block.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! The `BlockRngCore` trait and implementation helpers
1+
//! The `Generator` trait and implementation helpers
22
//!
3-
//! The [`BlockRngCore`] trait exists to assist in the implementation of RNGs
3+
//! The [`Generator`] trait exists to assist in the implementation of RNGs
44
//! which generate a block of data in a cache instead of returning generated
55
//! values directly.
66
//!
@@ -15,11 +15,11 @@
1515
//!
1616
//! ```no_run
1717
//! use rand_core::{RngCore, SeedableRng};
18-
//! use rand_core::block::{BlockRngCore, BlockRng};
18+
//! use rand_core::block::{Generator, BlockRng};
1919
//!
2020
//! struct MyRngCore;
2121
//!
22-
//! impl BlockRngCore for MyRngCore {
22+
//! impl Generator for MyRngCore {
2323
//! type Results = [u32; 16];
2424
//!
2525
//! fn generate(&mut self, results: &mut Self::Results) {
@@ -34,26 +34,22 @@
3434
//! }
3535
//! }
3636
//!
37-
//! // optionally, also implement CryptoBlockRng for MyRngCore
37+
//! // optionally, also implement CryptoGenerator for MyRngCore
3838
//!
3939
//! // Final RNG.
4040
//! let mut rng = BlockRng::<MyRngCore>::seed_from_u64(0);
4141
//! println!("First value: {}", rng.next_u32());
4242
//! ```
4343
//!
44-
//! [`BlockRngCore`]: crate::block::BlockRngCore
44+
//! [`Generator`]: crate::block::Generator
4545
//! [`fill_bytes`]: RngCore::fill_bytes
4646
4747
use crate::le::fill_via_chunks;
4848
use crate::{CryptoRng, RngCore, SeedableRng, TryRngCore};
4949
use core::fmt;
5050

51-
/// A trait for RNGs which do not generate random numbers individually, but in
52-
/// blocks (typically `[u32; N]`). This technique is commonly used by
53-
/// cryptographic RNGs to improve performance.
54-
///
55-
/// See the [module][crate::block] documentation for details.
56-
pub trait BlockRngCore {
51+
/// A random (block) generator
52+
pub trait Generator {
5753
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
5854
/// generates, which will usually be an array like `[u32; 16]`.
5955
type Results;
@@ -62,14 +58,20 @@ pub trait BlockRngCore {
6258
fn generate(&mut self, results: &mut Self::Results);
6359
}
6460

65-
/// A marker trait used to indicate that an [`RngCore`] implementation is
66-
/// supposed to be cryptographically secure.
61+
/// A cryptographically secure generator
62+
///
63+
/// This is a marker trait used to indicate that a [`Generator`] implementation
64+
/// is supposed to be cryptographically secure.
65+
///
66+
/// Mock generators should not implement this trait *except* under a
67+
/// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be
68+
/// used in production.
6769
///
6870
/// See [`CryptoRng`] docs for more information.
69-
pub trait CryptoBlockRng: BlockRngCore {}
71+
pub trait CryptoGenerator: Generator {}
7072

7173
/// A wrapper type implementing [`RngCore`] for some type implementing
72-
/// [`BlockRngCore`] with `u32` array buffer; i.e. this can be used to implement
74+
/// [`Generator`] with `u32` array buffer; i.e. this can be used to implement
7375
/// a full RNG from just a `generate` function.
7476
///
7577
/// The `core` field may be accessed directly but the results buffer may not.
@@ -82,7 +84,7 @@ pub trait CryptoBlockRng: BlockRngCore {}
8284
///
8385
/// `BlockRng` has heavily optimized implementations of the [`RngCore`] methods
8486
/// reading values from the results buffer, as well as
85-
/// calling [`BlockRngCore::generate`] directly on the output array when
87+
/// calling [`Generator::generate`] directly on the output array when
8688
/// [`fill_bytes`] is called on a large array. These methods also handle
8789
/// the bookkeeping of when to generate a new batch of values.
8890
///
@@ -102,15 +104,15 @@ pub trait CryptoBlockRng: BlockRngCore {}
102104
/// [`next_u64`]: RngCore::next_u64
103105
/// [`fill_bytes`]: RngCore::fill_bytes
104106
#[derive(Clone)]
105-
pub struct BlockRng<R: BlockRngCore> {
107+
pub struct BlockRng<R: Generator> {
106108
results: R::Results,
107109
index: usize,
108110
/// The *core* part of the RNG, implementing the `generate` function.
109111
pub core: R,
110112
}
111113

112114
// Custom Debug implementation that does not expose the contents of `results`.
113-
impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + fmt::Debug> fmt::Debug for BlockRng<R> {
115+
impl<const N: usize, R: Generator<Results = [u32; N]> + fmt::Debug> fmt::Debug for BlockRng<R> {
114116
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
115117
fmt.debug_struct("BlockRng")
116118
.field("core", &self.core)
@@ -120,9 +122,9 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + fmt::Debug> fmt::Debu
120122
}
121123
}
122124

123-
impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> BlockRng<R> {
125+
impl<const N: usize, R: Generator<Results = [u32; N]>> BlockRng<R> {
124126
/// Create a new `BlockRng` from an existing RNG implementing
125-
/// `BlockRngCore`. Results will be generated on first use.
127+
/// `Generator`. Results will be generated on first use.
126128
#[inline]
127129
pub fn new(core: R) -> BlockRng<R> {
128130
BlockRng {
@@ -159,7 +161,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> BlockRng<R> {
159161
}
160162
}
161163

162-
impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> RngCore for BlockRng<R> {
164+
impl<const N: usize, R: Generator<Results = [u32; N]>> RngCore for BlockRng<R> {
163165
#[inline]
164166
fn next_u32(&mut self) -> u32 {
165167
if self.index >= self.results.as_ref().len() {
@@ -212,9 +214,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> RngCore for BlockRng<R
212214
}
213215
}
214216

215-
impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + SeedableRng> SeedableRng
216-
for BlockRng<R>
217-
{
217+
impl<const N: usize, R: Generator<Results = [u32; N]> + SeedableRng> SeedableRng for BlockRng<R> {
218218
type Seed = R::Seed;
219219

220220
#[inline(always)]
@@ -238,10 +238,10 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + SeedableRng> Seedable
238238
}
239239
}
240240

241-
impl<const N: usize, R: CryptoBlockRng<Results = [u32; N]>> CryptoRng for BlockRng<R> {}
241+
impl<const N: usize, R: CryptoGenerator<Results = [u32; N]>> CryptoRng for BlockRng<R> {}
242242

243243
/// A wrapper type implementing [`RngCore`] for some type implementing
244-
/// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement
244+
/// [`Generator`] with `u64` array buffer; i.e. this can be used to implement
245245
/// a full RNG from just a `generate` function.
246246
///
247247
/// This is similar to [`BlockRng`], but specialized for algorithms that operate
@@ -261,7 +261,7 @@ impl<const N: usize, R: CryptoBlockRng<Results = [u32; N]>> CryptoRng for BlockR
261261
/// [`next_u64`]: RngCore::next_u64
262262
/// [`fill_bytes`]: RngCore::fill_bytes
263263
#[derive(Clone)]
264-
pub struct BlockRng64<R: BlockRngCore + ?Sized> {
264+
pub struct BlockRng64<R: Generator + ?Sized> {
265265
results: R::Results,
266266
index: usize,
267267
half_used: bool, // true if only half of the previous result is used
@@ -270,9 +270,7 @@ pub struct BlockRng64<R: BlockRngCore + ?Sized> {
270270
}
271271

272272
// Custom Debug implementation that does not expose the contents of `results`.
273-
impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + fmt::Debug> fmt::Debug
274-
for BlockRng64<R>
275-
{
273+
impl<const N: usize, R: Generator<Results = [u64; N]> + fmt::Debug> fmt::Debug for BlockRng64<R> {
276274
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
277275
fmt.debug_struct("BlockRng64")
278276
.field("core", &self.core)
@@ -283,9 +281,9 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + fmt::Debug> fmt::Debu
283281
}
284282
}
285283

286-
impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> BlockRng64<R> {
284+
impl<const N: usize, R: Generator<Results = [u64; N]>> BlockRng64<R> {
287285
/// Create a new `BlockRng` from an existing RNG implementing
288-
/// `BlockRngCore`. Results will be generated on first use.
286+
/// `Generator`. Results will be generated on first use.
289287
#[inline]
290288
pub fn new(core: R) -> BlockRng64<R> {
291289
let results_empty = [0; N];
@@ -326,7 +324,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> BlockRng64<R> {
326324
}
327325
}
328326

329-
impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> RngCore for BlockRng64<R> {
327+
impl<const N: usize, R: Generator<Results = [u64; N]>> RngCore for BlockRng64<R> {
330328
#[inline]
331329
fn next_u32(&mut self) -> u32 {
332330
let mut index = self.index - self.half_used as usize;
@@ -378,9 +376,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> RngCore for BlockRng64
378376
}
379377
}
380378

381-
impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + SeedableRng> SeedableRng
382-
for BlockRng64<R>
383-
{
379+
impl<const N: usize, R: Generator<Results = [u64; N]> + SeedableRng> SeedableRng for BlockRng64<R> {
384380
type Seed = R::Seed;
385381

386382
#[inline(always)]
@@ -404,19 +400,19 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + SeedableRng> Seedable
404400
}
405401
}
406402

407-
impl<const N: usize, R: CryptoBlockRng<Results = [u64; N]>> CryptoRng for BlockRng64<R> {}
403+
impl<const N: usize, R: CryptoGenerator<Results = [u64; N]>> CryptoRng for BlockRng64<R> {}
408404

409405
#[cfg(test)]
410406
mod test {
411-
use crate::block::{BlockRng, BlockRng64, BlockRngCore};
407+
use crate::block::{BlockRng, BlockRng64, Generator};
412408
use crate::{RngCore, SeedableRng};
413409

414410
#[derive(Debug, Clone)]
415411
struct DummyRng {
416412
counter: u32,
417413
}
418414

419-
impl BlockRngCore for DummyRng {
415+
impl Generator for DummyRng {
420416
type Results = [u32; 16];
421417

422418
fn generate(&mut self, results: &mut Self::Results) {
@@ -466,7 +462,7 @@ mod test {
466462
counter: u64,
467463
}
468464

469-
impl BlockRngCore for DummyRng64 {
465+
impl Generator for DummyRng64 {
470466
type Results = [u64; 8];
471467

472468
fn generate(&mut self, results: &mut Self::Results) {

0 commit comments

Comments
 (0)