Skip to content

Commit

Permalink
Merge pull request rust-random#381 from vks/large-seed
Browse files Browse the repository at this point in the history
Docs for implementing `SeedableRng` for large seeds
  • Loading branch information
pitdicker committed Apr 11, 2018
2 parents c83eb1a + 65e7cf1 commit d0003ed
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,43 @@ pub trait SeedableRng: Sized {
/// partially overlapping periods.
///
/// For cryptographic RNG's a seed of 256 bits is recommended, `[u8; 32]`.
///
///
/// # Implementing `SeedableRng` for RNGs with large seeds
///
/// Note that the required traits `core::default::Default` and
/// `core::convert::AsMut<u8>` are not implemented for large arrays
/// `[u8; N]` with `N` > 32. To be able to implement the traits required by
/// `SeedableRng` for RNGs with such large seeds, the newtype pattern can be
/// used:
///
/// ```
/// use rand_core::SeedableRng;
///
/// const N: usize = 64;
/// pub struct MyRngSeed(pub [u8; N]);
/// pub struct MyRng(MyRngSeed);
///
/// impl Default for MyRngSeed {
/// fn default() -> MyRngSeed {
/// MyRngSeed([0; N])
/// }
/// }
///
/// impl AsMut<[u8]> for MyRngSeed {
/// fn as_mut(&mut self) -> &mut [u8] {
/// &mut self.0
/// }
/// }
///
/// impl SeedableRng for MyRng {
/// type Seed = MyRngSeed;
///
/// fn from_seed(seed: MyRngSeed) -> MyRng {
/// MyRng(seed)
/// }
/// }
/// ```
type Seed: Sized + Default + AsMut<[u8]>;

/// Create a new PRNG using the given seed.
Expand Down

0 comments on commit d0003ed

Please sign in to comment.