@@ -339,17 +339,27 @@ pub trait Rand : Sized {
339
339
340
340
/// A random number generator.
341
341
pub trait Rng {
342
- /// Return the next random u32.
342
+ /// Return the next random `u32`.
343
+ ///
344
+ /// Implementations of this trait must implement at least one of
345
+ /// `next_u32`, `next_u64` and `fill_bytes` directly. In the case this
346
+ /// function is not implemented directly, it can be implemented using
347
+ /// `self.next_u64() as u32` or via `fill_bytes` (TODO: expose helper
348
+ /// function).
343
349
fn next_u32 ( & mut self ) -> u32 ;
344
350
345
- /// Return the next random u64.
351
+ /// Return the next random `u64`.
352
+ ///
353
+ /// Implementations of this trait must implement at least one of
354
+ /// `next_u32`, `next_u64` and `fill_bytes` directly. In the case this
355
+ /// function is not implemented directly, the default implementation will
356
+ /// generate values via `next_u32` in little-endian fashion, or this
357
+ /// function can be implemented via `fill_bytes` (TODO: expose helper
358
+ /// function).
346
359
///
347
- /// This function has a default implementation of `next_u32`. The
348
- /// default implementation should not be used in wrapper types since the
349
- /// wrapped RNG may have its own implementation which may be more efficient
350
- /// or even produce different results.
360
+ /// Types wrapping an inner RNG must not use the default implementation,
361
+ /// since the inner RNG's implementation may produce different values.
351
362
fn next_u64 ( & mut self ) -> u64 {
352
- // TODO: remove default implementation once impls module is exposed
353
363
impls:: next_u64_via_u32 ( self )
354
364
}
355
365
@@ -407,17 +417,21 @@ pub trait Rng {
407
417
408
418
/// Fill `dest` with random data.
409
419
///
410
- /// This function has a default implementation in terms of `next_u64`. The
411
- /// default implementation should not be used in wrapper types since the
412
- /// wrapped RNG may have its own implementation which may be more efficient
413
- /// or even produce different results.
414
- ///
415
- /// This method does *not* have a requirement to bear any fixed
416
- /// relationship to the other methods, for example, it does *not*
417
- /// have to result in the same output as progressively filling
418
- /// `dest` with `self.gen::<u8>()`, and any such behaviour should
419
- /// not be relied upon.
420
- ///
420
+ /// Implementations of this trait must implement at least one of
421
+ /// `next_u32`, `next_u64` and `fill_bytes` directly. In the case this
422
+ /// function is not implemented directly, the default implementation will
423
+ /// generate values via `next_u64` in little-endian fashion.
424
+ /// (TODO: expose helper function to allow implementation via `next_u32`.)
425
+ ///
426
+ /// There is no requirement on how this method generates values relative to
427
+ /// `next_u32` or `next_u64`; e.g. a `u64` cast to bytes is not required to
428
+ /// have the same value as eight bytes filled via this function. There *is*
429
+ /// a requirement of portability for reproducible generators which implies
430
+ /// that any seedable generator must fix endianness when generating bytes.
431
+ ///
432
+ /// Types wrapping an inner RNG must not use the default implementation,
433
+ /// since the inner RNG's implementation may produce different values.
434
+ ///
421
435
/// This method should guarantee that `dest` is entirely filled
422
436
/// with new data, and may panic if this is impossible
423
437
/// (e.g. reading past the end of a file that is being used as the
@@ -433,7 +447,6 @@ pub trait Rng {
433
447
/// println!("{:?}", &v[..]);
434
448
/// ```
435
449
fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
436
- // TODO: remove default implementation once impls module is exposed
437
450
impls:: fill_bytes_via_u64 ( self , dest)
438
451
}
439
452
0 commit comments