Skip to content

Commit 96cfa9e

Browse files
committed
Rename Generator::Results -> Output
1 parent 35bf25c commit 96cfa9e

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/block.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
//! struct MyRngCore;
2121
//!
2222
//! impl Generator for MyRngCore {
23-
//! type Results = [u32; 16];
23+
//! type Output = [u32; 16];
2424
//!
25-
//! fn generate(&mut self, results: &mut Self::Results) {
25+
//! fn generate(&mut self, output: &mut Self::Output) {
2626
//! unimplemented!()
2727
//! }
2828
//! }
@@ -52,12 +52,15 @@ use serde::{Deserialize, Serialize};
5252

5353
/// A random (block) generator
5454
pub trait Generator {
55-
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
56-
/// generates, which will usually be an array like `[u32; 16]`.
57-
type Results;
55+
/// The output type.
56+
///
57+
/// For use with [`rand_core::block`](crate::block) code this must be `[u32; _]` or `[u64; _]`.
58+
type Output;
5859

59-
/// Generate a new block of results.
60-
fn generate(&mut self, results: &mut Self::Results);
60+
/// Generate a new block of `output`.
61+
///
62+
/// This must fill `output` with random data.
63+
fn generate(&mut self, output: &mut Self::Output);
6164
}
6265

6366
/// A cryptographically secure generator
@@ -110,18 +113,18 @@ pub trait CryptoGenerator: Generator {}
110113
#[cfg_attr(
111114
feature = "serde",
112115
serde(
113-
bound = "for<'x> R: Serialize + Deserialize<'x>, for<'x> R::Results: Serialize + Deserialize<'x>"
116+
bound = "for<'x> R: Serialize + Deserialize<'x>, for<'x> R::Output: Serialize + Deserialize<'x>"
114117
)
115118
)]
116119
pub struct BlockRng<R: Generator> {
117-
results: R::Results,
120+
results: R::Output,
118121
index: usize,
119122
/// The *core* part of the RNG, implementing the `generate` function.
120123
pub core: R,
121124
}
122125

123126
// Custom Debug implementation that does not expose the contents of `results`.
124-
impl<const N: usize, R: Generator<Results = [u32; N]> + fmt::Debug> fmt::Debug for BlockRng<R> {
127+
impl<const N: usize, R: Generator<Output = [u32; N]> + fmt::Debug> fmt::Debug for BlockRng<R> {
125128
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
126129
fmt.debug_struct("BlockRng")
127130
.field("core", &self.core)
@@ -131,7 +134,7 @@ impl<const N: usize, R: Generator<Results = [u32; N]> + fmt::Debug> fmt::Debug f
131134
}
132135
}
133136

