@@ -1260,6 +1260,56 @@ impl<I, F, T, U, E> Iterator for FilterMapOk<I, F>
1260
1260
} ) . collect ( )
1261
1261
}
1262
1262
}
1263
+
1264
+ /// An iterator adapter to apply a fallible transformation within a nested `Result`.
1265
+ ///
1266
+ /// See [`.map_and_then()`](../trait.Itertools.html#method.map_and_then) for more information.
1267
+ #[ derive( Clone ) ]
1268
+ #[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
1269
+ pub struct MapAndThen < I , F > {
1270
+ iter : I ,
1271
+ f : F
1272
+ }
1273
+
1274
+ /// Create a new `MapAndThen` iterator.
1275
+ pub fn map_and_then < I , F , T , U , E > ( iter : I , f : F ) -> MapAndThen < I , F >
1276
+ where I : Iterator < Item = Result < T , E > > ,
1277
+ F : FnMut ( T ) -> Result < U , E >
1278
+ {
1279
+ MapAndThen {
1280
+ iter : iter,
1281
+ f : f,
1282
+ }
1283
+ }
1284
+
1285
+ impl < I , F , T , U , E > Iterator for MapAndThen < I , F >
1286
+ where I : Iterator < Item = Result < T , E > > ,
1287
+ F : FnMut ( T ) -> Result < U , E >
1288
+ {
1289
+ type Item = Result < U , E > ;
1290
+
1291
+ fn next ( & mut self ) -> Option < Self :: Item > {
1292
+ self . iter . next ( ) . map ( |v| v. and_then ( & mut self . f ) )
1293
+ }
1294
+
1295
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1296
+ self . iter . size_hint ( )
1297
+ }
1298
+
1299
+ fn fold < Acc , Fold > ( self , init : Acc , mut fold_f : Fold ) -> Acc
1300
+ where Fold : FnMut ( Acc , Self :: Item ) -> Acc ,
1301
+ {
1302
+ let mut f = self . f ;
1303
+ self . iter . fold ( init, move |acc, v| fold_f ( acc, v. and_then ( & mut f) ) )
1304
+ }
1305
+
1306
+ fn collect < C > ( self ) -> C
1307
+ where C : FromIterator < Self :: Item >
1308
+ {
1309
+ let mut f = self . f ;
1310
+ self . iter . map ( move |v| v. and_then ( & mut f) ) . collect ( )
1311
+ }
1312
+ }
1263
1313
1264
1314
/// An iterator adapter to get the positions of each element that matches a predicate.
1265
1315
///
0 commit comments