1#[cfg(feature = "arbitrary")]
2#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
3mod impl_arbitrary {
4 use crate::{IndexMap, IndexSet};
5 use arbitrary::{Arbitrary, Result, Unstructured};
6 use core::hash::{BuildHasher, Hash};
7
8 impl<'a, K, V, S> Arbitrary<'a> for IndexMap<K, V, S>
9 where
10 K: Arbitrary<'a> + Hash + Eq,
11 V: Arbitrary<'a>,
12 S: BuildHasher + Default,
13 {
14 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
15 u.arbitrary_iter()?.collect()
16 }
17
18 fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
19 u.arbitrary_take_rest_iter()?.collect()
20 }
21 }
22
23 impl<'a, T, S> Arbitrary<'a> for IndexSet<T, S>
24 where
25 T: Arbitrary<'a> + Hash + Eq,
26 S: BuildHasher + Default,
27 {
28 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
29 u.arbitrary_iter()?.collect()
30 }
31
32 fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
33 u.arbitrary_take_rest_iter()?.collect()
34 }
35 }
36}
37
38#[cfg(feature = "quickcheck")]
39#[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
40mod impl_quickcheck {
41 use crate::{IndexMap, IndexSet};
42 use alloc::boxed::Box;
43 use alloc::vec::Vec;
44 use core::hash::{BuildHasher, Hash};
45 use quickcheck::{Arbitrary, Gen};
46
47 impl<K, V, S> Arbitrary for IndexMap<K, V, S>
48 where
49 K: Arbitrary + Hash + Eq,
50 V: Arbitrary,
51 S: BuildHasher + Default + Clone + 'static,
52 {
53 fn arbitrary(g: &mut Gen) -> Self {
54 Self::from_iter(Vec::arbitrary(g))
55 }
56
57 fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
58 let vec = Vec::from_iter(self.clone());
59 Box::new(vec.shrink().map(Self::from_iter))
60 }
61 }
62
63 impl<T, S> Arbitrary for IndexSet<T, S>
64 where
65 T: Arbitrary + Hash + Eq,
66 S: BuildHasher + Default + Clone + 'static,
67 {
68 fn arbitrary(g: &mut Gen) -> Self {
69 Self::from_iter(Vec::arbitrary(g))
70 }
71
72 fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
73 let vec = Vec::from_iter(self.clone());
74 Box::new(vec.shrink().map(Self::from_iter))
75 }
76 }
77}
78