arbitrary/foreign/core/
result.rs1use crate::{size_hint, Arbitrary, Error, MaxRecursionReached, Unstructured};
2
3impl<'a, T, E> Arbitrary<'a> for Result<T, E>
4where
5 T: Arbitrary<'a>,
6 E: Arbitrary<'a>,
7{
8 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, Error> {
9 Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? {
10 Ok(<T as Arbitrary>::arbitrary(u)?)
11 } else {
12 Err(<E as Arbitrary>::arbitrary(u)?)
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>::size_hint(depth),
25 size_hint::or(
26 <T as Arbitrary>::try_size_hint(depth)?,
27 <E as Arbitrary>::try_size_hint(depth)?,
28 ),
29 ))
30 }
31}