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