Skip to content

Commit 99a3b7c

Browse files
committed
Improve Rng method documentation
1 parent 1d3439a commit 99a3b7c

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/lib.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,27 @@ pub trait Rand : Sized {
339339

340340
/// A random number generator.
341341
pub trait Rng {
342-
/// Return the next random u32.
342+
/// Return the next random `u32`.
343+
///
344+
/// Implementations of this trait must implement at least one of
345+
/// `next_u32`, `next_u64` and `fill_bytes` directly. In the case this
346+
/// function is not implemented directly, it can be implemented using
347+
/// `self.next_u64() as u32` or via `fill_bytes` (TODO: expose helper
348+
/// function).
343349
fn next_u32(&mut self) -> u32;
344350

345-
/// Return the next random u64.
351+
/// Return the next random `u64`.
352+
///
353+
/// Implementations of this trait must implement at least one of
354+
/// `next_u32`, `next_u64` and `fill_bytes` directly. In the case this
355+
/// function is not implemented directly, the default implementation will
356+
/// generate values via `next_u32` in little-endian fashion, or this
357+
/// function can be implemented via `fill_bytes` (TODO: expose helper
358+
/// function).
346359
///
347-
/// This function has a default implementation of `next_u32`. The
348-
/// default implementation should not be used in wrapper types since the
349-
/// wrapped RNG may have its own implementation which may be more efficient
350-
/// or even produce different results.
360+
/// Types wrapping an inner RNG must not use the default implementation,
361+
/// since the inner RNG's implementation may produce different values.
351362
fn next_u64(&mut self) -> u64 {
352-
// TODO: remove default implementation once impls module is exposed
353363
impls::next_u64_via_u32(self)
354364
}
355365

@@ -407,17 +417,21 @@ pub trait Rng {
407417

408418
/// Fill `dest` with random data.
409419
///
410-
/// This function has a default implementation in terms of `next_u64`. The
411-
/// default implementation should not be used in wrapper types since the
412-
/// wrapped RNG may have its own implementation which may be more efficient
413-
/// or even produce different results.
414-
///
415-
/// This method does *not* have a requirement to bear any fixed
416-
/// relationship to the other methods, for example, it does *not*
417-
/// have to result in the same output as progressively filling
418-
/// `dest` with `self.gen::<u8>()`, and any such behaviour should
419-
/// not be relied upon.
420-
///
420+
/// Implementations of this trait must implement at least one of
421+
/// `next_u32`, `next_u64` and `fill_bytes` directly. In the case this
422+
/// function is not implemented directly, the default implementation will
423+
/// generate values via `next_u64` in little-endian fashion.
424+
/// (TODO: expose helper function to allow implementation via `next_u32`.)
425+
///
426+
/// There is no requirement on how this method generates values relative to
427+
/// `next_u32` or `next_u64`; e.g. a `u64` cast to bytes is not required to
428+
/// have the same value as eight bytes filled via this function. There *is*
429+
/// a requirement of portability for reproducible generators which implies
430+
/// that any seedable generator must fix endianness when generating bytes.
431+
///
432+
/// Types wrapping an inner RNG must not use the default implementation,
433+
/// since the inner RNG's implementation may produce different values.
434+
///
421435
/// This method should guarantee that `dest` is entirely filled
422436
/// with new data, and may panic if this is impossible
423437
/// (e.g. reading past the end of a file that is being used as the
@@ -433,7 +447,6 @@ pub trait Rng {
433447
/// println!("{:?}", &v[..]);
434448
/// ```
435449
fn fill_bytes(&mut self, dest: &mut [u8]) {
436-
// TODO: remove default implementation once impls module is exposed
437450
impls::fill_bytes_via_u64(self, dest)
438451
}
439452

0 commit comments

Comments
 (0)