sized_chunks/
arbitrary.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use bitmaps::{Bits, BitsImpl};
6
7use ::arbitrary::{size_hint, Arbitrary, Result, Unstructured};
8
9use crate::{Chunk, InlineArray, SparseChunk};
10
11#[cfg(feature = "ringbuffer")]
12use crate::RingBuffer;
13
14impl<'a, A, const N: usize> Arbitrary<'a> for Chunk<A, N>
15where
16    A: Arbitrary<'a>,
17    BitsImpl<N>: Bits,
18{
19    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
20        u.arbitrary_iter()?.take(Self::CAPACITY).collect()
21    }
22
23    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
24        u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect()
25    }
26
27    fn size_hint(depth: usize) -> (usize, Option<usize>) {
28        size_hint::recursion_guard(depth, |depth| {
29            let (_, upper) = A::size_hint(depth);
30            (0, upper.map(|upper| upper * Self::CAPACITY))
31        })
32    }
33}
34
35#[cfg(feature = "ringbuffer")]
36impl<'a, A, const N: usize> Arbitrary<'a> for RingBuffer<A, N>
37where
38    A: Arbitrary<'a>,
39    BitsImpl<N>: Bits,
40{
41    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
42        u.arbitrary_iter()?.take(Self::CAPACITY).collect()
43    }
44
45    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
46        u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect()
47    }
48
49    fn size_hint(depth: usize) -> (usize, Option<usize>) {
50        size_hint::recursion_guard(depth, |depth| {
51            let (_, upper) = A::size_hint(depth);
52            (0, upper.map(|upper| upper * Self::CAPACITY))
53        })
54    }
55}
56
57impl<'a, A, const N: usize> Arbitrary<'a> for SparseChunk<A, N>
58where
59    A: Clone,
60    Option<A>: Arbitrary<'a>,
61    BitsImpl<N>: Bits,
62{
63    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
64        u.arbitrary_iter()?.take(Self::CAPACITY).collect()
65    }
66
67    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
68        u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect()
69    }
70
71    fn size_hint(depth: usize) -> (usize, Option<usize>) {
72        size_hint::recursion_guard(depth, |depth| {
73            let (_, upper) = Option::<A>::size_hint(depth);
74            (0, upper.map(|upper| upper * Self::CAPACITY))
75        })
76    }
77}
78
79impl<'a, A, T> Arbitrary<'a> for InlineArray<A, T>
80where
81    A: Arbitrary<'a>,
82    T: 'static,
83{
84    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
85        u.arbitrary_iter()?.take(Self::CAPACITY).collect()
86    }
87
88    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
89        u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect()
90    }
91
92    fn size_hint(depth: usize) -> (usize, Option<usize>) {
93        size_hint::recursion_guard(depth, |depth| {
94            let (_, upper) = A::size_hint(depth);
95            (0, upper.map(|upper| upper * Self::CAPACITY))
96        })
97    }
98}