Skip to content

Commit

Permalink
add recipes to convert a BufList into a Stream or a TryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshowers committed Feb 17, 2023
1 parent c4d8a02 commit 8da6055
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

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

## [1.0.1] - 2023-02-16

### Added

- Add recipes for converting a `BufList` into a `Stream` or a `TryStream`.

## [1.0.0] - 2023-01-06

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

- Initial release.

[1.0.1]: https://github.com/sunshowers-code/buf-list/releases/tag/1.0.1
[1.0.0]: https://github.com/sunshowers-code/buf-list/releases/tag/1.0.0
[0.1.3]: https://github.com/sunshowers-code/buf-list/releases/tag/0.1.3
[0.1.2]: https://github.com/sunshowers-code/buf-list/releases/tag/0.1.2
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Collect a fallible stream of `Bytes` into a `BufList`:
```rust
use buf_list::BufList;
use bytes::Bytes;
use futures::stream::TryStreamExt;
use futures::TryStreamExt;

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

## Converting to `Stream`s

A `BufList` can be converted into a `futures::Stream`, or a `TryStream`, of `Bytes` chunks. Use
this recipe to do so:

(This will be exposed as an API on `BufList` once `Stream` and/or `TryStream` become part of
stable Rust.)

```rust
use buf_list::BufList;
use bytes::Bytes;
use futures::{Stream, TryStream};

fn into_stream(buf_list: BufList) -> impl Stream<Item = Bytes> {
futures::stream::iter(buf_list)
}

fn into_try_stream<E>(buf_list: BufList) -> impl TryStream<Ok = Bytes, Error = E> {
futures::stream::iter(buf_list.into_iter().map(Ok))
}
```

## Minimum supported Rust version

The minimum supported Rust version (MSRV) is **1.39**, same as the `bytes` crate.
Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! ```
//! use buf_list::BufList;
//! use bytes::Bytes;
//! use futures::stream::TryStreamExt;
//! use futures::TryStreamExt;
//!
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() -> Result<(), ()> {
Expand All @@ -60,6 +60,28 @@
//! # Ok(()) }
//! ```
//!
//! # Converting to `Stream`s
//!
//! A `BufList` can be converted into a `futures::Stream`, or a `TryStream`, of `Bytes` chunks. Use
//! this recipe to do so:
//!
//! (This will be exposed as an API on `BufList` once `Stream` and/or `TryStream` become part of
//! stable Rust.)
//!
//! ```rust
//! use buf_list::BufList;
//! use bytes::Bytes;
//! use futures::{Stream, TryStream};
//!
//! fn into_stream(buf_list: BufList) -> impl Stream<Item = Bytes> {
//! futures::stream::iter(buf_list)
//! }
//!
//! fn into_try_stream<E>(buf_list: BufList) -> impl TryStream<Ok = Bytes, Error = E> {
//! futures::stream::iter(buf_list.into_iter().map(Ok))
//! }
//! ```
//!
//! # Minimum supported Rust version
//!
//! The minimum supported Rust version (MSRV) is **1.39**, same as the `bytes` crate.
Expand Down

0 comments on commit 8da6055

Please sign in to comment.