Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-introduce Rng::gen_iter #1305

Merged
merged 1 commit into from
May 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ pub trait Rng: RngCore {
range.sample_single(self).unwrap()
}

/// Generate values via an iterator
///
/// This is a just a wrapper over [`Rng::sample_iter`] using
/// [`distributions::Standard`].
///
/// Note: this method consumes its argument. Use
/// `(&mut rng).gen_iter()` to avoid consuming the RNG.
///
/// # Example
///
/// ```
/// use rand::{rngs::mock::StepRng, Rng};
///
/// let rng = StepRng::new(1, 1);
/// let v: Vec<i32> = rng.gen_iter().take(5).collect();
/// assert_eq!(&v, &[1, 2, 3, 4, 5]);
/// ```
#[inline]
fn gen_iter<T>(self) -> distributions::DistIter<Standard, Self, T>
where
Self: Sized,
Standard: Distribution<T>,
{
Standard.sample_iter(self)
}

/// Sample a new value, using the given distribution.
///
/// ### Example
Expand All @@ -153,11 +179,8 @@ pub trait Rng: RngCore {

/// Create an iterator that generates values using the given distribution.
///
/// Note that this function takes its arguments by value. This works since
/// `(&mut R): Rng where R: Rng` and
/// `(&D): Distribution where D: Distribution`,
/// however borrowing is not automatic hence `rng.sample_iter(...)` may
/// need to be replaced with `(&mut rng).sample_iter(...)`.
/// Note: this method consumes its arguments. Use
/// `(&mut rng).sample_iter(..)` to avoid consuming the RNG.
///
/// # Example
///
Expand Down