Skip to content

Commit ab6a4ba

Browse files
committed
Add Item to RngCore
1 parent 628a952 commit ab6a4ba

File tree

8 files changed

+70
-0
lines changed

8 files changed

+70
-0
lines changed

rand_core/src/impls.rs

+16
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ impl<R: BlockRngCore> BlockRng<R> {
239239
impl<R: BlockRngCore<Item=u32>> RngCore for BlockRng<R>
240240
where <R as BlockRngCore>::Results: AsRef<[u32]>
241241
{
242+
type Item = R::Results;
243+
242244
#[inline(always)]
243245
fn next_u32(&mut self) -> u32 {
244246
if self.index >= self.results.as_ref().len() {
@@ -346,6 +348,12 @@ where <R as BlockRngCore>::Results: AsRef<[u32]>
346348
self.fill_bytes(dest);
347349
Ok(())
348350
}
351+
352+
fn next(&mut self) -> Self::Item {
353+
let mut results = R::Results::default();
354+
self.core.generate(&mut results);
355+
results
356+
}
349357
}
350358

351359
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
@@ -423,6 +431,8 @@ impl<R: BlockRngCore> BlockRng64<R> {
423431
impl<R: BlockRngCore<Item=u64>> RngCore for BlockRng64<R>
424432
where <R as BlockRngCore>::Results: AsRef<[u64]>
425433
{
434+
type Item = R::Results;
435+
426436
#[inline(always)]
427437
fn next_u32(&mut self) -> u32 {
428438
let mut index = self.index * 2 - self.half_used as usize;
@@ -525,6 +535,12 @@ where <R as BlockRngCore>::Results: AsRef<[u64]>
525535
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
526536
Ok(self.fill_bytes(dest))
527537
}
538+
539+
fn next(&mut self) -> Self::Item {
540+
let mut results = R::Results::default();
541+
self.core.generate(&mut results);
542+
results
543+
}
528544
}
529545

530546
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {

rand_core/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ pub mod le;
140140
/// [`next_u64`]: trait.RngCore.html#tymethod.next_u64
141141
/// [`CryptoRng`]: trait.CryptoRng.html
142142
pub trait RngCore {
143+
// Type of the values natively generated by this RNG.
144+
type Item;
145+
143146
/// Return the next random `u32`.
144147
///
145148
/// RNGs must implement at least one method from this trait directly. In
@@ -186,6 +189,9 @@ pub trait RngCore {
186189
///
187190
/// [`fill_bytes`]: trait.RngCore.html#method.fill_bytes
188191
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;
192+
193+
/// Return a native value
194+
fn next(&mut self) -> Self::Item;
189195
}
190196

191197
/// A trait for RNGs which do not generate random numbers individually, but in
@@ -349,6 +355,8 @@ pub trait SeedableRng: Sized {
349355

350356

351357
impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
358+
type Item = R::Item;
359+
352360
#[inline(always)]
353361
fn next_u32(&mut self) -> u32 {
354362
(**self).next_u32()
@@ -366,10 +374,16 @@ impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
366374
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
367375
(**self).try_fill_bytes(dest)
368376
}
377+
378+
fn next(&mut self) -> Self::Item {
379+
(**self).next()
380+
}
369381
}
370382

371383
#[cfg(feature="alloc")]
372384
impl<R: RngCore + ?Sized> RngCore for Box<R> {
385+
type Item = R::Item;
386+
373387
#[inline(always)]
374388
fn next_u32(&mut self) -> u32 {
375389
(**self).next_u32()
@@ -387,4 +401,8 @@ impl<R: RngCore + ?Sized> RngCore for Box<R> {
387401
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
388402
(**self).try_fill_bytes(dest)
389403
}
404+
405+
fn next(&mut self) -> Self::Item {
406+
(**self).next()
407+
}
390408
}

src/jitter.rs

+6
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ fn black_box<T>(dummy: T) -> T {
781781
}
782782

783783
impl RngCore for JitterRng {
784+
type Item = u64;
785+
784786
fn next_u32(&mut self) -> u32 {
785787
// We want to use both parts of the generated entropy
786788
if self.data_half_used {
@@ -810,6 +812,10 @@ impl RngCore for JitterRng {
810812
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
811813
Ok(self.fill_bytes(dest))
812814
}
815+
816+
fn next(&mut self) -> Self::Item {
817+
self.gen_entropy()
818+
}
813819
}
814820

815821
impl CryptoRng for JitterRng {}

src/prng/chacha.rs

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ const STATE_WORDS: usize = 16;
6868
pub struct ChaChaRng(BlockRng<ChaChaCore>);
6969

7070
impl RngCore for ChaChaRng {
71+
type Item = <ChaChaCore as BlockRngCore>::Results;
72+
7173
#[inline]
7274
fn next_u32(&mut self) -> u32 {
7375
self.0.next_u32()
@@ -87,6 +89,10 @@ impl RngCore for ChaChaRng {
8789
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
8890
self.0.try_fill_bytes(dest)
8991
}
92+
93+
fn next(&mut self) -> Self::Item {
94+
self.0.next()
95+
}
9096
}
9197

9298
impl SeedableRng for ChaChaRng {

src/prng/hc128.rs

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
6565
pub struct Hc128Rng(BlockRng<Hc128Core>);
6666

6767
impl RngCore for Hc128Rng {
68+
type Item = <Hc128Core as BlockRngCore>::Results;
69+
6870
#[inline(always)]
6971
fn next_u32(&mut self) -> u32 {
7072
self.0.next_u32()
@@ -82,6 +84,10 @@ impl RngCore for Hc128Rng {
8284
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
8385
self.0.try_fill_bytes(dest)
8486
}
87+
88+
fn next(&mut self) -> Self::Item {
89+
self.0.next()
90+
}
8591
}
8692

8793
impl SeedableRng for Hc128Rng {

src/prng/isaac.rs

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
9191
pub struct IsaacRng(BlockRng<IsaacCore>);
9292

9393
impl RngCore for IsaacRng {
94+
type Item = <IsaacCore as BlockRngCore>::Results;
95+
9496
#[inline(always)]
9597
fn next_u32(&mut self) -> u32 {
9698
self.0.next_u32()
@@ -108,6 +110,10 @@ impl RngCore for IsaacRng {
108110
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
109111
self.0.try_fill_bytes(dest)
110112
}
113+
114+
fn next(&mut self) -> Self::Item {
115+
self.0.next()
116+
}
111117
}
112118

113119
impl SeedableRng for IsaacRng {

src/prng/isaac64.rs

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
8181
pub struct Isaac64Rng(BlockRng64<Isaac64Core>);
8282

8383
impl RngCore for Isaac64Rng {
84+
type Item = <Isaac64Core as BlockRngCore>::Results;
85+
8486
#[inline(always)]
8587
fn next_u32(&mut self) -> u32 {
8688
self.0.next_u32()
@@ -98,6 +100,10 @@ impl RngCore for Isaac64Rng {
98100
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
99101
self.0.try_fill_bytes(dest)
100102
}
103+
104+
fn next(&mut self) -> Self::Item {
105+
self.0.next()
106+
}
101107
}
102108

103109
impl SeedableRng for Isaac64Rng {

src/prng/xorshift.rs

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ impl XorShiftRng {
5959
}
6060

6161
impl RngCore for XorShiftRng {
62+
type Item = u32;
63+
6264
#[inline]
6365
fn next_u32(&mut self) -> u32 {
6466
let x = self.x;
@@ -82,6 +84,10 @@ impl RngCore for XorShiftRng {
8284
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
8385
Ok(self.fill_bytes(dest))
8486
}
87+
88+
fn next(&mut self) -> Self::Item {
89+
self.next_u32()
90+
}
8591
}
8692

8793
impl SeedableRng for XorShiftRng {

0 commit comments

Comments
 (0)