From 1df7cb69dd50eb79abae153cdfe2523c1183a232 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sun, 23 May 2021 11:50:34 +1000 Subject: [PATCH] arbitrary: add fill_slice method for AsMut<[T: Arbitrary]> 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 --- src/arbitrary.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/arbitrary.rs b/src/arbitrary.rs index 92f893b..3136af3 100644 --- a/src/arbitrary.rs +++ b/src/arbitrary.rs @@ -62,6 +62,16 @@ impl Gen { slice.choose(&mut self.rng) } + /// Fill a mutable slice of any Arbitrary-compatible type with Arbitrary + /// values. + pub fn fill_slice(&mut self, mut slice: S) + where + T: Arbitrary, + S: AsMut<[T]>, + { + slice.as_mut().fill_with(|| T::arbitrary(self)) + } + fn gen(&mut self) -> T where rand::distributions::Standard: rand::distributions::Distribution,