Skip to content

Commit

Permalink
Add Gen::fill method
Browse files Browse the repository at this point in the history
This allows users to fairly easily migrate from CoreRng::fill_byte() and
similar constructions without requiring them to fuss around with a new
SeedableRng (or copy-paste this boilerplate) when their slice is just
full of Arbitrary-able types.

Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar authored and cloudhead committed Dec 3, 2022
1 parent 2d4ba3b commit 01abd13
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ impl Gen {
slice.choose(&mut self.rng)
}

/// Fill a mutable slice of any Arbitrary-compatible type with Arbitrary
/// values.
pub fn fill<S, T>(&mut self, mut slice: S)
where
T: Arbitrary,
S: AsMut<[T]>,
{
slice.as_mut().fill_with(|| T::arbitrary(self))
}

fn gen<T>(&mut self) -> T
where
rand::distributions::Standard: rand::distributions::Distribution<T>,
Expand Down

0 comments on commit 01abd13

Please sign in to comment.