@@ -2180,137 +2180,65 @@ impl<I: ExactSizeIterator, F> ExactSizeIterator for Inspect<I, F>
2180
2180
impl < I : FusedIterator , F > FusedIterator for Inspect < I , F >
2181
2181
where F : FnMut ( & I :: Item ) { }
2182
2182
2183
- /// An iterator adapter that produces output as long as the underlying
2184
- /// iterator produces `Option::Some` values.
2185
- pub ( crate ) struct OptionShunt < I > {
2186
- iter : I ,
2187
- exited_early : bool ,
2188
- }
2189
-
2190
- impl < I , T > OptionShunt < I >
2191
- where
2192
- I : Iterator < Item = Option < T > > ,
2193
- {
2194
- /// Process the given iterator as if it yielded a `T` instead of a
2195
- /// `Option<T>`. Any `None` value will stop the inner iterator and
2196
- /// the overall result will be a `None`.
2197
- pub fn process < F , U > ( iter : I , mut f : F ) -> Option < U >
2198
- where
2199
- F : FnMut ( & mut Self ) -> U ,
2200
- {
2201
- let mut shunt = OptionShunt :: new ( iter) ;
2202
- let value = f ( shunt. by_ref ( ) ) ;
2203
- shunt. reconstruct ( value)
2204
- }
2205
-
2206
- fn new ( iter : I ) -> Self {
2207
- OptionShunt {
2208
- iter,
2209
- exited_early : false ,
2210
- }
2211
- }
2212
-
2213
- /// Consume the adapter and rebuild a `Option` value.
2214
- fn reconstruct < U > ( self , val : U ) -> Option < U > {
2215
- if self . exited_early {
2216
- None
2217
- } else {
2218
- Some ( val)
2219
- }
2220
- }
2221
- }
2222
-
2223
- impl < I , T > Iterator for OptionShunt < I >
2224
- where
2225
- I : Iterator < Item = Option < T > > ,
2226
- {
2227
- type Item = T ;
2228
-
2229
- fn next ( & mut self ) -> Option < Self :: Item > {
2230
- match self . iter . next ( ) {
2231
- Some ( Some ( v) ) => Some ( v) ,
2232
- Some ( None ) => {
2233
- self . exited_early = true ;
2234
- None
2235
- }
2236
- None => None ,
2237
- }
2238
- }
2239
-
2240
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2241
- if self . exited_early {
2242
- ( 0 , Some ( 0 ) )
2243
- } else {
2244
- let ( _, upper) = self . iter . size_hint ( ) ;
2245
- ( 0 , upper)
2246
- }
2247
- }
2248
- }
2249
-
2250
2183
/// An iterator adapter that produces output as long as the underlying
2251
2184
/// iterator produces `Result::Ok` values.
2252
2185
///
2253
2186
/// If an error is encountered, the iterator stops and the error is
2254
- /// stored. The error may be recovered later via `reconstruct`.
2255
- pub ( crate ) struct ResultShunt < I , E > {
2187
+ /// stored.
2188
+ pub ( crate ) struct ResultShunt < ' a , I , E > {
2256
2189
iter : I ,
2257
- error : Option < E > ,
2190
+ error : & ' a mut Result < ( ) , E > ,
2258
2191
}
2259
2192
2260
- impl < I , T , E > ResultShunt < I , E >
2261
- where I : Iterator < Item = Result < T , E > >
2193
+ /// Process the given iterator as if it yielded a `T` instead of a
2194
+ /// `Result<T, _>`. Any errors will stop the inner iterator and
2195
+ /// the overall result will be an error.
2196
+ pub ( crate ) fn process_results < I , T , E , F , U > ( iter : I , mut f : F ) -> Result < U , E >
2197
+ where
2198
+ I : Iterator < Item = Result < T , E > > ,
2199
+ for < ' a > F : FnMut ( ResultShunt < ' a , I , E > ) -> U ,
2262
2200
{
2263
- /// Process the given iterator as if it yielded a `T` instead of a
2264
- /// `Result<T, _>`. Any errors will stop the inner iterator and
2265
- /// the overall result will be an error.
2266
- pub fn process < F , U > ( iter : I , mut f : F ) -> Result < U , E >
2267
- where F : FnMut ( & mut Self ) -> U
2268
- {
2269
- let mut shunt = ResultShunt :: new ( iter) ;
2270
- let value = f ( shunt. by_ref ( ) ) ;
2271
- shunt. reconstruct ( value)
2272
- }
2273
-
2274
- fn new ( iter : I ) -> Self {
2275
- ResultShunt {
2276
- iter,
2277
- error : None ,
2278
- }
2279
- }
2280
-
2281
- /// Consume the adapter and rebuild a `Result` value. This should
2282
- /// *always* be called, otherwise any potential error would be
2283
- /// lost.
2284
- fn reconstruct < U > ( self , val : U ) -> Result < U , E > {
2285
- match self . error {
2286
- None => Ok ( val) ,
2287
- Some ( e) => Err ( e) ,
2288
- }
2289
- }
2201
+ let mut error = Ok ( ( ) ) ;
2202
+ let shunt = ResultShunt {
2203
+ iter,
2204
+ error : & mut error,
2205
+ } ;
2206
+ let value = f ( shunt) ;
2207
+ error. map ( |( ) | value)
2290
2208
}
2291
2209
2292
- impl < I , T , E > Iterator for ResultShunt < I , E >
2210
+ impl < I , T , E > Iterator for ResultShunt < ' _ , I , E >
2293
2211
where I : Iterator < Item = Result < T , E > >
2294
2212
{
2295
2213
type Item = T ;
2296
2214
2297
2215
fn next ( & mut self ) -> Option < Self :: Item > {
2298
- match self . iter . next ( ) {
2299
- Some ( Ok ( v) ) => Some ( v) ,
2300
- Some ( Err ( e) ) => {
2301
- self . error = Some ( e) ;
2302
- None
2303
- }
2304
- None => None ,
2305
- }
2216
+ self . find ( |_| true )
2306
2217
}
2307
2218
2308
2219
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2309
- if self . error . is_some ( ) {
2220
+ if self . error . is_err ( ) {
2310
2221
( 0 , Some ( 0 ) )
2311
2222
} else {
2312
2223
let ( _, upper) = self . iter . size_hint ( ) ;
2313
2224
( 0 , upper)
2314
2225
}
2315
2226
}
2227
+
2228
+ fn try_fold < B , F , R > ( & mut self , init : B , mut f : F ) -> R
2229
+ where
2230
+ F : FnMut ( B , Self :: Item ) -> R ,
2231
+ R : Try < Ok = B > ,
2232
+ {
2233
+ let error = & mut * self . error ;
2234
+ self . iter
2235
+ . try_fold ( init, |acc, x| match x {
2236
+ Ok ( x) => LoopState :: from_try ( f ( acc, x) ) ,
2237
+ Err ( e) => {
2238
+ * error = Err ( e) ;
2239
+ LoopState :: Break ( Try :: from_ok ( acc) )
2240
+ }
2241
+ } )
2242
+ . into_try ( )
2243
+ }
2316
2244
}
0 commit comments