33// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
55use std:: hash:: { BuildHasher , Hash } ;
6- use std:: iter;
76
87use :: arbitrary:: { size_hint, Arbitrary , Result , Unstructured } ;
98
109use crate :: { HashMap , HashSet , OrdMap , OrdSet , Vector } ;
1110
12- fn empty < T : ' static > ( ) -> Box < dyn Iterator < Item = T > > {
13- Box :: new ( iter:: empty ( ) )
14- }
15-
16- fn shrink_collection < T : Clone , A : Clone + Arbitrary > (
17- entries : impl Iterator < Item = T > ,
18- f : impl Fn ( & T ) -> Box < dyn Iterator < Item = A > > ,
19- ) -> Box < dyn Iterator < Item = Vec < A > > > {
20- let entries: Vec < _ > = entries. collect ( ) ;
21- if entries. is_empty ( ) {
22- return empty ( ) ;
23- }
24-
25- let mut shrinkers: Vec < Vec < _ > > = vec ! [ ] ;
26- let mut i = entries. len ( ) ;
27- loop {
28- shrinkers. push ( entries. iter ( ) . take ( i) . map ( & f) . collect ( ) ) ;
29- i /= 2 ;
30- if i == 0 {
31- break ;
32- }
33- }
34- Box :: new ( iter:: once ( Vec :: new ( ) ) . chain ( iter:: from_fn ( move || loop {
35- let mut shrinker = shrinkers. pop ( ) ?;
36- let x: Option < Vec < A > > = shrinker. iter_mut ( ) . map ( |s| s. next ( ) ) . collect ( ) ;
37- if x. is_none ( ) {
38- continue ;
39- }
40- shrinkers. push ( shrinker) ;
41- return x;
42- } ) ) )
43- }
44-
45- impl < A : Arbitrary + Clone > Arbitrary for Vector < A > {
46- fn arbitrary ( u : & mut Unstructured < ' _ > ) -> Result < Self > {
11+ impl < ' a , A : Arbitrary < ' a > + Clone > Arbitrary < ' a > for Vector < A > {
12+ fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
4713 u. arbitrary_iter ( ) ?. collect ( )
4814 }
4915
50- fn arbitrary_take_rest ( u : Unstructured < ' _ > ) -> Result < Self > {
16+ fn arbitrary_take_rest ( u : Unstructured < ' a > ) -> Result < Self > {
5117 u. arbitrary_take_rest_iter ( ) ?. collect ( )
5218 }
5319
@@ -56,19 +22,14 @@ impl<A: Arbitrary + Clone> Arbitrary for Vector<A> {
5622 size_hint:: and ( <usize as Arbitrary >:: size_hint ( depth) , ( 0 , None ) )
5723 } )
5824 }
59-
60- fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
61- let collections = shrink_collection ( self . iter ( ) , |x| x. shrink ( ) ) ;
62- Box :: new ( collections. map ( |entries| entries. into_iter ( ) . collect ( ) ) )
63- }
6425}
6526
66- impl < K : Arbitrary + Ord + Clone , V : Arbitrary + Clone > Arbitrary for OrdMap < K , V > {
67- fn arbitrary ( u : & mut Unstructured < ' _ > ) -> Result < Self > {
27+ impl < ' a , K : Arbitrary < ' a > + Ord + Clone , V : Arbitrary < ' a > + Clone > Arbitrary < ' a > for OrdMap < K , V > {
28+ fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
6829 u. arbitrary_iter ( ) ?. collect ( )
6930 }
7031
71- fn arbitrary_take_rest ( u : Unstructured < ' _ > ) -> Result < Self > {
32+ fn arbitrary_take_rest ( u : Unstructured < ' a > ) -> Result < Self > {
7233 u. arbitrary_take_rest_iter ( ) ?. collect ( )
7334 }
7435
@@ -77,20 +38,14 @@ impl<K: Arbitrary + Ord + Clone, V: Arbitrary + Clone> Arbitrary for OrdMap<K, V
7738 size_hint:: and ( <usize as Arbitrary >:: size_hint ( depth) , ( 0 , None ) )
7839 } )
7940 }
80-
81- fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
82- let collections =
83- shrink_collection ( self . iter ( ) , |( k, v) | Box :: new ( k. shrink ( ) . zip ( v. shrink ( ) ) ) ) ;
84- Box :: new ( collections. map ( |entries| entries. into_iter ( ) . collect ( ) ) )
85- }
8641}
8742
88- impl < A : Arbitrary + Ord + Clone > Arbitrary for OrdSet < A > {
89- fn arbitrary ( u : & mut Unstructured < ' _ > ) -> Result < Self > {
43+ impl < ' a , A : Arbitrary < ' a > + Ord + Clone > Arbitrary < ' a > for OrdSet < A > {
44+ fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
9045 u. arbitrary_iter ( ) ?. collect ( )
9146 }
9247
93- fn arbitrary_take_rest ( u : Unstructured < ' _ > ) -> Result < Self > {
48+ fn arbitrary_take_rest ( u : Unstructured < ' a > ) -> Result < Self > {
9449 u. arbitrary_take_rest_iter ( ) ?. collect ( )
9550 }
9651
@@ -99,24 +54,19 @@ impl<A: Arbitrary + Ord + Clone> Arbitrary for OrdSet<A> {
9954 size_hint:: and ( <usize as Arbitrary >:: size_hint ( depth) , ( 0 , None ) )
10055 } )
10156 }
102-
103- fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
104- let collections = shrink_collection ( self . iter ( ) , |v| v. shrink ( ) ) ;
105- Box :: new ( collections. map ( |entries| entries. into_iter ( ) . collect ( ) ) )
106- }
10757}
10858
109- impl < K , V , S > Arbitrary for HashMap < K , V , S >
59+ impl < ' a , K , V , S > Arbitrary < ' a > for HashMap < K , V , S >
11060where
111- K : Arbitrary + Hash + Eq + Clone ,
112- V : Arbitrary + Clone ,
61+ K : Arbitrary < ' a > + Hash + Eq + Clone ,
62+ V : Arbitrary < ' a > + Clone ,
11363 S : BuildHasher + Default + ' static ,
11464{
115- fn arbitrary ( u : & mut Unstructured < ' _ > ) -> Result < Self > {
65+ fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
11666 u. arbitrary_iter ( ) ?. collect ( )
11767 }
11868
119- fn arbitrary_take_rest ( u : Unstructured < ' _ > ) -> Result < Self > {
69+ fn arbitrary_take_rest ( u : Unstructured < ' a > ) -> Result < Self > {
12070 u. arbitrary_take_rest_iter ( ) ?. collect ( )
12171 }
12272
@@ -125,24 +75,18 @@ where
12575 size_hint:: and ( <usize as Arbitrary >:: size_hint ( depth) , ( 0 , None ) )
12676 } )
12777 }
128-
129- fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
130- let collections =
131- shrink_collection ( self . iter ( ) , |( k, v) | Box :: new ( k. shrink ( ) . zip ( v. shrink ( ) ) ) ) ;
132- Box :: new ( collections. map ( |entries| entries. into_iter ( ) . collect ( ) ) )
133- }
13478}
13579
136- impl < A , S > Arbitrary for HashSet < A , S >
80+ impl < ' a , A , S > Arbitrary < ' a > for HashSet < A , S >
13781where
138- A : Arbitrary + Hash + Eq + Clone ,
82+ A : Arbitrary < ' a > + Hash + Eq + Clone ,
13983 S : BuildHasher + Default + ' static ,
14084{
141- fn arbitrary ( u : & mut Unstructured < ' _ > ) -> Result < Self > {
85+ fn arbitrary ( u : & mut Unstructured < ' a > ) -> Result < Self > {
14286 u. arbitrary_iter ( ) ?. collect ( )
14387 }
14488
145- fn arbitrary_take_rest ( u : Unstructured < ' _ > ) -> Result < Self > {
89+ fn arbitrary_take_rest ( u : Unstructured < ' a > ) -> Result < Self > {
14690 u. arbitrary_take_rest_iter ( ) ?. collect ( )
14791 }
14892
15195 size_hint:: and ( <usize as Arbitrary >:: size_hint ( depth) , ( 0 , None ) )
15296 } )
15397 }
154-
155- fn shrink ( & self ) -> Box < dyn Iterator < Item = Self > > {
156- let collections = shrink_collection ( self . iter ( ) , |v| v. shrink ( ) ) ;
157- Box :: new ( collections. map ( |entries| entries. into_iter ( ) . collect ( ) ) )
158- }
15998}
0 commit comments