-
-
Notifications
You must be signed in to change notification settings - Fork 439
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 1 commit
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 | ||
|
@@ -403,6 +416,10 @@ 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 bytes_per_round(&self) -> usize { | ||
(**self).bytes_per_round() | ||
} | ||
} | ||
|
||
#[cfg(feature="alloc")] | ||
|
@@ -424,4 +441,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 bytes_per_round(&self) -> usize { | ||
(**self).bytes_per_round() | ||
} | ||
} |
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.