Skip to content

Commit 8150157

Browse files
committed
add an Extend impl
1 parent 19a114a commit 8150157

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ rust-version = "1.39"
1414
bytes = "1.3.0"
1515

1616
[dev-dependencies]
17+
futures = "0.3.25"
1718
tokio = { version = "1.0.0", features = ["io-std", "io-util", "macros", "rt"] }

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,33 @@ Gather chunks into a `BufList`, then write them all out to standard error in one
2323
use buf_list::BufList;
2424
use tokio::io::AsyncWriteExt;
2525

26-
#[tokio::main(flavor = "current_thread")]
27-
async fn main() {
28-
let mut buf_list = BufList::new();
29-
buf_list.push_chunk(&b"hello "[..]);
30-
buf_list.push_chunk(&b"world"[..]);
31-
buf_list.push_chunk(&b"!"[..]);
32-
33-
let mut stderr = tokio::io::stderr();
34-
stderr.write_all_buf(&mut buf_list).await.unwrap();
35-
}
26+
let mut buf_list = BufList::new();
27+
buf_list.push_chunk(&b"hello "[..]);
28+
buf_list.push_chunk(&b"world"[..]);
29+
buf_list.push_chunk(&b"!"[..]);
30+
31+
let mut stderr = tokio::io::stderr();
32+
stderr.write_all_buf(&mut buf_list).await?;
33+
```
34+
35+
Collect a fallible stream of `Bytes` into a `BufList`:
36+
37+
```rust
38+
use buf_list::BufList;
39+
use bytes::Bytes;
40+
use futures::stream::TryStreamExt;
41+
42+
// A common example is a stream of bytes read over HTTP.
43+
let stream = futures::stream::iter(
44+
vec![
45+
Ok(Bytes::from_static(&b"laputa, "[..])),
46+
Ok(Bytes::from_static(&b"castle "[..])),
47+
Ok(Bytes::from_static(&b"in the sky"[..]))
48+
],
49+
);
50+
51+
let buf_list = stream.try_collect::<BufList>().await?;
52+
assert_eq!(buf_list.num_chunks(), 3);
3653
```
3754

3855
## Minimum supported Rust version

src/lib.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,39 @@
2525
//! use buf_list::BufList;
2626
//! use tokio::io::AsyncWriteExt;
2727
//!
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"!"[..]);
3434
//!
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(()) }
3861
//! ```
3962
//!
4063
//! # Minimum supported Rust version
@@ -147,10 +170,15 @@ impl BufList {
147170
}
148171
}
149172

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 {
154182
fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self {
155183
let mut buf_list = BufList::new();
156184
for buf in iter.into_iter() {

0 commit comments

Comments
 (0)