Skip to content

Commit dfa8a99

Browse files
committed
Move trait Generator up to the crate root
1 parent 1b1af4e commit dfa8a99

File tree

2 files changed

+55
-69
lines changed

2 files changed

+55
-69
lines changed

src/block.rs

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
//! The `Generator` trait and implementation helpers
1+
//! Support for block generators
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 [`BlockRng`] and [`BlockRng64`] types may be used to provide an
4+
//! implementation of [`RngCore`] over a block [`Generator`].
135
//!
146
//! # Example
157
//!
168
//! ```no_run
17-
//! use rand_core::{RngCore, SeedableRng};
18-
//! use rand_core::block::{Generator, BlockRng};
9+
//! use rand_core::{Generator, RngCore, SeedableRng};
10+
//! use rand_core::block::BlockRng;
1911
//!
2012
//! struct MyRngCore;
2113
//!
@@ -45,57 +37,11 @@
4537
//! [`fill_bytes`]: RngCore::fill_bytes
4638
4739
use crate::le::fill_via_chunks;
48-
use crate::{CryptoRng, RngCore, SeedableRng, TryRngCore};
40+
use crate::*;
4941
use core::fmt;
5042
#[cfg(feature = "serde")]
5143
use serde::{Deserialize, Serialize};
5244

53-
/// A random generator
54-
pub trait Generator {
55-
/// The result type.
56-
///
57-
/// This could be a simple word like `u64` or an array like `[u32; 16]`.
58-
type Result;
59-
60-
/// Generate a new result.
61-
///
62-
/// Since [`Self::Result`] may be large, the output is passed by reference.
63-
/// Word generators should likely implement this as a shim over another
64-
/// method:
65-
/// ```
66-
/// pub struct CountingGenerator(u64);
67-
/// impl CountingGenerator {
68-
/// fn next(&mut self) -> u64 {
69-
/// let x = self.0;
70-
/// self.0 = self.0.wrapping_add(1);
71-
/// x
72-
/// }
73-
/// }
74-
///
75-
/// impl rand_core::block::Generator for CountingGenerator {
76-
/// type Result = u64;
77-
///
78-
/// #[inline]
79-
/// fn generate(&mut self, result: &mut Self::Result) {
80-
/// *result = self.next();
81-
/// }
82-
/// }
83-
/// ```
84-
fn generate(&mut self, result: &mut Self::Result);
85-
}
86-
87-
/// A cryptographically secure generator
88-
///
89-
/// This is a marker trait used to indicate that a [`Generator`] implementation
90-
/// is supposed to be cryptographically secure.
91-
///
92-
/// Mock generators should not implement this trait *except* under a
93-
/// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be
94-
/// used in production.
95-
///
96-
/// See [`CryptoRng`] docs for more information.
97-
pub trait CryptoGenerator: Generator {}
98-
9945
/// A wrapper type implementing [`RngCore`] for some type implementing
10046
/// [`Generator`] with `u32` array buffer; i.e. this can be used to implement
10147
/// a full RNG from just a `generate` function.
@@ -247,9 +193,7 @@ impl<const N: usize, R: Generator<Result = [u32; N]>> RngCore for BlockRng<R> {
247193
}
248194
}
249195

250-
impl<const N: usize, R: Generator<Result = [u32; N]> + SeedableRng> SeedableRng
251-
for BlockRng<R>
252-
{
196+
impl<const N: usize, R: Generator<Result = [u32; N]> + SeedableRng> SeedableRng for BlockRng<R> {
253197
type Seed = R::Seed;
254198

255199
#[inline(always)]
@@ -306,9 +250,7 @@ pub struct BlockRng64<R: Generator + ?Sized> {
306250
}
307251

308252
// Custom Debug implementation that does not expose the contents of `results`.
309-
impl<const N: usize, R: Generator<Result = [u64; N]> + fmt::Debug> fmt::Debug
310-
for BlockRng64<R>
311-
{
253+
impl<const N: usize, R: Generator<Result = [u64; N]> + fmt::Debug> fmt::Debug for BlockRng64<R> {
312254
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
313255
fmt.debug_struct("BlockRng64")
314256
.field("core", &self.core)
@@ -414,9 +356,7 @@ impl<const N: usize, R: Generator<Result = [u64; N]>> RngCore for BlockRng64<R>
414356
}
415357
}
416358

417-
impl<const N: usize, R: Generator<Result = [u64; N]> + SeedableRng> SeedableRng
418-
for BlockRng64<R>
419-
{
359+
impl<const N: usize, R: Generator<Result = [u64; N]> + SeedableRng> SeedableRng for BlockRng64<R> {
420360
type Seed = R::Seed;
421361

422362
#[inline(always)]

src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,52 @@ use core::{fmt, ops::DerefMut};
3232
pub mod block;
3333
pub mod le;
3434

35+
/// A random generator
36+
pub trait Generator {
37+
/// The result type.
38+
///
39+
/// This could be a simple word like `u64` or an array like `[u32; 16]`.
40+
type Result;
41+
42+
/// Generate a new result.
43+
///
44+
/// Since [`Self::Result`] may be large, the output is passed by reference.
45+
/// Word generators should likely implement this as a shim over another
46+
/// method:
47+
/// ```
48+
/// pub struct CountingGenerator(u64);
49+
/// impl CountingGenerator {
50+
/// fn next(&mut self) -> u64 {
51+
/// let x = self.0;
52+
/// self.0 = self.0.wrapping_add(1);
53+
/// x
54+
/// }
55+
/// }
56+
///
57+
/// impl rand_core::Generator for CountingGenerator {
58+
/// type Result = u64;
59+
///
60+
/// #[inline]
61+
/// fn generate(&mut self, result: &mut Self::Result) {
62+
/// *result = self.next();
63+
/// }
64+
/// }
65+
/// ```
66+
fn generate(&mut self, result: &mut Self::Result);
67+
}
68+
69+
/// A cryptographically secure generator
70+
///
71+
/// This is a marker trait used to indicate that a [`Generator`] implementation
72+
/// is supposed to be cryptographically secure.
73+
///
74+
/// Mock generators should not implement this trait *except* under a
75+
/// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be
76+
/// used in production.
77+
///
78+
/// See [`CryptoRng`] docs for more information.
79+
pub trait CryptoGenerator: Generator {}
80+
3581
/// Implementation-level interface for RNGs
3682
///
3783
/// This trait encapsulates the low-level functionality common to all

0 commit comments

Comments
 (0)