Skip to content

Commit 0eb3cc6

Browse files
committed
Simplify trait BlockRngCore: remove assoc. type Item
1 parent 4d22ab2 commit 0eb3cc6

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) {
@@ -57,12 +56,9 @@ use serde::{Deserialize, Serialize};
5756
///
5857
/// See the [module][crate::block] documentation for details.
5958
pub trait BlockRngCore {
60-
/// Results element type, e.g. `u32`.
61-
type Item;
62-
6359
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
6460
/// generates, which will usually be an array like `[u32; 16]`.
65-
type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default;
61+
type Results;
6662

6763
/// Generate a new block of results.
6864
fn generate(&mut self, results: &mut Self::Results);
@@ -123,7 +119,7 @@ pub struct BlockRng<R: BlockRngCore> {
123119
}
124120

125121
// Custom Debug implementation that does not expose the contents of `results`.
126-
impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
122+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + fmt::Debug> fmt::Debug for BlockRng<R> {
127123
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
128124
fmt.debug_struct("BlockRng")
129125
.field("core", &self.core)
@@ -133,16 +129,15 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
133129
}
134130
}
135131

136-
impl<R: BlockRngCore> BlockRng<R> {
132+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> BlockRng<R> {
137133
/// Create a new `BlockRng` from an existing RNG implementing
138134
/// `BlockRngCore`. Results will be generated on first use.
139135
#[inline]
140136
pub fn new(core: R) -> BlockRng<R> {
141-
let results_empty = R::Results::default();
142137
BlockRng {
143138
core,
144-
index: results_empty.as_ref().len(),
145-
results: results_empty,
139+
index: N,
140+
results: [0; N],
146141
}
147142
}
148143

@@ -173,7 +168,7 @@ impl<R: BlockRngCore> BlockRng<R> {
173168
}
174169
}
175170

176-
impl<R: BlockRngCore<Item = u32>> RngCore for BlockRng<R> {
171+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]>> RngCore for BlockRng<R> {
177172
#[inline]
178173
fn next_u32(&mut self) -> u32 {
179174
if self.index >= self.results.as_ref().len() {
@@ -226,7 +221,9 @@ impl<R: BlockRngCore<Item = u32>> RngCore for BlockRng<R> {
226221
}
227222
}
228223

229-
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
224+
impl<const N: usize, R: BlockRngCore<Results = [u32; N]> + SeedableRng> SeedableRng
225+
for BlockRng<R>
226+
{
230227
type Seed = R::Seed;
231228

232229
#[inline(always)]
@@ -250,7 +247,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
250247
}
251248
}
252249

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

255252
/// A wrapper type implementing [`RngCore`] for some type implementing
256253
/// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement
@@ -283,7 +280,9 @@ pub struct BlockRng64<R: BlockRngCore + ?Sized> {
283280
}
284281

285282
// Custom Debug implementation that does not expose the contents of `results`.
286-
impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
283+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + fmt::Debug> fmt::Debug
284+
for BlockRng64<R>
285+
{
287286
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
288287
fmt.debug_struct("BlockRng64")
289288
.field("core", &self.core)
@@ -294,12 +293,12 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
294293
}
295294
}
296295

297-
impl<R: BlockRngCore> BlockRng64<R> {
296+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> BlockRng64<R> {
298297
/// Create a new `BlockRng` from an existing RNG implementing
299298
/// `BlockRngCore`. Results will be generated on first use.
300299
#[inline]
301300
pub fn new(core: R) -> BlockRng64<R> {
302-
let results_empty = R::Results::default();
301+
let results_empty = [0; N];
303302
BlockRng64 {
304303
core,
305304
index: results_empty.as_ref().len(),
@@ -337,7 +336,7 @@ impl<R: BlockRngCore> BlockRng64<R> {
337336
}
338337
}
339338

340-
impl<R: BlockRngCore<Item = u64>> RngCore for BlockRng64<R> {
339+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]>> RngCore for BlockRng64<R> {
341340
#[inline]
342341
fn next_u32(&mut self) -> u32 {
343342
let mut index = self.index - self.half_used as usize;
@@ -389,7 +388,9 @@ impl<R: BlockRngCore<Item = u64>> RngCore for BlockRng64<R> {
389388
}
390389
}
391390

392-
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
391+
impl<const N: usize, R: BlockRngCore<Results = [u64; N]> + SeedableRng> SeedableRng
392+
for BlockRng64<R>
393+
{
393394
type Seed = R::Seed;
394395

395396
#[inline(always)]
@@ -413,7 +414,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
413414
}
414415
}
415416

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

418419
#[cfg(test)]
419420
mod test {
@@ -426,7 +427,6 @@ mod test {
426427
}
427428

428429
impl BlockRngCore for DummyRng {
429-
type Item = u32;
430430
type Results = [u32; 16];
431431

432432
fn generate(&mut self, results: &mut Self::Results) {
@@ -477,7 +477,6 @@ mod test {
477477
}
478478

479479
impl BlockRngCore for DummyRng64 {
480-
type Item = u64;
481480
type Results = [u64; 8];
482481

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

0 commit comments

Comments
 (0)