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