• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::{size_hint, Arbitrary, MaxRecursionReached, Result, Unstructured};
2 
3 impl<'a, A> Arbitrary<'a> for Option<A>
4 where
5     A: Arbitrary<'a>,
6 {
arbitrary(u: &mut Unstructured<'a>) -> Result<Self>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]
size_hint(depth: usize) -> (usize, Option<usize>)16     fn size_hint(depth: usize) -> (usize, Option<usize>) {
17         Self::try_size_hint(depth).unwrap_or_default()
18     }
19 
20     #[inline]
try_size_hint(depth: usize) -> Result<(usize, Option<usize>), MaxRecursionReached>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 }
28