Skip to content

Commit

Permalink
Impl Fill for slices over bool, char, f32, f64
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Feb 28, 2020
1 parent eccd1f8 commit d66d4d1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/rng.rs
Expand Up @@ -303,6 +303,26 @@ pub trait Fill {
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), Error>;
}

macro_rules! impl_fill_each {
() => {};
($t:ty) => {
impl Fill for [$t] {
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), Error> {
for elt in self.iter_mut() {
*elt = rng.gen();
}
Ok(())
}
}
};
($t:ty, $($tt:ty,)*) => {
impl_fill_each!($t);
impl_fill_each!($($tt,)*);
};
}

impl_fill_each!(bool, char, f32, f64,);

impl Fill for [u8] {
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), Error> {
rng.try_fill_bytes(self)
Expand Down Expand Up @@ -435,6 +455,12 @@ mod test {
rng.fill(&mut warray[..]);
assert_eq!(array[0], warray[0].0);
assert_eq!(array[1], warray[1].0);

// Check equivalence for generated floats
let mut array = [0f32; 2];
rng.fill(&mut array);
let gen: [f32; 2] = rng.gen();
assert_eq!(array, gen);
}

#[test]
Expand Down

0 comments on commit d66d4d1

Please sign in to comment.