Skip to content

Commit

Permalink
Add Item to RngCore
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 6, 2018
1 parent 628a952 commit ab6a4ba
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ impl<R: BlockRngCore> BlockRng<R> {
impl<R: BlockRngCore<Item=u32>> RngCore for BlockRng<R>
where <R as BlockRngCore>::Results: AsRef<[u32]>
{
type Item = R::Results;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
if self.index >= self.results.as_ref().len() {
Expand Down Expand Up @@ -346,6 +348,12 @@ where <R as BlockRngCore>::Results: AsRef<[u32]>
self.fill_bytes(dest);
Ok(())
}

fn next(&mut self) -> Self::Item {
let mut results = R::Results::default();
self.core.generate(&mut results);
results
}
}

impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
Expand Down Expand Up @@ -423,6 +431,8 @@ impl<R: BlockRngCore> BlockRng64<R> {
impl<R: BlockRngCore<Item=u64>> RngCore for BlockRng64<R>
where <R as BlockRngCore>::Results: AsRef<[u64]>
{
type Item = R::Results;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
let mut index = self.index * 2 - self.half_used as usize;
Expand Down Expand Up @@ -525,6 +535,12 @@ where <R as BlockRngCore>::Results: AsRef<[u64]>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}

fn next(&mut self) -> Self::Item {
let mut results = R::Results::default();
self.core.generate(&mut results);
results
}
}

impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
Expand Down
18 changes: 18 additions & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ pub mod le;
/// [`next_u64`]: trait.RngCore.html#tymethod.next_u64
/// [`CryptoRng`]: trait.CryptoRng.html
pub trait RngCore {
// Type of the values natively generated by this RNG.
type Item;

/// Return the next random `u32`.
///
/// RNGs must implement at least one method from this trait directly. In
Expand Down Expand Up @@ -186,6 +189,9 @@ pub trait RngCore {
///
/// [`fill_bytes`]: trait.RngCore.html#method.fill_bytes
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;

/// Return a native value
fn next(&mut self) -> Self::Item;
}

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


impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
type Item = R::Item;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
(**self).next_u32()
Expand All @@ -366,10 +374,16 @@ impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
(**self).try_fill_bytes(dest)
}

fn next(&mut self) -> Self::Item {
(**self).next()
}
}

#[cfg(feature="alloc")]
impl<R: RngCore + ?Sized> RngCore for Box<R> {
type Item = R::Item;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
(**self).next_u32()
Expand All @@ -387,4 +401,8 @@ impl<R: RngCore + ?Sized> RngCore for Box<R> {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
(**self).try_fill_bytes(dest)
}

fn next(&mut self) -> Self::Item {
(**self).next()
}
}
6 changes: 6 additions & 0 deletions src/jitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ fn black_box<T>(dummy: T) -> T {
}

impl RngCore for JitterRng {
type Item = u64;

fn next_u32(&mut self) -> u32 {
// We want to use both parts of the generated entropy
if self.data_half_used {
Expand Down Expand Up @@ -810,6 +812,10 @@ impl RngCore for JitterRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}

fn next(&mut self) -> Self::Item {
self.gen_entropy()
}
}

impl CryptoRng for JitterRng {}
Expand Down
6 changes: 6 additions & 0 deletions src/prng/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const STATE_WORDS: usize = 16;
pub struct ChaChaRng(BlockRng<ChaChaCore>);

impl RngCore for ChaChaRng {
type Item = <ChaChaCore as BlockRngCore>::Results;

#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
Expand All @@ -87,6 +89,10 @@ impl RngCore for ChaChaRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn next(&mut self) -> Self::Item {
self.0.next()
}
}

impl SeedableRng for ChaChaRng {
Expand Down
6 changes: 6 additions & 0 deletions src/prng/hc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
pub struct Hc128Rng(BlockRng<Hc128Core>);

impl RngCore for Hc128Rng {
type Item = <Hc128Core as BlockRngCore>::Results;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
Expand All @@ -82,6 +84,10 @@ impl RngCore for Hc128Rng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn next(&mut self) -> Self::Item {
self.0.next()
}
}

impl SeedableRng for Hc128Rng {
Expand Down
6 changes: 6 additions & 0 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
pub struct IsaacRng(BlockRng<IsaacCore>);

impl RngCore for IsaacRng {
type Item = <IsaacCore as BlockRngCore>::Results;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
Expand All @@ -108,6 +110,10 @@ impl RngCore for IsaacRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn next(&mut self) -> Self::Item {
self.0.next()
}
}

impl SeedableRng for IsaacRng {
Expand Down
6 changes: 6 additions & 0 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
pub struct Isaac64Rng(BlockRng64<Isaac64Core>);

impl RngCore for Isaac64Rng {
type Item = <Isaac64Core as BlockRngCore>::Results;

#[inline(always)]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
Expand All @@ -98,6 +100,10 @@ impl RngCore for Isaac64Rng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}

fn next(&mut self) -> Self::Item {
self.0.next()
}
}

impl SeedableRng for Isaac64Rng {
Expand Down
6 changes: 6 additions & 0 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ impl XorShiftRng {
}

impl RngCore for XorShiftRng {
type Item = u32;

#[inline]
fn next_u32(&mut self) -> u32 {
let x = self.x;
Expand All @@ -82,6 +84,10 @@ impl RngCore for XorShiftRng {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}

fn next(&mut self) -> Self::Item {
self.next_u32()
}
}

impl SeedableRng for XorShiftRng {
Expand Down

0 comments on commit ab6a4ba

Please sign in to comment.