@@ -483,13 +483,17 @@ pub trait Rng {
483
483
Ok ( self . fill_bytes ( dest) )
484
484
}
485
485
486
- /// Fill `dest` entirely with random data.
487
- ///
488
- /// This method provides a convenient way to fill a slice with random data .
486
+ /// Fill `dest` entirely with random bytes, where `dest` is any type
487
+ /// supporting [`AsByteSliceMut`], namely slices over primitive integer
488
+ /// types (`i8`, `i16`, `u32`, etc.) .
489
489
///
490
490
/// On big-endian platforms this performs byte-swapping to ensure
491
491
/// portability of results from reproducible generators.
492
492
///
493
+ /// This uses [`fill_bytes`] internally which may handle some RNG errors
494
+ /// implicitly (e.g. waiting if the OS generator is not ready), but panics
495
+ /// on other errors. See also [`try_fill`] which returns errors.
496
+ ///
493
497
/// # Example
494
498
///
495
499
/// ```rust
@@ -499,21 +503,25 @@ pub trait Rng {
499
503
/// thread_rng().try_fill(&mut arr[..]);
500
504
/// ```
501
505
///
502
- /// [`ErrorKind`]: enum.ErrorKind.html
506
+ /// [`fill_bytes`]: trait.Rng.html#method.fill_bytes
507
+ /// [`try_fill`]: trait.Rng.html#method.try_fill
508
+ /// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
503
509
fn fill < T : AsByteSliceMut + ?Sized > ( & mut self , dest : & mut T ) where Self : Sized {
504
510
self . fill_bytes ( dest. as_byte_slice_mut ( ) ) ;
505
511
dest. to_le ( ) ;
506
512
}
507
513
508
- /// Fill `dest` entirely with random data.
509
- ///
510
- /// This method provides a convenient way to fill a slice with random data .
514
+ /// Fill `dest` entirely with random bytes, where `dest` is any type
515
+ /// supporting [`AsByteSliceMut`], namely slices over primitive integer
516
+ /// types (`i8`, `i16`, `u32`, etc.) .
511
517
///
512
518
/// On big-endian platforms this performs byte-swapping to ensure
513
519
/// portability of results from reproducible generators.
514
520
///
515
- /// Errors are forwarded from their source. In some cases errors may be
516
- /// resolvable; see [`ErrorKind`] and documentation for the RNG in use.
521
+ /// This uses [`try_fill_bytes`] internally and forwards all RNG errors. In
522
+ /// some cases errors may be resolvable; see [`ErrorKind`] and
523
+ /// documentation for the RNG in use. If you do not plan to handle these
524
+ /// errors you may prefer to use [`fill`].
517
525
///
518
526
/// # Example
519
527
///
@@ -531,6 +539,9 @@ pub trait Rng {
531
539
/// ```
532
540
///
533
541
/// [`ErrorKind`]: enum.ErrorKind.html
542
+ /// [`try_fill_bytes`]: trait.Rng.html#method.try_fill_bytes
543
+ /// [`fill`]: trait.Rng.html#method.fill
544
+ /// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
534
545
fn try_fill < T : AsByteSliceMut + ?Sized > ( & mut self , dest : & mut T ) -> Result < ( ) , Error > where Self : Sized {
535
546
self . try_fill_bytes ( dest. as_byte_slice_mut ( ) ) ?;
536
547
dest. to_le ( ) ;
@@ -757,6 +768,11 @@ impl<R: ?Sized> Rng for Box<R> where R: Rng {
757
768
}
758
769
759
770
/// Trait for casting types to byte slices
771
+ ///
772
+ /// This is used by the [`fill`] and [`try_fill`] methods.
773
+ ///
774
+ /// [`fill`]: trait.Rng.html#method.fill
775
+ /// [`try_fill`]: trait.Rng.html#method.try_fill
760
776
pub trait AsByteSliceMut {
761
777
/// Return a mutable reference to self as a byte slice
762
778
fn as_byte_slice_mut < ' a > ( & ' a mut self ) -> & ' a mut [ u8 ] ;
0 commit comments