134-
impl<const N: usize, R: Generator<Results = [u32; N]>> BlockRng<R> {
137+
impl<const N: usize, R: Generator<Output = [u32; N]>> BlockRng<R> {
135138
/// Create a new `BlockRng` from an existing RNG implementing
136139
/// `Generator`. Results will be generated on first use.
137140
#[inline]
@@ -170,7 +173,7 @@ impl<const N: usize, R: Generator<Results = [u32; N]>> BlockRng<R> {
170173
}
171174
}
172175

173-
impl<const N: usize, R: Generator<Results = [u32; N]>> RngCore for BlockRng<R> {
176+
impl<const N: usize, R: Generator<Output = [u32; N]>> RngCore for BlockRng<R> {
174177
#[inline]
175178
fn next_u32(&mut self) -> u32 {
176179
if self.index >= self.results.as_ref().len() {
@@ -223,7 +226,7 @@ impl<const N: usize, R: Generator<Results = [u32; N]>> RngCore for BlockRng<R> {
223226
}
224227
}
225228

226-
impl<const N: usize, R: Generator<Results = [u32; N]> + SeedableRng> SeedableRng for BlockRng<R> {
229+
impl<const N: usize, R: Generator<Output = [u32; N]> + SeedableRng> SeedableRng for BlockRng<R> {
227230
type Seed = R::Seed;
228231

229232
#[inline(always)]
@@ -247,7 +250,7 @@ impl<const N: usize, R: Generator<Results = [u32; N]> + SeedableRng> SeedableRng
247250
}
248251
}
249252

250-
impl<const N: usize, R: CryptoGenerator<Results = [u32; N]>> CryptoRng for BlockRng<R> {}
253+
impl<const N: usize, R: CryptoGenerator<Output = [u32; N]>> CryptoRng for BlockRng<R> {}
251254

252255
/// A wrapper type implementing [`RngCore`] for some type implementing
253256
/// [`Generator`] with `u64` array buffer; i.e. this can be used to implement
@@ -272,15 +275,15 @@ impl<const N: usize, R: CryptoGenerator<Results = [u32; N]>> CryptoRng for Block
272275
#[derive(Clone)]
273276
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
274277
pub struct BlockRng64<R: Generator + ?Sized> {
275-
results: R::Results,
278+
results: R::Output,
276279
index: usize,
277280
half_used: bool, // true if only half of the previous result is used
278281
/// The *core* part of the RNG, implementing the `generate` function.
279282
pub core: R,
280283
}
281284

282285
// Custom Debug implementation that does not expose the contents of `results`.
283-
impl<const N: usize, R: Generator<Results = [u64; N]> + fmt::Debug> fmt::Debug for BlockRng64<R> {
286+
impl<const N: usize, R: Generator<Output = [u64; N]> + fmt::Debug> fmt::Debug for BlockRng64<R> {
284287
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
285288
fmt.debug_struct("BlockRng64")
286289
.field("core", &self.core)
@@ -291,7 +294,7 @@ impl<const N: usize, R: Generator<Results = [u64; N]> + fmt::Debug> fmt::Debug f
291294
}
292295
}
293296

294-
impl<const N: usize, R: Generator<Results = [u64; N]>> BlockRng64<R> {
297+
impl<const N: usize, R: Generator<Output = [u64; N]>> BlockRng64<R> {
295298
/// Create a new `BlockRng` from an existing RNG implementing
296299
/// `Generator`. Results will be generated on first use.
297300
#[inline]
@@ -334,7 +337,7 @@ impl<const N: usize, R: Generator<Results = [u64; N]>> BlockRng64<R> {
334337
}
335338
}
336339

337-
impl<const N: usize, R: Generator<Results = [u64; N]>> RngCore for BlockRng64<R> {
340+
impl<const N: usize, R: Generator<Output = [u64; N]>> RngCore for BlockRng64<R> {
338341
#[inline]
339342
fn next_u32(&mut self) -> u32 {
340343
let mut index = self.index - self.half_used as usize;
@@ -386,7 +389,7 @@ impl<const N: usize, R: Generator<Results = [u64; N]>> RngCore for BlockRng64<R>
386389
}
387390
}
388391

389-
impl<const N: usize, R: Generator<Results = [u64; N]> + SeedableRng> SeedableRng for BlockRng64<R> {
392+
impl<const N: usize, R: Generator<Output = [u64; N]> + SeedableRng> SeedableRng for BlockRng64<R> {
390393
type Seed = R::Seed;
391394

392395
#[inline(always)]
@@ -410,7 +413,7 @@ impl<const N: usize, R: Generator<Results = [u64; N]> + SeedableRng> SeedableRng
410413
}
411414
}
412415

413-
impl<const N: usize, R: CryptoGenerator<Results = [u64; N]>> CryptoRng for BlockRng64<R> {}
416+
impl<const N: usize, R: CryptoGenerator<Output = [u64; N]>> CryptoRng for BlockRng64<R> {}
414417

415418
#[cfg(test)]
416419
mod test {
@@ -423,11 +426,11 @@ mod test {
423426
}
424427

425428
impl Generator for DummyRng {
426-
type Results = [u32; 16];
429+
type Output = [u32; 16];
427430

428-
fn generate(&mut self, results: &mut Self::Results) {
429-
for r in results {
430-
*r = self.counter;
431+
fn generate(&mut self, output: &mut Self::Output) {
432+
for item in output {
433+
*item = self.counter;
431434
self.counter = self.counter.wrapping_add(3511615421);
432435
}
433436
}
@@ -473,11 +476,11 @@ mod test {
473476
}
474477

475478
impl Generator for DummyRng64 {
476-
type Results = [u64; 8];
479+
type Output = [u64; 8];
477480

478-
fn generate(&mut self, results: &mut Self::Results) {
479-
for r in results {
480-
*r = self.counter;
481+
fn generate(&mut self, output: &mut Self::Output) {
482+
for item in output {
483+
*item = self.counter;
481484
self.counter = self.counter.wrapping_add(2781463553396133981);
482485
}
483486
}

0 commit comments

Comments
 (0)