-
-
Notifications
You must be signed in to change notification settings - Fork 1
Simplify trait BlockRngCore and rename to Generator #26
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
base: master
Are you sure you want to change the base?
Changes from all 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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
| #![deny(missing_docs)] | ||
| #![deny(missing_debug_implementations)] | ||
| #![deny(clippy::undocumented_unsafe_blocks)] | ||
| #![doc(test(attr(allow(unused_variables), deny(warnings))))] | ||
| #![doc(test(attr(allow(unused), deny(warnings))))] | ||
| #![cfg_attr(docsrs, feature(doc_cfg))] | ||
| #![no_std] | ||
|
|
||
|
|
@@ -32,6 +32,52 @@ use core::{fmt, ops::DerefMut}; | |
| pub mod block; | ||
| pub mod le; | ||
|
|
||
| /// A random generator | ||
| pub trait Generator { | ||
| /// The result type. | ||
| /// | ||
| /// This could be a simple word like `u64` or an array like `[u32; 16]`. | ||
| type Result; | ||
|
|
||
| /// Generate a new result. | ||
| /// | ||
| /// Since [`Self::Result`] may be large, the output is passed by reference. | ||
| /// Word generators should likely implement this as a shim over another | ||
| /// method: | ||
| /// ``` | ||
| /// pub struct CountingGenerator(u64); | ||
| /// impl CountingGenerator { | ||
| /// fn next(&mut self) -> u64 { | ||
| /// let x = self.0; | ||
| /// self.0 = self.0.wrapping_add(1); | ||
| /// x | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// impl rand_core::Generator for CountingGenerator { | ||
| /// type Result = u64; | ||
| /// | ||
| /// #[inline] | ||
| /// fn generate(&mut self, result: &mut Self::Result) { | ||
| /// *result = self.next(); | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| fn generate(&mut self, result: &mut Self::Result); | ||
|
Member
Author
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. Generating by-reference has two limitations:
Alternative 1: return Alternative 2: have both fns. Over-complex. |
||
| } | ||
|
|
||
| /// A cryptographically secure generator | ||
| /// | ||
| /// This is a marker trait used to indicate that a [`Generator`] implementation | ||
| /// is supposed to be cryptographically secure. | ||
| /// | ||
| /// Mock generators should not implement this trait *except* under a | ||
| /// `#[cfg(test)]` attribute to ensure that mock "crypto" generators cannot be | ||
| /// used in production. | ||
| /// | ||
| /// See [`CryptoRng`] docs for more information. | ||
| pub trait CryptoGenerator: Generator {} | ||
|
|
||
| /// Implementation-level interface for RNGs | ||
| /// | ||
| /// This trait encapsulates the low-level functionality common to all | ||
|
|
||
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.
Should this be renamed? Most of the
std::opstraits have anOutputassociated type, so that may be the more logical name.