generic_array/ext_impls/
impl_arbitrary.rs

1use arbitrary::Arbitrary;
2
3use crate::{sequence::FallibleGenericSequence as _, ArrayLength, GenericArray};
4
5impl<'a, T, N: ArrayLength> Arbitrary<'a> for GenericArray<T, N>
6where
7    T: Arbitrary<'a>,
8{
9    #[inline]
10    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
11        GenericArray::try_generate(|_| T::arbitrary(u))
12    }
13
14    #[inline]
15    fn arbitrary_take_rest(mut u: arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
16        let mut array = Self::arbitrary(&mut u)?;
17        if let Some(last) = array.last_mut() {
18            *last = T::arbitrary_take_rest(u)?;
19        }
20        Ok(array)
21    }
22
23    fn size_hint(depth: usize) -> (usize, Option<usize>) {
24        Self::try_size_hint(depth).unwrap_or_default()
25    }
26
27    fn try_size_hint(
28        depth: usize,
29    ) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
30        let hint = <T as Arbitrary>::try_size_hint(depth)?;
31
32        // same as `arbitrary::size_hint::and_all(...)` but without allocations
33        Ok(core::iter::repeat(hint)
34            .take(N::USIZE)
35            .fold((0, Some(0)), arbitrary::size_hint::and))
36    }
37}