2323
2424mod all;
2525mod any;
26+ mod filter_map;
2627mod min_by;
2728mod next;
2829mod take;
@@ -31,6 +32,7 @@ pub use take::Take;
3132
3233use all:: AllFuture ;
3334use any:: AnyFuture ;
35+ use filter_map:: FilterMap ;
3436use min_by:: MinByFuture ;
3537use next:: NextFuture ;
3638
@@ -128,6 +130,45 @@ pub trait Stream {
128130 }
129131 }
130132
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+
131172 /// Returns the element that gives the minimum value with respect to the
132173 /// specified comparison function. If several elements are equally minimum,
133174 /// the first element is returned. If the stream is empty, `None` is returned.
0 commit comments