-
-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RngCore::bytes_per_round #396
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,6 +186,19 @@ pub trait RngCore { | |
/// | ||
/// [`fill_bytes`]: trait.RngCore.html#method.fill_bytes | ||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>; | ||
|
||
/// Number of bytes generated per round of this RNG. | ||
/// | ||
/// Some algorithms would benefit from knowing some basic properties about | ||
/// the RNG. In terms of performance an algorithm may want to know whether | ||
/// an RNG is best at generating `u32`s, or could provide `u64`s or more at | ||
/// little to no extra cost. | ||
/// | ||
/// For many RNGs a simple definition is: the smallest number of bytes this | ||
/// RNG can generate without throwing away part of the generated value. | ||
/// | ||
/// `bytes_per_round` has a default implementation that returns `4` (bytes). | ||
fn bytes_per_round(&self) -> usize { 4 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we really give a default implementation here? Did you forget to implement for Jitter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not sure. A disadvantage with a default implementation is that you can easily forget a wrapper.
|
||
} | ||
|
||
/// A trait for RNGs which do not generate random numbers individually, but in | ||
|
@@ -384,7 +397,9 @@ pub trait SeedableRng: Sized { | |
} | ||
} | ||
|
||
|
||
// Implement `RngCore` for references to an `RngCore`. | ||
// Force inlining all functions, so that it is up to the `RngCore` | ||
// implementation and the optimizer to decide on inlining. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, true. But before |
||
impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R { | ||
#[inline(always)] | ||
fn next_u32(&mut self) -> u32 { | ||
|
@@ -396,15 +411,24 @@ impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R { | |
(**self).next_u64() | ||
} | ||
|
||
#[inline(always)] | ||
fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
(**self).fill_bytes(dest) | ||
} | ||
|
||
#[inline(always)] | ||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
(**self).try_fill_bytes(dest) | ||
} | ||
|
||
fn bytes_per_round(&self) -> usize { | ||
(**self).bytes_per_round() | ||
} | ||
} | ||
|
||
// Implement `RngCore` for boxed references to an `RngCore`. | ||
// Force inlining all functions, so that it is up to the `RngCore` | ||
// implementation and the optimizer to decide on inlining. | ||
#[cfg(feature="alloc")] | ||
impl<R: RngCore + ?Sized> RngCore for Box<R> { | ||
#[inline(always)] | ||
|
@@ -417,11 +441,17 @@ impl<R: RngCore + ?Sized> RngCore for Box<R> { | |
(**self).next_u64() | ||
} | ||
|
||
#[inline(always)] | ||
fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
(**self).fill_bytes(dest) | ||
} | ||
|
||
#[inline(always)] | ||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
(**self).try_fill_bytes(dest) | ||
} | ||
|
||
fn bytes_per_round(&self) -> usize { | ||
(**self).bytes_per_round() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,46 +13,52 @@ | |
use {Rng}; | ||
use distributions::{Distribution, Standard}; | ||
|
||
impl Distribution<isize> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> isize { | ||
rng.gen::<usize>() as isize | ||
} | ||
} | ||
|
||
impl Distribution<i8> for Standard { | ||
impl Distribution<u8> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i8 { | ||
rng.next_u32() as i8 | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 { | ||
rng.next_u32() as u8 | ||
} | ||
} | ||
|
||
impl Distribution<i16> for Standard { | ||
impl Distribution<u16> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i16 { | ||
rng.next_u32() as i16 | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u16 { | ||
rng.next_u32() as u16 | ||
} | ||
} | ||
|
||
impl Distribution<i32> for Standard { | ||
impl Distribution<u32> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i32 { | ||
rng.next_u32() as i32 | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u32 { | ||
rng.next_u32() | ||
} | ||
} | ||
|
||
impl Distribution<i64> for Standard { | ||
impl Distribution<u64> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i64 { | ||
rng.next_u64() as i64 | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 { | ||
rng.next_u64() | ||
} | ||
} | ||
|
||
#[cfg(feature = "i128_support")] | ||
impl Distribution<i128> for Standard { | ||
impl Distribution<u128> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i128 { | ||
rng.gen::<u128>() as i128 | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u128 { | ||
if rng.bytes_per_round() < 128 { | ||
// Use LE; we explicitly generate one value before the next. | ||
let x = rng.next_u64() as u128; | ||
let y = rng.next_u64() as u128; | ||
(y << 64) | x | ||
} else { | ||
let mut val = 0u128; | ||
unsafe { | ||
let ptr = &mut val; | ||
let b_ptr = &mut *(ptr as *mut u128 as *mut [u8; 16]); | ||
rng.fill_bytes(b_ptr); | ||
} | ||
val.to_le() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to see benchmarks for this on a BE platform There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sometimes just change things to |
||
} | ||
} | ||
} | ||
|
||
|
@@ -70,44 +76,23 @@ impl Distribution<usize> for Standard { | |
} | ||
} | ||
|
||
impl Distribution<u8> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 { | ||
rng.next_u32() as u8 | ||
macro_rules! impl_int_from_uint { | ||
($ty:ty, $uty:ty) => { | ||
impl Distribution<$ty> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty { | ||
rng.gen::<$uty>() as $ty | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Distribution<u16> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u16 { | ||
rng.next_u32() as u16 | ||
} | ||
} | ||
|
||
impl Distribution<u32> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u32 { | ||
rng.next_u32() | ||
} | ||
} | ||
|
||
impl Distribution<u64> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 { | ||
rng.next_u64() | ||
} | ||
} | ||
|
||
#[cfg(feature = "i128_support")] | ||
impl Distribution<u128> for Standard { | ||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u128 { | ||
// Use LE; we explicitly generate one value before the next. | ||
let x = rng.next_u64() as u128; | ||
let y = rng.next_u64() as u128; | ||
(y << 64) | x | ||
} | ||
} | ||
impl_int_from_uint! { i8, u8 } | ||
impl_int_from_uint! { i16, u16 } | ||
impl_int_from_uint! { i32, u32 } | ||
impl_int_from_uint! { i64, u64 } | ||
#[cfg(feature = "i128_support")] impl_int_from_uint! { i128, u128 } | ||
impl_int_from_uint! { isize, usize } | ||
|
||
|
||
#[cfg(test)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be an associated constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something like that in a not really thought through attempt: #377 (comment). The problem is that we then can't make
RngCore
into a trait object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird; doesn't sound like associated constants should prevent a trait from becoming object-safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.