11//! The `Generator` trait and implementation helpers
22//!
3- //! The [`Generator`] trait exists to assist in the implementation of RNGs
4- //! which generate a block of data in a cache instead of returning generated
5- //! values directly.
6- //!
7- //! Usage of this trait is optional, but provides two advantages:
8- //! implementations only need to concern themselves with generation of the
9- //! block, not the various [`RngCore`] methods (especially [`fill_bytes`], where
10- //! the optimal implementations are not trivial), and this allows
11- //! `ReseedingRng` (see [`rand`](https://docs.rs/rand) crate) perform periodic
12- //! reseeding with very low overhead.
3+ //! The [`Generator`] trait and [`BlockRng`] exist to assist in the
4+ //! implementation of [`RngCore`] for block-generators over an output buffer.
135//!
146//! # Example
157//!
6052//! println!("First value: {}", rng.next_u32());
6153//! ```
6254//!
55+ //! # ReseedingRng
56+ //!
57+ //! The [`Generator`] trait supports usage of [`rand::rngs::ReseedingRng`].
58+ //! This requires that [`SeedableRng`] be implemented on the "core" generator.
59+ //! (This is in addition to any implementations on an [`RngCore`] type.)
60+ //!
6361//! [`Generator`]: crate::block::Generator
64- //! [`fill_bytes`]: RngCore::fill_bytes
62+ //! [`RngCore`]: crate::RngCore
63+ //! [`SeedableRng`]: crate::SeedableRng
64+ //! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html
6565
6666use crate :: le:: { Observable , fill_via_chunks} ;
6767use core:: fmt;
@@ -88,39 +88,20 @@ pub trait Generator {
8888/// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be
8989/// used in production.
9090///
91- /// See [`CryptoRng`] docs for more information.
91+ /// See [`CryptoRng`](crate::CryptoRng) docs for more information.
9292pub trait CryptoGenerator : Generator { }
9393
94- /// A wrapper type implementing [`RngCore`] for some type implementing
95- /// [`Generator`] with `u32` array buffer; i.e. this can be used to implement
96- /// a full RNG from just a `generate` function.
97- ///
98- /// The `core` field may be accessed directly but the results buffer may not.
99- /// PRNG implementations can simply use a type alias
100- /// (`pub type MyRng = BlockRng<MyRngCore>;`) but might prefer to use a
101- /// wrapper type (`pub struct MyRng(BlockRng<MyRngCore>);`); the latter must
102- /// re-implement `RngCore` but hides the implementation details and allows
103- /// extra functionality to be defined on the RNG
104- /// (e.g. `impl MyRng { fn set_stream(...){...} }`).
105- ///
106- /// `BlockRng` has heavily optimized implementations of the [`RngCore`] methods
107- /// reading values from the results buffer, as well as
108- /// calling [`Generator::generate`] directly on the output array when
109- /// [`fill_bytes`] is called on a large array. These methods also handle
110- /// the bookkeeping of when to generate a new batch of values.
94+ /// RNG functionality for a block [`Generator`]
11195///
112- /// No whole generated `u32` values are thrown away and all values are consumed
113- /// in-order. [`next_u32`] simply takes the next available `u32` value.
114- /// [`next_u64`] is implemented by combining two `u32` values, least
115- /// significant first. [`fill_bytes`] consume a whole number of `u32` values,
116- /// converting each `u32` to a byte slice in little-endian order. If the requested byte
117- /// length is not a multiple of 4, some bytes will be discarded.
96+ /// This type encompasses a [`Generator`] [`core`](Self::core) and a buffer.
97+ /// It provides optimized implementations of methods required by an [`RngCore`].
11898///
119- /// For easy initialization `BlockRng` also implements [`SeedableRng`].
99+ /// All values are consumed in-order of generation. No whole words (e.g. `u32`
100+ /// or `u64`) are discarded, though where a word is partially used (e.g. for a
101+ /// byte-fill whose length is not a multiple of the word size) the rest of the
102+ /// word is discarded.
120103///
121- /// [`next_u32`]: RngCore::next_u32
122- /// [`next_u64`]: RngCore::next_u64
123- /// [`fill_bytes`]: RngCore::fill_bytes
104+ /// [`RngCore`]: crate::RngCore
124105#[ derive( Clone ) ]
125106pub struct BlockRng < G : Generator > {
126107 results : G :: Output ,
0 commit comments