Skip to content

Commit

Permalink
fill and try_fill methods: improve documentation; add links
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Feb 7, 2018
1 parent da06ac6 commit 48f076c
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,17 @@ pub trait Rng {
Ok(self.fill_bytes(dest))
}

/// Fill `dest` entirely with random data.
///
/// This method provides a convenient way to fill a slice with random data.
/// Fill `dest` entirely with random bytes, where `dest` is any type
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
/// types (`i8`, `i16`, `u32`, etc.).
///
/// On big-endian platforms this performs byte-swapping to ensure
/// portability of results from reproducible generators.
///
/// This uses [`fill_bytes`] internally which may handle some RNG errors
/// implicitly (e.g. waiting if the OS generator is not ready), but panics
/// on other errors. See also [`try_fill`] which returns errors.
///
/// # Example
///
/// ```rust
Expand All @@ -499,21 +503,25 @@ pub trait Rng {
/// thread_rng().try_fill(&mut arr[..]);
/// ```
///
/// [`ErrorKind`]: enum.ErrorKind.html
/// [`fill_bytes`]: trait.Rng.html#method.fill_bytes
/// [`try_fill`]: trait.Rng.html#method.try_fill
/// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
self.fill_bytes(dest.as_byte_slice_mut());
dest.to_le();
}

/// Fill `dest` entirely with random data.
///
/// This method provides a convenient way to fill a slice with random data.
/// Fill `dest` entirely with random bytes, where `dest` is any type
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
/// types (`i8`, `i16`, `u32`, etc.).
///
/// On big-endian platforms this performs byte-swapping to ensure
/// portability of results from reproducible generators.
///
/// Errors are forwarded from their source. In some cases errors may be
/// resolvable; see [`ErrorKind`] and documentation for the RNG in use.
/// This uses [`try_fill_bytes`] internally and forwards all RNG errors. In
/// some cases errors may be resolvable; see [`ErrorKind`] and
/// documentation for the RNG in use. If you do not plan to handle these
/// errors you may prefer to use [`fill`].
///
/// # Example
///
Expand All @@ -531,6 +539,9 @@ pub trait Rng {
/// ```
///
/// [`ErrorKind`]: enum.ErrorKind.html
/// [`try_fill_bytes`]: trait.Rng.html#method.try_fill_bytes
/// [`fill`]: trait.Rng.html#method.fill
/// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
self.try_fill_bytes(dest.as_byte_slice_mut())?;
dest.to_le();
Expand Down Expand Up @@ -757,6 +768,11 @@ impl<R: ?Sized> Rng for Box<R> where R: Rng {
}

/// Trait for casting types to byte slices
///
/// This is used by the [`fill`] and [`try_fill`] methods.
///
/// [`fill`]: trait.Rng.html#method.fill
/// [`try_fill`]: trait.Rng.html#method.try_fill
pub trait AsByteSliceMut {
/// Return a mutable reference to self as a byte slice
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8];
Expand Down

0 comments on commit 48f076c

Please sign in to comment.