File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -284,6 +284,43 @@ pub trait SeedableRng: Sized {
284
284
/// partially overlapping periods.
285
285
///
286
286
/// 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
+ /// ```
287
324
type Seed : Sized + Default + AsMut < [ u8 ] > ;
288
325
289
326
/// Create a new PRNG using the given seed.
You can’t perform that action at this time.
0 commit comments