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