arbitrary/foreign/core/
option.rs

1use crate::{size_hint, Arbitrary, MaxRecursionReached, Result, Unstructured};
2
3impl<'a, A> Arbitrary<'a> for Option<A>
4where
5    A: Arbitrary<'a>,
6{
7    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
8        Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? {
9            Some(Arbitrary::arbitrary(u)?)
10        } else {
11            None
12        })
13    }
14
15    #[inline]
16    fn size_hint(depth: usize) -> (usize, Option<usize>) {
17        Self::try_size_hint(depth).unwrap_or_default()
18    }
19
20    #[inline]
21    fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), MaxRecursionReached> {
22        Ok(size_hint::and(
23            <bool as Arbitrary>::try_size_hint(depth)?,
24            size_hint::or((0, Some(0)), <A as Arbitrary>::try_size_hint(depth)?),
25        ))
26    }
27}