async_std/stream/
from_stream.rs

1use core::future::Future;
2use core::pin::Pin;
3
4use crate::stream::IntoStream;
5
6/// Conversion from a `Stream`.
7///
8/// By implementing `FromStream` for a type, you define how it will be created from a stream.
9/// This is common for types which describe a collection of some kind.
10///
11/// See also: [`IntoStream`].
12///
13/// # Examples
14///
15/// Basic usage:
16///
17/// ```
18/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
19/// #
20/// use async_std::prelude::*;
21/// use async_std::stream::{self, FromStream};
22///
23/// let five_fives = stream::repeat(5).take(5);
24///
25/// let v = Vec::from_stream(five_fives).await;
26///
27/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
28/// #
29/// # Ok(()) }) }
30/// ```
31///
32/// Using `collect` to  implicitly use `FromStream`
33///
34/// ```
35/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
36/// #
37/// use async_std::prelude::*;
38/// use async_std::stream;
39/// let five_fives = stream::repeat(5).take(5);
40///
41/// let v: Vec<i32> = five_fives.collect().await;
42///
43/// assert_eq!(v, vec![5, 5, 5, 5, 5]);
44/// #
45/// # Ok(()) }) }
46/// ```
47///
48/// Implementing `FromStream` for your type:
49///
50/// ```
51/// use async_std::prelude::*;
52/// use async_std::stream::{self, FromStream, IntoStream};
53/// use std::pin::Pin;
54///
55/// // A sample collection, that's just a wrapper over Vec<T>
56/// #[derive(Debug)]
57/// struct MyCollection(Vec<i32>);
58///
59/// // Let's give it some methods so we can create one and add things
60/// // to it.
61/// impl MyCollection {
62///     fn new() -> MyCollection {
63///         MyCollection(Vec::new())
64///     }
65///
66///     fn add(&mut self, elem: i32) {
67///         self.0.push(elem);
68///     }
69/// }
70///
71/// // and we'll implement FromIterator
72/// impl FromStream<i32> for MyCollection {
73///     fn from_stream<'a, S: IntoStream<Item = i32> + 'a>(
74///         stream: S,
75///     ) -> Pin<Box<dyn Future<Output = Self> + 'a + Send>>
76///    where
77///        <S as IntoStream>::IntoStream: Send,
78///    {
79///         let stream = stream.into_stream();
80///
81///         Box::pin(async move {
82///             let mut c = MyCollection::new();
83///
84///             let mut v = vec![];
85///             stream::extend(&mut v, stream).await;
86///
87///             for i in v {
88///                 c.add(i);
89///             }
90///             c
91///         })
92///     }
93/// }
94///
95/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
96/// #
97/// // Now we can make a new stream...
98/// let stream = stream::repeat(5).take(5);
99///
100/// // ...and make a MyCollection out of it
101/// let c = MyCollection::from_stream(stream).await;
102///
103/// assert_eq!(c.0, vec![5, 5, 5, 5, 5]);
104///
105/// // collect works too!
106///
107/// let stream = stream::repeat(5).take(5);
108/// let c: MyCollection = stream.collect().await;
109///
110/// assert_eq!(c.0, vec![5, 5, 5, 5, 5]);
111/// #
112/// # Ok(()) }) }
113/// ```
114///
115/// [`IntoStream`]: trait.IntoStream.html
116#[cfg(feature = "unstable")]
117#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
118pub trait FromStream<T: Send> {
119    /// Creates a value from a stream.
120    ///
121    /// # Examples
122    ///
123    /// Basic usage:
124    ///
125    /// ```
126    /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
127    /// #
128    /// use async_std::prelude::*;
129    /// use async_std::stream::{self, FromStream};
130    ///
131    /// let five_fives = stream::repeat(5).take(5);
132    ///
133    /// let v = Vec::from_stream(five_fives).await;
134    ///
135    /// assert_eq!(v, vec![5, 5, 5, 5, 5]);
136    /// #
137    /// # Ok(()) }) }
138    /// ```
139    fn from_stream<'a, S: IntoStream<Item = T> + 'a>(
140        stream: S,
141    ) -> Pin<Box<dyn Future<Output = Self> + 'a + Send>>
142    where
143        <S as IntoStream>::IntoStream: Send;
144}