@@ -1159,6 +1159,55 @@ impl<I, F, T, U, E> Iterator for MapResults<I, F>
1159
1159
}
1160
1160
}
1161
1161
1162
+ /// An iterator adapter to apply a fallible transformation within a nested `Result`.
1163
+ ///
1164
+ /// See [`.try_map_results()`](../trait.Itertools.html#method.try_map_results) for more information.
1165
+ #[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
1166
+ pub struct TryMapResults < I , F > {
1167
+ iter : I ,
1168
+ f : F
1169
+ }
1170
+
1171
+ /// Create a new `TryMapResults` iterator.
1172
+ pub fn try_map_results < I , F , T , U , E > ( iter : I , f : F ) -> TryMapResults < I , F >
1173
+ where I : Iterator < Item = Result < T , E > > ,
1174
+ F : FnMut ( T ) -> Result < U , E >
1175
+ {
1176
+ TryMapResults {
1177
+ iter : iter,
1178
+ f : f,
1179
+ }
1180
+ }
1181
+
1182
+ impl < I , F , T , U , E > Iterator for TryMapResults < I , F >
1183
+ where I : Iterator < Item = Result < T , E > > ,
1184
+ F : FnMut ( T ) -> Result < U , E >
1185
+ {
1186
+ type Item = Result < U , E > ;
1187
+
1188
+ fn next ( & mut self ) -> Option < Self :: Item > {
1189
+ self . iter . next ( ) . map ( |v| v. and_then ( & mut self . f ) )
1190
+ }
1191
+
1192
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1193
+ self . iter . size_hint ( )
1194
+ }
1195
+
1196
+ fn fold < Acc , Fold > ( self , init : Acc , mut fold_f : Fold ) -> Acc
1197
+ where Fold : FnMut ( Acc , Self :: Item ) -> Acc ,
1198
+ {
1199
+ let mut f = self . f ;
1200
+ self . iter . fold ( init, move |acc, v| fold_f ( acc, v. and_then ( & mut f) ) )
1201
+ }
1202
+
1203
+ fn collect < C > ( self ) -> C
1204
+ where C : FromIterator < Self :: Item >
1205
+ {
1206
+ let mut f = self . f ;
1207
+ self . iter . map ( move |v| v. and_then ( & mut f) ) . collect ( )
1208
+ }
1209
+ }
1210
+
1162
1211
/// An iterator adapter to get the positions of each element that matches a predicate.
1163
1212
///
1164
1213
/// See [`.positions()`](../trait.Itertools.html#method.positions) for more information.
0 commit comments