Skip to content

Commit

Permalink
feat: impl array for any length
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Aug 22, 2023
1 parent 7ab8b86 commit e29d173
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions fake/src/impls/std/array.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
use crate::{Dummy, Fake};
use rand::Rng;

macro_rules! array_impl {
{$n:expr, $t:ident $($ts:ident)*} => {
impl<T, U> Dummy<U> for [T; $n] where T: Dummy<U> {
fn dummy_with_rng<R: Rng + ?Sized>(config: &U, rng: &mut R) -> Self {
[Fake::fake_with_rng::<$t, _>(config, rng), $(Fake::fake_with_rng::<$ts, _>(config, rng)),*]
}
}
array_impl!{($n - 1), $($ts)*}
};
{$n:expr,} => {
impl<T, U> Dummy<U> for [T; $n] where T: Dummy<U> {
fn dummy(_: &U) -> Self {
[]
}

fn dummy_with_rng<R: Rng + ?Sized>(_: &U, _rng: &mut R) -> Self {
[]
}
}
};
impl<T, U, const N: usize> Dummy<U> for [T; N]
where
T: Dummy<U>,
{
fn dummy_with_rng<R: Rng + ?Sized>(config: &U, rng: &mut R) -> Self {
std::array::from_fn(|_| Fake::fake_with_rng::<T, _>(config, rng))
}
}

array_impl! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T}

0 comments on commit e29d173

Please sign in to comment.