Skip to content

Commit 48f076c

Browse files
committed
fill and try_fill methods: improve documentation; add links
1 parent da06ac6 commit 48f076c

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/lib.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,17 @@ pub trait Rng {
483483
Ok(self.fill_bytes(dest))
484484
}
485485

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.).
489489
///
490490
/// On big-endian platforms this performs byte-swapping to ensure
491491
/// portability of results from reproducible generators.
492492
///
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+
///
493497
/// # Example
494498
///
495499
/// ```rust
@@ -499,21 +503,25 @@ pub trait Rng {
499503
/// thread_rng().try_fill(&mut arr[..]);
500504
/// ```
501505
///
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
503509
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
504510
self.fill_bytes(dest.as_byte_slice_mut());
505511
dest.to_le();
506512
}
507513

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.).
511517
///
512518
/// On big-endian platforms this performs byte-swapping to ensure
513519
/// portability of results from reproducible generators.
514520
///
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`].
517525
///
518526
/// # Example
519527
///
@@ -531,6 +539,9 @@ pub trait Rng {
531539
/// ```
532540
///
533541
/// [`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
534545
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
535546
self.try_fill_bytes(dest.as_byte_slice_mut())?;
536547
dest.to_le();
@@ -757,6 +768,11 @@ impl<R: ?Sized> Rng for Box<R> where R: Rng {
757768
}
758769

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

0 commit comments

Comments
 (0)