|
25 | 25 | //! use buf_list::BufList; |
26 | 26 | //! use tokio::io::AsyncWriteExt; |
27 | 27 | //! |
28 | | -//! #[tokio::main(flavor = "current_thread")] |
29 | | -//! async fn main() { |
30 | | -//! let mut buf_list = BufList::new(); |
31 | | -//! buf_list.push_chunk(&b"hello "[..]); |
32 | | -//! buf_list.push_chunk(&b"world"[..]); |
33 | | -//! buf_list.push_chunk(&b"!"[..]); |
| 28 | +//! # #[tokio::main(flavor = "current_thread")] |
| 29 | +//! # async fn main() -> Result<(), std::io::Error> { |
| 30 | +//! let mut buf_list = BufList::new(); |
| 31 | +//! buf_list.push_chunk(&b"hello "[..]); |
| 32 | +//! buf_list.push_chunk(&b"world"[..]); |
| 33 | +//! buf_list.push_chunk(&b"!"[..]); |
34 | 34 | //! |
35 | | -//! let mut stderr = tokio::io::stderr(); |
36 | | -//! stderr.write_all_buf(&mut buf_list).await.unwrap(); |
37 | | -//! } |
| 35 | +//! let mut stderr = tokio::io::stderr(); |
| 36 | +//! stderr.write_all_buf(&mut buf_list).await?; |
| 37 | +//! # Ok(()) } |
| 38 | +//! ``` |
| 39 | +//! |
| 40 | +//! Collect a fallible stream of `Bytes` into a `BufList`: |
| 41 | +//! |
| 42 | +//! ``` |
| 43 | +//! use buf_list::BufList; |
| 44 | +//! use bytes::Bytes; |
| 45 | +//! use futures::stream::TryStreamExt; |
| 46 | +//! |
| 47 | +//! # #[tokio::main(flavor = "current_thread")] |
| 48 | +//! # async fn main() -> Result<(), ()> { |
| 49 | +//! // A common example is a stream of bytes read over HTTP. |
| 50 | +//! let stream = futures::stream::iter( |
| 51 | +//! vec![ |
| 52 | +//! Ok(Bytes::from_static(&b"laputa, "[..])), |
| 53 | +//! Ok(Bytes::from_static(&b"castle "[..])), |
| 54 | +//! Ok(Bytes::from_static(&b"in the sky"[..])) |
| 55 | +//! ], |
| 56 | +//! ); |
| 57 | +//! |
| 58 | +//! let buf_list = stream.try_collect::<BufList>().await?; |
| 59 | +//! assert_eq!(buf_list.num_chunks(), 3); |
| 60 | +//! # Ok(()) } |
38 | 61 | //! ``` |
39 | 62 | //! |
40 | 63 | //! # Minimum supported Rust version |
@@ -147,10 +170,15 @@ impl BufList { |
147 | 170 | } |
148 | 171 | } |
149 | 172 |
|
150 | | -impl<B> FromIterator<B> for BufList |
151 | | -where |
152 | | - B: Buf, |
153 | | -{ |
| 173 | +impl<B: Buf> Extend<B> for BufList { |
| 174 | + fn extend<T: IntoIterator<Item = B>>(&mut self, iter: T) { |
| 175 | + for buf in iter.into_iter() { |
| 176 | + self.push_chunk(buf); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +impl<B: Buf> FromIterator<B> for BufList { |
154 | 182 | fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self { |
155 | 183 | let mut buf_list = BufList::new(); |
156 | 184 | for buf in iter.into_iter() { |
|
0 commit comments