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//!
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) {
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
4747use crate :: le:: fill_via_chunks;
@@ -50,12 +50,8 @@ use core::fmt;
5050#[ cfg( feature = "serde" ) ]
5151use serde:: { Deserialize , Serialize } ;
5252
53- /// A trait for RNGs which do not generate random numbers individually, but in
54- /// blocks (typically `[u32; N]`). This technique is commonly used by
55- /// cryptographic RNGs to improve performance.
56- ///
57- /// See the [module][crate::block] documentation for details.
58- pub trait BlockRngCore {
53+ /// A random (block) generator
54+ pub trait Generator {
5955 /// Results type. This is the 'block' an RNG implementing `BlockRngCore`
6056 /// generates, which will usually be an array like `[u32; 16]`.
6157 type Results ;
@@ -64,14 +60,20 @@ pub trait BlockRngCore {
6460 fn generate ( & mut self , results : & mut Self :: Results ) ;
6561}
6662
67- /// A marker trait used to indicate that an [`RngCore`] implementation is
68- /// supposed to be cryptographically secure.
63+ /// A cryptographically secure generator
64+ ///
65+ /// This is a marker trait used to indicate that a [`Generator`] implementation
66+ /// is supposed to be cryptographically secure.
67+ ///
68+ /// Mock generators should not implement this trait *except* under a
69+ /// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be
70+ /// used in production.
6971///
7072/// See [`CryptoRng`] docs for more information.
71- pub trait CryptoBlockRng : BlockRngCore { }
73+ pub trait CryptoGenerator : Generator { }
7274
7375/// A wrapper type implementing [`RngCore`] for some type implementing
74- /// [`BlockRngCore `] with `u32` array buffer; i.e. this can be used to implement
76+ /// [`Generator `] with `u32` array buffer; i.e. this can be used to implement
7577/// a full RNG from just a `generate` function.
7678///
7779/// The `core` field may be accessed directly but the results buffer may not.
@@ -84,7 +86,7 @@ pub trait CryptoBlockRng: BlockRngCore {}
8486///
8587/// `BlockRng` has heavily optimized implementations of the [`RngCore`] methods
8688/// reading values from the results buffer, as well as
87- /// calling [`BlockRngCore ::generate`] directly on the output array when
89+ /// calling [`Generator ::generate`] directly on the output array when
8890/// [`fill_bytes`] is called on a large array. These methods also handle
8991/// the bookkeeping of when to generate a new batch of values.
9092///
@@ -111,15 +113,15 @@ pub trait CryptoBlockRng: BlockRngCore {}
111113 bound = "for<'x> R: Serialize + Deserialize<'x>, for<'x> R::Results: Serialize + Deserialize<'x>"
112114 )
113115) ]
114- pub struct BlockRng < R : BlockRngCore > {
116+ pub struct BlockRng < R : Generator > {
115117 results : R :: Results ,
116118 index : usize ,
117119 /// The *core* part of the RNG, implementing the `generate` function.
118120 pub core : R ,
119121}
120122
121123// Custom Debug implementation that does not expose the contents of `results`.
122- impl < const N : usize , R : BlockRngCore < Results = [ u32 ; N ] > + fmt:: Debug > fmt:: Debug for BlockRng < R > {
124+ impl < const N : usize , R : Generator < Results = [ u32 ; N ] > + fmt:: Debug > fmt:: Debug for BlockRng < R > {
123125 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
124126 fmt. debug_struct ( "BlockRng" )
125127 . field ( "core" , & self . core )
@@ -129,9 +131,9 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + fmt::Debug> fmt::Debu
129131 }
130132}
131133
132- impl < const N : usize , R : BlockRngCore < Results = [ u32 ; N ] > > BlockRng < R > {
134+ impl < const N : usize , R : Generator < Results = [ u32 ; N ] > > BlockRng < R > {
133135 /// Create a new `BlockRng` from an existing RNG implementing
134- /// `BlockRngCore `. Results will be generated on first use.
136+ /// `Generator `. Results will be generated on first use.
135137 #[ inline]
136138 pub fn new ( core : R ) -> BlockRng < R > {
137139 BlockRng {
@@ -168,7 +170,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> BlockRng<R> {
168170 }
169171}
170172
171- impl < const N : usize , R : BlockRngCore < Results = [ u32 ; N ] > > RngCore for BlockRng < R > {
173+ impl < const N : usize , R : Generator < Results = [ u32 ; N ] > > RngCore for BlockRng < R > {
172174 #[ inline]
173175 fn next_u32 ( & mut self ) -> u32 {
174176 if self . index >= self . results . as_ref ( ) . len ( ) {
@@ -221,9 +223,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> RngCore for BlockRng<R
221223 }
222224}
223225
224- impl < const N : usize , R : BlockRngCore < Results = [ u32 ; N ] > + SeedableRng > SeedableRng
225- for BlockRng < R >
226- {
226+ impl < const N : usize , R : Generator < Results = [ u32 ; N ] > + SeedableRng > SeedableRng for BlockRng < R > {
227227 type Seed = R :: Seed ;
228228
229229 #[ inline( always) ]
@@ -247,10 +247,10 @@ impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + SeedableRng> Seedable
247247 }
248248}
249249
250- impl < const N : usize , R : CryptoBlockRng < Results = [ u32 ; N ] > > CryptoRng for BlockRng < R > { }
250+ impl < const N : usize , R : CryptoGenerator < Results = [ u32 ; N ] > > CryptoRng for BlockRng < R > { }
251251
252252/// A wrapper type implementing [`RngCore`] for some type implementing
253- /// [`BlockRngCore `] with `u64` array buffer; i.e. this can be used to implement
253+ /// [`Generator `] with `u64` array buffer; i.e. this can be used to implement
254254/// a full RNG from just a `generate` function.
255255///
256256/// This is similar to [`BlockRng`], but specialized for algorithms that operate
@@ -271,7 +271,7 @@ impl<const N: usize, R: CryptoBlockRng<Results = [u32; N]>> CryptoRng for BlockR
271271/// [`fill_bytes`]: RngCore::fill_bytes
272272#[ derive( Clone ) ]
273273#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
274- pub struct BlockRng64 < R : BlockRngCore + ?Sized > {
274+ pub struct BlockRng64 < R : Generator + ?Sized > {
275275 results : R :: Results ,
276276 index : usize ,
277277 half_used : bool , // true if only half of the previous result is used
@@ -280,9 +280,7 @@ pub struct BlockRng64<R: BlockRngCore + ?Sized> {
280280}
281281
282282// Custom Debug implementation that does not expose the contents of `results`.
283- impl < const N : usize , R : BlockRngCore < Results = [ u64 ; N ] > + fmt:: Debug > fmt:: Debug
284- for BlockRng64 < R >
285- {
283+ impl < const N : usize , R : Generator < Results = [ u64 ; N ] > + fmt:: Debug > fmt:: Debug for BlockRng64 < R > {
286284 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
287285 fmt. debug_struct ( "BlockRng64" )
288286 . field ( "core" , & self . core )
@@ -293,9 +291,9 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + fmt::Debug> fmt::Debu
293291 }
294292}
295293
296- impl < const N : usize , R : BlockRngCore < Results = [ u64 ; N ] > > BlockRng64 < R > {
294+ impl < const N : usize , R : Generator < Results = [ u64 ; N ] > > BlockRng64 < R > {
297295 /// Create a new `BlockRng` from an existing RNG implementing
298- /// `BlockRngCore `. Results will be generated on first use.
296+ /// `Generator `. Results will be generated on first use.
299297 #[ inline]
300298 pub fn new ( core : R ) -> BlockRng64 < R > {
301299 let results_empty = [ 0 ; N ] ;
@@ -336,7 +334,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> BlockRng64<R> {
336334 }
337335}
338336
339- impl < const N : usize , R : BlockRngCore < Results = [ u64 ; N ] > > RngCore for BlockRng64 < R > {
337+ impl < const N : usize , R : Generator < Results = [ u64 ; N ] > > RngCore for BlockRng64 < R > {
340338 #[ inline]
341339 fn next_u32 ( & mut self ) -> u32 {
342340 let mut index = self . index - self . half_used as usize ;
@@ -388,9 +386,7 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> RngCore for BlockRng64
388386 }
389387}
390388
391- impl < const N : usize , R : BlockRngCore < Results = [ u64 ; N ] > + SeedableRng > SeedableRng
392- for BlockRng64 < R >
393- {
389+ impl < const N : usize , R : Generator < Results = [ u64 ; N ] > + SeedableRng > SeedableRng for BlockRng64 < R > {
394390 type Seed = R :: Seed ;
395391
396392 #[ inline( always) ]
@@ -414,19 +410,19 @@ impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + SeedableRng> Seedable
414410 }
415411}
416412
417- impl < const N : usize , R : CryptoBlockRng < Results = [ u64 ; N ] > > CryptoRng for BlockRng64 < R > { }
413+ impl < const N : usize , R : CryptoGenerator < Results = [ u64 ; N ] > > CryptoRng for BlockRng64 < R > { }
418414
419415#[ cfg( test) ]
420416mod test {
421- use crate :: block:: { BlockRng , BlockRng64 , BlockRngCore } ;
417+ use crate :: block:: { BlockRng , BlockRng64 , Generator } ;
422418 use crate :: { RngCore , SeedableRng } ;
423419
424420 #[ derive( Debug , Clone ) ]
425421 struct DummyRng {
426422 counter : u32 ,
427423 }
428424
429- impl BlockRngCore for DummyRng {
425+ impl Generator for DummyRng {
430426 type Results = [ u32 ; 16 ] ;
431427
432428 fn generate ( & mut self , results : & mut Self :: Results ) {
@@ -476,7 +472,7 @@ mod test {
476472 counter : u64 ,
477473 }
478474
479- impl BlockRngCore for DummyRng64 {
475+ impl Generator for DummyRng64 {
480476 type Results = [ u64 ; 8 ] ;
481477
482478 fn generate ( & mut self , results : & mut Self :: Results ) {
0 commit comments