@@ -50,14 +50,15 @@ use min_by::MinByFuture;
5050use next:: NextFuture ;
5151use nth:: NthFuture ;
5252
53+ use super :: from_stream:: FromStream ;
54+ use crate :: future:: Future ;
55+ use crate :: task:: { Context , Poll } ;
5356use std:: cmp:: Ordering ;
5457use std:: marker:: PhantomData ;
5558use std:: pin:: Pin ;
5659
5760use cfg_if:: cfg_if;
5861
59- use crate :: task:: { Context , Poll } ;
60-
6162cfg_if ! {
6263 if #[ cfg( feature = "docs" ) ] {
6364 #[ doc( hidden) ]
@@ -80,6 +81,21 @@ cfg_if! {
8081 }
8182}
8283
84+ cfg_if ! {
85+ if #[ cfg( feature = "docs" ) ] {
86+ #[ doc( hidden) ]
87+ pub struct DynFuture <' a, T >( std:: marker:: PhantomData <& ' a T >) ;
88+
89+ macro_rules! dyn_ret {
90+ ( $a: lifetime, $o: ty) => ( DynFuture <$a, $o>) ;
91+ }
92+ } else {
93+ macro_rules! dyn_ret {
94+ ( $a: lifetime, $o: ty) => ( Pin <Box <dyn core:: future:: Future <Output = $o> + ' a>>)
95+ }
96+ }
97+ }
98+
8399/// An asynchronous stream of values.
84100///
85101/// This trait is an async version of [`std::iter::Iterator`].
@@ -528,7 +544,6 @@ pub trait Stream {
528544 ///
529545 /// Basic usage:
530546 ///
531- /// ```
532547 /// # fn main() { async_std::task::block_on(async {
533548 /// #
534549 /// use async_std::prelude::*;
@@ -652,6 +667,48 @@ pub trait Stream {
652667 {
653668 Zip :: new ( self , other)
654669 }
670+
671+ /// Transforms a stream into a collection.
672+ ///
673+ /// `collect()` can take anything streamable, and turn it into a relevant
674+ /// collection. This is one of the more powerful methods in the async
675+ /// standard library, used in a variety of contexts.
676+ ///
677+ /// The most basic pattern in which `collect()` is used is to turn one
678+ /// collection into another. You take a collection, call [`stream`] on it,
679+ /// do a bunch of transformations, and then `collect()` at the end.
680+ ///
681+ /// Because `collect()` is so general, it can cause problems with type
682+ /// inference. As such, `collect()` is one of the few times you'll see
683+ /// the syntax affectionately known as the 'turbofish': `::<>`. This
684+ /// helps the inference algorithm understand specifically which collection
685+ /// you're trying to collect into.
686+ ///
687+ /// # Examples
688+ ///
689+ /// ```
690+ /// # fn main() { async_std::task::block_on(async {
691+ /// #
692+ /// use async_std::prelude::*;
693+ /// use async_std::stream;
694+ ///
695+ /// let s = stream::repeat(9u8).take(3);
696+ /// let buf: Vec<u8> = s.collect().await;
697+ ///
698+ /// assert_eq!(buf, vec![9; 3]);
699+ /// #
700+ /// # }) }
701+ /// ```
702+ ///
703+ /// [`stream`]: trait.Stream.html#tymethod.next
704+ #[ must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead (TODO)" ]
705+ fn collect < ' a , B > ( self ) -> dyn_ret ! ( ' a, B )
706+ where
707+ Self : futures_core:: stream:: Stream + Sized + ' a ,
708+ B : FromStream < <Self as futures_core:: stream:: Stream >:: Item > ,
709+ {
710+ FromStream :: from_stream ( self )
711+ }
655712}
656713
657714impl < T : futures_core:: stream:: Stream + ?Sized > Stream for T {
0 commit comments