@@ -112,30 +112,18 @@ mod iterator {
112112 }
113113
114114 #[ inline]
115- fn filter_compressed_trait_clause_helper < I , P , S , T , O > (
116- filter : & TraitClauses < I , P , S , T , O > ,
115+ fn filter_compressed_trait_helper < I , T > (
116+ trait_filter : & T ,
117+ cx : I ,
117118 self_ty : I :: Ty ,
118119 trait_ref_single_bit : CompressedTraitRefs ,
119120 ) -> Option < Binder < I , TraitClause < I > > >
120121 where
121122 I : Interner ,
122- P : PolarityFilter ,
123- S : SelfTyFilter < I > ,
124123 T : TraitFilter < I > ,
125- O : OutputAs < I > ,
126124 {
127- let & TraitClauses {
128- cx,
129- ref polarity_filter,
130- ref self_type_filter,
131- ref trait_filter,
132- output : _,
133- } = filter;
134- // All compressed clauses are positive clauses, so filter negatives out.
135- if polarity_filter. allow_polarity ( ClausePolarity :: Positive )
136- // Match the self ty, of course.
137- && self_type_filter. allow_self_ty ( self_ty)
138- && let Some ( def_id) = trait_filter. allow_compressed_trait_ref_single_bit ( cx, trait_ref_single_bit)
125+ if let Some ( def_id) =
126+ trait_filter. allow_compressed_trait_ref_single_bit ( cx, trait_ref_single_bit)
139127 {
140128 // Only if all filtering allows this compressed clause do we reconstruct the trait clause,
141129 // but we don't intern it yet, since that depends on the output transformation. If we can
@@ -146,8 +134,21 @@ mod iterator {
146134 }
147135 }
148136
149- impl < I , Iter , P , S , T , O > CallerBoundsIterator < I , Iter >
150- for CallerBoundsAccessor < I , Iter , TraitClauses < I , P , S , T , O > >
137+ struct CallerBoundsIteratorImpl < I , Iter , CF >
138+ where
139+ I : Interner ,
140+ Iter : Iterator < Item = I :: Clause > ,
141+ CF : ClauseFilter ,
142+ {
143+ accessor : CallerBoundsAccessor < I , Iter , CF > ,
144+ decompression_in_progress : CompressedTraitRefs ,
145+ // self ty of the currently decompression-in-progress trait ref,
146+ // if applicable.
147+ self_ty : Option < I :: Ty > ,
148+ }
149+
150+ impl < I , Iter , P , S , T , O > Iterator
151+ for CallerBoundsIteratorImpl < I , Iter , TraitClauses < I , P , S , T , O > >
151152 where
152153 I : Interner ,
153154 Iter : Iterator < Item = I :: Clause > ,
@@ -158,33 +159,81 @@ mod iterator {
158159 {
159160 type Item = O :: Output ;
160161
161- #[ inline]
162- fn iter ( self ) -> impl Iterator < Item = Self :: Item > {
163- let Self { clauses, clause_filter : filter, cx : _ } = self ;
162+ fn next ( & mut self ) -> Option < Self :: Item > {
163+ let CallerBoundsAccessor { clauses, clause_filter : filter, cx : _ } = & mut self . accessor ;
164164 let cx = filter. cx ;
165- clauses
166- . flat_map ( move |clause| {
165+
166+ // This loop finds the next item in `clauses` that can be returned.
167+ // If `clauses` is empty, we bail out of the function inside the loop,
168+ // otherwise the next non-filtered element is returned using `break`,
169+ // expanding compressed trait refs.
170+ let res = loop {
171+ // First check whether we're still expanding some compressed trait,
172+ // and make one step of progress towards it if we can.
173+ // `self.compressed_elem` is `0` when we're not expanding a compressed trait,
174+ // and in that case this function returns `None`
175+ if let Some ( trait_ref_single_bit) =
176+ self . decompression_in_progress . get_and_mask_least_significant_bit ( )
177+ {
178+ match filter_compressed_trait_helper (
179+ & filter. trait_filter ,
180+ cx,
181+ self . self_ty . unwrap ( ) ,
182+ trait_ref_single_bit,
183+ ) {
184+ Some ( tc) => break tc,
185+ None => continue ,
186+ }
187+ } else {
188+ // Only if we indeed are not, do we check the next item in the iterator.
189+ // If exhausted, we also return None.
190+ let clause = clauses. next ( ) ?;
167191 let kind = clause. kind ( ) ;
168192 match kind. skip_binder ( ) {
193+ // If we find a trait clause, and it matches, return it
169194 ClauseKind :: Trait ( trait_clause)
170195 if filter_trait_clause_helper ( & filter, trait_clause) =>
171196 {
172- // Use either to allow returning two kinds of iterators.
173- Either :: Left ( Some ( kind. rebind ( trait_clause) ) . into_iter ( ) )
197+ break kind. rebind ( trait_clause) ;
198+ }
199+ // If we find a compressed trait clause, and it matches, store it in `self.compressed_elem`
200+ ClauseKind :: CompressedTrait ( ct)
201+ if filter. self_type_filter . allow_self_ty ( ct. self_ty )
202+ && filter
203+ . polarity_filter
204+ . allow_polarity ( ClausePolarity :: Positive ) =>
205+ {
206+ self . decompression_in_progress = ct. compressed_trait_refs ;
207+ self . self_ty = Some ( ct. self_ty ) ;
174208 }
175- ClauseKind :: CompressedTrait ( ct) => Either :: Right (
176- ct. compressed_trait_refs
177- . iter ( )
178- . filter_map ( |i| {
179- filter_compressed_trait_clause_helper ( & filter, ct. self_ty , i)
180- } )
181- . collect :: < Vec < _ > > ( )
182- . into_iter ( ) ,
183- ) ,
184- _ => Either :: Left ( None . into_iter ( ) ) ,
209+ _ => continue ,
185210 }
186- } )
187- . map ( move |i| O :: construct_from_trait_clause ( i, cx) )
211+ }
212+ } ;
213+
214+ Some ( O :: construct_from_trait_clause ( res, cx) )
215+ }
216+ }
217+
218+ impl < I , Iter , P , S , T , O > CallerBoundsIterator < I , Iter >
219+ for CallerBoundsAccessor < I , Iter , TraitClauses < I , P , S , T , O > >
220+ where
221+ I : Interner ,
222+ Iter : Iterator < Item = I :: Clause > ,
223+ P : PolarityFilter ,
224+ S : SelfTyFilter < I > ,
225+ T : TraitFilter < I > ,
226+ O : OutputAs < I > ,
227+ {
228+ type Item = O :: Output ;
229+
230+ #[ inline]
231+ fn iter ( self ) -> impl Iterator < Item = Self :: Item > {
232+ CallerBoundsIteratorImpl {
233+ accessor : self ,
234+ decompression_in_progress : CompressedTraitRefs :: empty ( ) ,
235+ self_ty : None ,
236+ }
188237 }
189238 }
190239}
0 commit comments