24
24
mod all;
25
25
mod any;
26
26
mod filter_map;
27
+ mod find;
27
28
mod find_map;
28
29
mod min_by;
29
30
mod next;
@@ -35,6 +36,7 @@ pub use take::Take;
35
36
use all:: AllFuture ;
36
37
use any:: AnyFuture ;
37
38
use filter_map:: FilterMap ;
39
+ use find:: FindFuture ;
38
40
use find_map:: FindMapFuture ;
39
41
use min_by:: MinByFuture ;
40
42
use next:: NextFuture ;
@@ -321,6 +323,50 @@ pub trait Stream {
321
323
}
322
324
}
323
325
326
+ /// Searches for an element in a stream that satisfies a predicate.
327
+ ///
328
+ /// # Examples
329
+ ///
330
+ /// Basic usage:
331
+ ///
332
+ /// ```
333
+ /// # fn main() { async_std::task::block_on(async {
334
+ /// #
335
+ /// use async_std::prelude::*;
336
+ /// use std::collections::VecDeque;
337
+ ///
338
+ /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
339
+ /// let res = s.find(|x| *x == 2).await;
340
+ /// assert_eq!(res, Some(2));
341
+ /// #
342
+ /// # }) }
343
+ /// ```
344
+ ///
345
+ /// Resuming after a first find:
346
+ ///
347
+ /// ```
348
+ /// # fn main() { async_std::task::block_on(async {
349
+ /// #
350
+ /// use async_std::prelude::*;
351
+ /// use std::collections::VecDeque;
352
+ ///
353
+ /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
354
+ /// let res = s.find(|x| *x == 2).await;
355
+ /// assert_eq!(res, Some(2));
356
+ ///
357
+ /// let next = s.next().await;
358
+ /// assert_eq!(next, Some(3));
359
+ /// #
360
+ /// # }) }
361
+ /// ```
362
+ fn find < P > ( & mut self , p : P ) -> ret ! ( ' _, FindFuture , Option <Self :: Item >, P , Self :: Item )
363
+ where
364
+ Self : Sized ,
365
+ P : FnMut ( & Self :: Item ) -> bool ,
366
+ {
367
+ FindFuture :: new ( self , p)
368
+ }
369
+
324
370
/// Applies function to the elements of stream and returns the first non-none result.
325
371
///
326
372
/// ```
0 commit comments