Skip to content

Commit 01d4237

Browse files
committed
Simplify trait BlockRngCore: remove assoc. type Item
1 parent 7161690 commit 01d4237

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/block.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
//! struct MyRngCore;
2121
//!
2222
//! impl BlockRngCore for MyRngCore {
23-
//! type Item = u32;
2423
//! type Results = [u32; 16];
2524
//!
2625
//! fn generate(&mut self, results: &mut Self::Results) {
@@ -55,12 +54,9 @@ use core::fmt;
5554
///
5655
/// See the [module][crate::block] documentation for details.
5756
pub trait BlockRngCore {
58-
/// Results element type, e.g. `u32`.
59-
type Item;
60-
6157
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
6258
/// generates, which will usually be an array like `[u32; 16]`.
63-
type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default;
59+
type Results;
6460

6561
/// Generate a new block of results.
6662
fn generate(&mut self, results: &mut Self::Results);
@@ -114,7 +110,7 @@ pub struct BlockRng<R: BlockRngCore> {
114110
}
115111

116112
// Custom Debug implementation that does not expose the contents of `results`.
117-
impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
113+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + fmt::Debug> fmt::Debug for BlockRng<R> {
118114
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
119115
fmt.debug_struct("BlockRng")
120116
.field("core", &self.core)
@@ -124,16 +120,15 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
124120
}
125121
}
126122

127-
impl<R: BlockRngCore> BlockRng<R> {
123+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> BlockRng<R> {
128124
/// Create a new `BlockRng` from an existing RNG implementing
129125
/// `BlockRngCore`. Results will be generated on first use.
130126
#[inline]
131127
pub fn new(core: R) -> BlockRng<R> {
132-
let results_empty = R::Results::default();
133128
BlockRng {
134129
core,
135-
index: results_empty.as_ref().len(),
136-
results: results_empty,
130+
index: N,
131+
results: [0; N],
137132
}
138133
}
139134

@@ -164,7 +159,7 @@ impl<R: BlockRngCore> BlockRng<R> {
164159
}
165160
}
166161

167-
impl<R: BlockRngCore<Item = u32>> RngCore for BlockRng<R> {
162+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> RngCore for BlockRng<R> {
168163
#[inline]
169164
fn next_u32(&mut self) -> u32 {
170165
if self.index >= self.results.as_ref().len() {
@@ -217,7 +212,9 @@ impl<R: BlockRngCore<Item = u32>> RngCore for BlockRng<R> {
217212
}
218213
}
219214

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

223220
#[inline(always)]
@@ -241,7 +238,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
241238
}
242239
}
243240

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

246243
/// A wrapper type implementing [`RngCore`] for some type implementing
247244
/// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement
@@ -273,7 +270,9 @@ pub struct BlockRng64<R: BlockRngCore + ?Sized> {
273270
}
274271

275272
// Custom Debug implementation that does not expose the contents of `results`.
276-
impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
273+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + fmt::Debug> fmt::Debug
274+
for BlockRng64<R>
275+
{
277276
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
278277
fmt.debug_struct("BlockRng64")
279278
.field("core", &self.core)
@@ -284,12 +283,12 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
284283
}
285284
}
286285

287-
impl<R: BlockRngCore> BlockRng64<R> {
286+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> BlockRng64<R> {
288287
/// Create a new `BlockRng` from an existing RNG implementing
289288
/// `BlockRngCore`. Results will be generated on first use.
290289
#[inline]
291290
pub fn new(core: R) -> BlockRng64<R> {
292-
let results_empty = R::Results::default();
291+
let results_empty = [0; N];
293292
BlockRng64 {
294293
core,
295294
index: results_empty.as_ref().len(),
@@ -327,7 +326,7 @@ impl<R: BlockRngCore> BlockRng64<R> {
327326
}
328327
}
329328

330-
impl<R: BlockRngCore<Item = u64>> RngCore for BlockRng64<R> {
329+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> RngCore for BlockRng64<R> {
331330
#[inline]
332331
fn next_u32(&mut self) -> u32 {
333332
let mut index = self.index - self.half_used as usize;
@@ -379,7 +378,9 @@ impl<R: BlockRngCore<Item = u64>> RngCore for BlockRng64<R> {
379378
}
380379
}
381380

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

385386
#[inline(always)]
@@ -403,7 +404,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
403404
}
404405
}
405406

406-
impl<R: CryptoBlockRng + BlockRngCore<Item = u64>> CryptoRng for BlockRng64<R> {}
407+
impl<const N: usize, R: CryptoBlockRng<Results = [u64; N]>> CryptoRng for BlockRng64<R> {}
407408

408409
#[cfg(test)]
409410
mod test {
@@ -416,7 +417,6 @@ mod test {
416417
}
417418

418419
impl BlockRngCore for DummyRng {
419-
type Item = u32;
420420
type Results = [u32; 16];
421421

422422
fn generate(&mut self, results: &mut Self::Results) {
@@ -467,7 +467,6 @@ mod test {
467467
}
468468

469469
impl BlockRngCore for DummyRng64 {
470-
type Item = u64;
471470
type Results = [u64; 8];
472471

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

0 commit comments

Comments
 (0)