Skip to content

Commit 8da6055

Browse files
committed
add recipes to convert a BufList into a Stream or a TryStream
1 parent c4d8a02 commit 8da6055

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
This project adheres to [Semantic Versioning](https://semver.org).
66

7+
## [1.0.1] - 2023-02-16
8+
9+
### Added
10+
11+
- Add recipes for converting a `BufList` into a `Stream` or a `TryStream`.
12+
713
## [1.0.0] - 2023-01-06
814

915
### Added
@@ -32,6 +38,7 @@ This project adheres to [Semantic Versioning](https://semver.org).
3238

3339
- Initial release.
3440

41+
[1.0.1]: https://github.com/sunshowers-code/buf-list/releases/tag/1.0.1
3542
[1.0.0]: https://github.com/sunshowers-code/buf-list/releases/tag/1.0.0
3643
[0.1.3]: https://github.com/sunshowers-code/buf-list/releases/tag/0.1.3
3744
[0.1.2]: https://github.com/sunshowers-code/buf-list/releases/tag/0.1.2

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Collect a fallible stream of `Bytes` into a `BufList`:
3737
```rust
3838
use buf_list::BufList;
3939
use bytes::Bytes;
40-
use futures::stream::TryStreamExt;
40+
use futures::TryStreamExt;
4141

4242
// A common example is a stream of bytes read over HTTP.
4343
let stream = futures::stream::iter(
@@ -52,6 +52,28 @@ let buf_list = stream.try_collect::<BufList>().await?;
5252
assert_eq!(buf_list.num_chunks(), 3);
5353
```
5454

55+
## Converting to `Stream`s
56+
57+
A `BufList` can be converted into a `futures::Stream`, or a `TryStream`, of `Bytes` chunks. Use
58+
this recipe to do so:
59+
60+
(This will be exposed as an API on `BufList` once `Stream` and/or `TryStream` become part of
61+
stable Rust.)
62+
63+
```rust
64+
use buf_list::BufList;
65+
use bytes::Bytes;
66+
use futures::{Stream, TryStream};
67+
68+
fn into_stream(buf_list: BufList) -> impl Stream<Item = Bytes> {
69+
futures::stream::iter(buf_list)
70+
}
71+
72+
fn into_try_stream<E>(buf_list: BufList) -> impl TryStream<Ok = Bytes, Error = E> {
73+
futures::stream::iter(buf_list.into_iter().map(Ok))
74+
}
75+
```
76+
5577
## Minimum supported Rust version
5678

5779
The minimum supported Rust version (MSRV) is **1.39**, same as the `bytes` crate.

src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//! ```
4343
//! use buf_list::BufList;
4444
//! use bytes::Bytes;
45-
//! use futures::stream::TryStreamExt;
45+
//! use futures::TryStreamExt;
4646
//!
4747
//! # #[tokio::main(flavor = "current_thread")]
4848
//! # async fn main() -> Result<(), ()> {
@@ -60,6 +60,28 @@
6060
//! # Ok(()) }
6161
//! ```
6262
//!
63+
//! # Converting to `Stream`s
64+
//!
65+
//! A `BufList` can be converted into a `futures::Stream`, or a `TryStream`, of `Bytes` chunks. Use
66+
//! this recipe to do so:
67+
//!
68+
//! (This will be exposed as an API on `BufList` once `Stream` and/or `TryStream` become part of
69+
//! stable Rust.)
70+
//!
71+
//! ```rust
72+
//! use buf_list::BufList;
73+
//! use bytes::Bytes;
74+
//! use futures::{Stream, TryStream};
75+
//!
76+
//! fn into_stream(buf_list: BufList) -> impl Stream<Item = Bytes> {
77+
//! futures::stream::iter(buf_list)
78+
//! }
79+
//!
80+
//! fn into_try_stream<E>(buf_list: BufList) -> impl TryStream<Ok = Bytes, Error = E> {
81+
//! futures::stream::iter(buf_list.into_iter().map(Ok))
82+
//! }
83+
//! ```
84+
//!
6385
//! # Minimum supported Rust version
6486
//!
6587
//! The minimum supported Rust version (MSRV) is **1.39**, same as the `bytes` crate.

0 commit comments

Comments
 (0)