arbitrary/foreign/alloc/
boxed.rs

1use {
2    crate::{size_hint, Arbitrary, Result, Unstructured},
3    std::boxed::Box,
4};
5
6impl<'a, A> Arbitrary<'a> for Box<A>
7where
8    A: Arbitrary<'a>,
9{
10    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
11        Arbitrary::arbitrary(u).map(Self::new)
12    }
13
14    #[inline]
15    fn size_hint(depth: usize) -> (usize, Option<usize>) {
16        Self::try_size_hint(depth).unwrap_or_default()
17    }
18
19    #[inline]
20    fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
21        size_hint::try_recursion_guard(depth, <A as Arbitrary>::try_size_hint)
22    }
23}
24
25impl<'a, A> Arbitrary<'a> for Box<[A]>
26where
27    A: Arbitrary<'a>,
28{
29    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
30        u.arbitrary_iter()?.collect()
31    }
32
33    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
34        u.arbitrary_take_rest_iter()?.collect()
35    }
36
37    #[inline]
38    fn size_hint(_depth: usize) -> (usize, Option<usize>) {
39        (0, None)
40    }
41}
42
43impl<'a> Arbitrary<'a> for Box<str> {
44    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
45        <String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str())
46    }
47
48    #[inline]
49    fn size_hint(depth: usize) -> (usize, Option<usize>) {
50        <String as Arbitrary>::size_hint(depth)
51    }
52}