@@ -9,6 +9,102 @@ use std::pin::Pin;
99///
1010/// See also: [`IntoStream`].
1111///
12+ /// # Examples
13+ ///
14+ /// Basic usage:
15+ ///
16+ /// ```
17+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
18+ /// use crate::async_std::stream::FromStream;
19+ /// use async_std::prelude::*;
20+ /// use async_std::stream;
21+ ///
22+ /// let five_fives = stream::repeat(5).take(5);
23+ ///
24+ /// let v = Vec::from_stream(five_fives).await;
25+ ///
26+ /// assert_eq!(v, vec![5, 5, 5, 5, 5]);
27+ /// # Ok(()) }) }
28+ /// ```
29+ ///
30+ /// Using `collect` to implicitly use `FromStream`
31+ ///
32+ ///```
33+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
34+ /// use async_std::prelude::*;
35+ /// use async_std::stream;
36+ /// let five_fives = stream::repeat(5).take(5);
37+ ///
38+ /// let v: Vec<i32> = five_fives.collect().await;
39+ ///
40+ /// assert_eq!(v, vec![5, 5, 5, 5, 5]);
41+ /// #
42+ /// # Ok(()) }) }
43+ ///```
44+ ///
45+ /// Implementing `FromStream` for your type:
46+ ///
47+ /// ```
48+ /// use async_std::prelude::*;
49+ /// use async_std::stream::{Extend, FromStream, IntoStream};
50+ /// use async_std::stream;
51+ /// use std::pin::Pin;
52+ ///
53+ /// // A sample collection, that's just a wrapper over Vec<T>
54+ /// #[derive(Debug)]
55+ /// struct MyCollection(Vec<i32>);
56+ ///
57+ /// // Let's give it some methods so we can create one and add things
58+ /// // to it.
59+ /// impl MyCollection {
60+ /// fn new() -> MyCollection {
61+ /// MyCollection(Vec::new())
62+ /// }
63+ ///
64+ /// fn add(&mut self, elem: i32) {
65+ /// self.0.push(elem);
66+ /// }
67+ /// }
68+ ///
69+ /// // and we'll implement FromIterator
70+ /// impl FromStream<i32> for MyCollection {
71+ /// fn from_stream<'a, S: IntoStream<Item = i32> + 'a>(
72+ /// stream: S,
73+ /// ) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>> {
74+ /// let stream = stream.into_stream();
75+ ///
76+ /// Box::pin(async move {
77+ /// let mut c = MyCollection::new();
78+ ///
79+ /// let mut v = vec![];
80+ /// v.stream_extend(stream).await;
81+ ///
82+ /// for i in v {
83+ /// c.add(i);
84+ /// }
85+ /// c
86+ /// })
87+ /// }
88+ /// }
89+ ///
90+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
91+ /// // Now we can make a new stream...
92+ /// let stream = stream::repeat(5).take(5);
93+ ///
94+ /// // ...and make a MyCollection out of it
95+ /// let c = MyCollection::from_stream(stream).await;
96+ ///
97+ /// assert_eq!(c.0, vec![5, 5, 5, 5, 5]);
98+ ///
99+ /// // collect works too!
100+ ///
101+ /// let stream = stream::repeat(5).take(5);
102+ /// let c: MyCollection = stream.collect().await;
103+ ///
104+ /// assert_eq!(c.0, vec![5, 5, 5, 5, 5]);
105+ /// # Ok(()) }) }
106+ ///```
107+ ///
12108/// [`IntoStream`]: trait.IntoStream.html
13109#[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
14110#[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
@@ -20,9 +116,17 @@ pub trait FromStream<T> {
20116 /// Basic usage:
21117 ///
22118 /// ```
23- /// // use async_std::stream::FromStream;
119+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
120+ /// use crate::async_std::stream::FromStream;
121+ /// use async_std::prelude::*;
122+ /// use async_std::stream;
123+ ///
124+ /// let five_fives = stream::repeat(5).take(5);
125+ ///
126+ /// let v = Vec::from_stream(five_fives).await;
24127 ///
25- /// // let _five_fives = async_std::stream::repeat(5).take(5);
128+ /// assert_eq!(v, vec![5, 5, 5, 5, 5]);
129+ /// # Ok(()) }) }
26130 /// ```
27131 fn from_stream < ' a , S : IntoStream < Item = T > + ' a > (
28132 stream : S ,
0 commit comments