Skip to content

Commit 80696ec

Browse files
committed
fill and try_fill methods: improve documentation; add links
1 parent 122272e commit 80696ec

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/lib.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,16 @@ pub trait Rng {
482482
Ok(self.fill_bytes(dest))
483483
}
484484

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.).
488487
///
489488
/// On big-endian platforms this performs byte-swapping to ensure
490489
/// portability of results from reproducible generators.
491490
///
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+
///
492495
/// # Example
493496
///
494497
/// ```rust
@@ -498,21 +501,23 @@ pub trait Rng {
498501
/// thread_rng().try_fill(&mut arr[..]);
499502
/// ```
500503
///
501-
/// [`ErrorKind`]: enum.ErrorKind.html
504+
/// [`fill_bytes`]: trait.Rng.html#method.fill_bytes
505+
/// [`try_fill`]: trait.Rng.html#method.try_fill
502506
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
503507
self.fill_bytes(dest.as_byte_slice_mut());
504508
dest.to_le();
505509
}
506510

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.).
510513
///
511514
/// On big-endian platforms this performs byte-swapping to ensure
512515
/// portability of results from reproducible generators.
513516
///
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`].
516521
///
517522
/// # Example
518523
///
@@ -530,6 +535,8 @@ pub trait Rng {
530535
/// ```
531536
///
532537
/// [`ErrorKind`]: enum.ErrorKind.html
538+
/// [`try_fill_bytes`]: trait.Rng.html#method.try_fill_bytes
539+
/// [`fill`]: trait.Rng.html#method.fill
533540
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
534541
self.try_fill_bytes(dest.as_byte_slice_mut())?;
535542
dest.to_le();
@@ -756,6 +763,11 @@ impl<R: ?Sized> Rng for Box<R> where R: Rng {
756763
}
757764

758765
/// 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
759771
pub trait AsByteSliceMut {
760772
/// Return a mutable reference to self as a byte slice
761773
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8];

0 commit comments

Comments
 (0)