@@ -193,6 +193,16 @@ pub trait StreamingIterator {
193
193
}
194
194
}
195
195
196
+ /// Creates a regular, non-streaming iterator which both filters and maps by applying a closure to elements.
197
+ #[ inline]
198
+ fn filter_map_deref < B , F > ( self , f : F ) -> FilterMapDeref < Self , F >
199
+ where
200
+ Self : Sized ,
201
+ F : FnMut ( & Self :: Item ) -> Option < B > ,
202
+ {
203
+ FilterMapDeref { it : self , f }
204
+ }
205
+
196
206
/// Returns the first element of the iterator that satisfies the predicate.
197
207
#[ inline]
198
208
fn find < F > ( & mut self , mut f : F ) -> Option < & Self :: Item >
@@ -255,6 +265,16 @@ pub trait StreamingIterator {
255
265
}
256
266
}
257
267
268
+ /// Creates a regular, non-streaming iterator which transforms elements of this iterator by passing them to a closure.
269
+ #[ inline]
270
+ fn map_deref < B , F > ( self , f : F ) -> MapDeref < Self , F >
271
+ where
272
+ Self : Sized ,
273
+ F : FnMut ( & Self :: Item ) -> B ,
274
+ {
275
+ MapDeref { it : self , f }
276
+ }
277
+
258
278
/// Creates an iterator which transforms elements of this iterator by passing them to a closure.
259
279
///
260
280
/// Unlike `map`, this method takes a closure that returns a reference into the original value.
@@ -1013,6 +1033,47 @@ where
1013
1033
}
1014
1034
}
1015
1035
1036
+ /// An regular, non-streaming iterator which both filters and maps elements of a streaming iterator with a closure.
1037
+ #[ derive( Debug ) ]
1038
+ pub struct FilterMapDeref < I , F > {
1039
+ it : I ,
1040
+ f : F ,
1041
+ }
1042
+
1043
+ impl < I , B , F > Iterator for FilterMapDeref < I , F >
1044
+ where
1045
+ I : StreamingIterator ,
1046
+ F : FnMut ( & I :: Item ) -> Option < B > ,
1047
+ {
1048
+ type Item = B ;
1049
+
1050
+ fn next ( & mut self ) -> Option < Self :: Item > {
1051
+ while let Some ( item) = self . it . next ( ) {
1052
+ if let Some ( mapped) = ( self . f ) ( item) {
1053
+ return Some ( mapped) ;
1054
+ }
1055
+ }
1056
+
1057
+ None
1058
+ }
1059
+ }
1060
+
1061
+ impl < I , B , F > DoubleEndedIterator for FilterMapDeref < I , F >
1062
+ where
1063
+ I : DoubleEndedStreamingIterator ,
1064
+ F : FnMut ( & I :: Item ) -> Option < B > ,
1065
+ {
1066
+ fn next_back ( & mut self ) -> Option < B > {
1067
+ while let Some ( item) = self . it . next_back ( ) {
1068
+ if let Some ( mapped) = ( self . f ) ( item) {
1069
+ return Some ( mapped) ;
1070
+ }
1071
+ }
1072
+
1073
+ None
1074
+ }
1075
+ }
1076
+
1016
1077
#[ derive( Copy , Clone , Debug ) ]
1017
1078
enum FuseState {
1018
1079
Start ,
@@ -1213,6 +1274,42 @@ where
1213
1274
}
1214
1275
}
1215
1276
1277
+ /// A regular, non-streaming iterator which transforms the elements of a streaming iterator.
1278
+ #[ derive( Debug ) ]
1279
+ pub struct MapDeref < I , F > {
1280
+ it : I ,
1281
+ f : F ,
1282
+ }
1283
+
1284
+ impl < I , B , F > Iterator for MapDeref < I , F >
1285
+ where
1286
+ I : StreamingIterator ,
1287
+ F : FnMut ( & I :: Item ) -> B ,
1288
+ {
1289
+ type Item = B ;
1290
+
1291
+ #[ inline]
1292
+ fn next ( & mut self ) -> Option < Self :: Item > {
1293
+ self . it . next ( ) . map ( & mut self . f )
1294
+ }
1295
+
1296
+ #[ inline]
1297
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1298
+ self . it . size_hint ( )
1299
+ }
1300
+ }
1301
+
1302
+ impl < I , B , F > DoubleEndedIterator for MapDeref < I , F >
1303
+ where
1304
+ I : DoubleEndedStreamingIterator ,
1305
+ F : FnMut ( & I :: Item ) -> B ,
1306
+ {
1307
+ #[ inline]
1308
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
1309
+ self . it . next_back ( ) . map ( & mut self . f )
1310
+ }
1311
+ }
1312
+
1216
1313
/// A streaming iterator which transforms the elements of a streaming iterator.
1217
1314
#[ derive( Debug ) ]
1218
1315
pub struct MapRef < I , F > {
@@ -1611,6 +1708,17 @@ mod test {
1611
1708
assert_eq ! ( it. get( ) , None ) ;
1612
1709
}
1613
1710
1711
+ fn test_deref < I > ( mut it : I , expected : & [ I :: Item ] )
1712
+ where
1713
+ I : Iterator ,
1714
+ I :: Item : Sized + PartialEq + Debug ,
1715
+ {
1716
+ for item in expected {
1717
+ assert_eq ! ( it. next( ) . as_ref( ) , Some ( item) ) ;
1718
+ }
1719
+ assert_eq ! ( it. next( ) , None )
1720
+ }
1721
+
1614
1722
#[ test]
1615
1723
fn all ( ) {
1616
1724
let items = [ 0 , 1 , 2 ] ;
@@ -1769,6 +1877,13 @@ mod test {
1769
1877
test ( it, & items) ;
1770
1878
}
1771
1879
1880
+ #[ test]
1881
+ fn map_deref ( ) {
1882
+ let items = [ 0 , 1 ] ;
1883
+ let it = convert ( items. iter ( ) . map ( |& i| i as usize ) ) . map_deref ( |& i| i as i32 ) ;
1884
+ test_deref ( it, & items) ;
1885
+ }
1886
+
1772
1887
#[ test]
1773
1888
fn map_ref ( ) {
1774
1889
#[ derive( Clone ) ]
@@ -1803,6 +1918,14 @@ mod test {
1803
1918
test ( it, & [ 0 , 2 , 4 ] )
1804
1919
}
1805
1920
1921
+ #[ test]
1922
+ fn filter_map_deref ( ) {
1923
+ let items = [ 0u8 , 1 , 1 , 2 , 4 ] ;
1924
+ let it =
1925
+ convert ( items. iter ( ) ) . filter_map_deref ( |& & i| if i % 2 == 0 { Some ( i) } else { None } ) ;
1926
+ test_deref ( it, & [ 0 , 2 , 4 ] )
1927
+ }
1928
+
1806
1929
#[ test]
1807
1930
fn find ( ) {
1808
1931
let items = [ 0 , 1 ] ;
0 commit comments