Skip to main content

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)).unwrap_or_else(|e| match e {})
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}
38
39#[cfg(test)]
40mod tests {
41    use crate::{
42        typenum::{U0, U4},
43        GenericArray,
44    };
45    use arbitrary::{Arbitrary, Unstructured};
46
47    #[test]
48    fn arbitrary_and_take_rest() {
49        let data = [1u8, 2, 3, 4, 5, 6, 7, 8];
50
51        let mut u = Unstructured::new(&data);
52        let a = GenericArray::<u8, U4>::arbitrary(&mut u).unwrap();
53        assert_eq!(a.len(), 4);
54
55        let u = Unstructured::new(&data);
56        let b = GenericArray::<u8, U4>::arbitrary_take_rest(u).unwrap();
57        assert_eq!(b.len(), 4);
58
59        // zero-length array: `last_mut()` is None, exercising the empty branch
60        // of `arbitrary_take_rest`.
61        let u = Unstructured::new(&data);
62        let z = GenericArray::<u8, U0>::arbitrary_take_rest(u).unwrap();
63        assert_eq!(z.len(), 0);
64    }
65
66    #[test]
67    fn size_hints() {
68        // four `u8`s, each contributing (1, Some(1))
69        let (lo, hi) = <GenericArray<u8, U4> as Arbitrary>::size_hint(0);
70        assert_eq!((lo, hi), (4, Some(4)));
71
72        let hint = <GenericArray<u8, U4> as Arbitrary>::try_size_hint(0).unwrap();
73        assert_eq!(hint, (4, Some(4)));
74    }
75}