23
23
24
24
mod all;
25
25
mod any;
26
+ mod filter_map;
26
27
mod min_by;
27
28
mod next;
28
29
mod take;
@@ -31,6 +32,7 @@ pub use take::Take;
31
32
32
33
use all:: AllFuture ;
33
34
use any:: AnyFuture ;
35
+ use filter_map:: FilterMap ;
34
36
use min_by:: MinByFuture ;
35
37
use next:: NextFuture ;
36
38
@@ -128,6 +130,45 @@ pub trait Stream {
128
130
}
129
131
}
130
132
133
+ /// Both filters and maps a stream.
134
+ ///
135
+ /// # Examples
136
+ ///
137
+ /// Basic usage:
138
+ ///
139
+ /// ```
140
+ ///
141
+ /// # fn main() { async_std::task::block_on(async {
142
+ /// #
143
+ /// use std::collections::VecDeque;
144
+ /// use async_std::stream::Stream;
145
+ ///
146
+ /// let s: VecDeque<&str> = vec!["1", "lol", "3", "NaN", "5"].into_iter().collect();
147
+ ///
148
+ /// let mut parsed = s.filter_map(|a| a.parse::<u32>().ok());
149
+ ///
150
+ /// let one = parsed.next().await;
151
+ /// assert_eq!(one, Some(1));
152
+ ///
153
+ /// let three = parsed.next().await;
154
+ /// assert_eq!(three, Some(3));
155
+ ///
156
+ /// let five = parsed.next().await;
157
+ /// assert_eq!(five, Some(5));
158
+ ///
159
+ /// let end = parsed.next().await;
160
+ /// assert_eq!(end, None);
161
+ ///
162
+ /// #
163
+ /// # }) }
164
+ fn filter_map < B , F > ( self , f : F ) -> FilterMap < Self , F , Self :: Item , B >
165
+ where
166
+ Self : Sized ,
167
+ F : FnMut ( Self :: Item ) -> Option < B > ,
168
+ {
169
+ FilterMap :: new ( self , f)
170
+ }
171
+
131
172
/// Returns the element that gives the minimum value with respect to the
132
173
/// specified comparison function. If several elements are equally minimum,
133
174
/// the first element is returned. If the stream is empty, `None` is returned.
0 commit comments