Skip to content

Commit d0003ed

Browse files
authored
Merge pull request #381 from vks/large-seed
Docs for implementing `SeedableRng` for large seeds
2 parents c83eb1a + 65e7cf1 commit d0003ed

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rand_core/src/lib.rs

+37
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,43 @@ pub trait SeedableRng: Sized {
284284
/// partially overlapping periods.
285285
///
286286
/// For cryptographic RNG's a seed of 256 bits is recommended, `[u8; 32]`.
287+
///
288+
///
289+
/// # Implementing `SeedableRng` for RNGs with large seeds
290+
///
291+
/// Note that the required traits `core::default::Default` and
292+
/// `core::convert::AsMut<u8>` are not implemented for large arrays
293+
/// `[u8; N]` with `N` > 32. To be able to implement the traits required by
294+
/// `SeedableRng` for RNGs with such large seeds, the newtype pattern can be
295+
/// used:
296+
///
297+
/// ```
298+
/// use rand_core::SeedableRng;
299+
///
300+
/// const N: usize = 64;
301+
/// pub struct MyRngSeed(pub [u8; N]);
302+
/// pub struct MyRng(MyRngSeed);
303+
///
304+
/// impl Default for MyRngSeed {
305+
/// fn default() -> MyRngSeed {
306+
/// MyRngSeed([0; N])
307+
/// }
308+
/// }
309+
///
310+
/// impl AsMut<[u8]> for MyRngSeed {
311+
/// fn as_mut(&mut self) -> &mut [u8] {
312+
/// &mut self.0
313+
/// }
314+
/// }
315+
///
316+
/// impl SeedableRng for MyRng {
317+
/// type Seed = MyRngSeed;
318+
///
319+
/// fn from_seed(seed: MyRngSeed) -> MyRng {
320+
/// MyRng(seed)
321+
/// }
322+
/// }
323+
/// ```
287324
type Seed: Sized + Default + AsMut<[u8]>;
288325

289326
/// Create a new PRNG using the given seed.

0 commit comments

Comments
 (0)