Skip to content

Commit

Permalink
Use stream::iter to simplify examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed May 10, 2024
1 parent 6992d01 commit e00c007
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# futures-batch

![Build status](https://github.com/mre/futures-batch/workflows/Rust/badge.svg)
[![Cargo](https://img.shields.io/crates/v/futures-batch.svg)](
https://crates.io/crates/futures-batch)
[![Documentation](https://docs.rs/futures-batch/badge.svg)](
https://docs.rs/futures-batch)
[![Cargo](https://img.shields.io/crates/v/futures-batch.svg)](https://crates.io/crates/futures-batch)
[![Documentation](https://docs.rs/futures-batch/badge.svg)](https://docs.rs/futures-batch)

An adaptor that chunks up completed futures in a stream and flushes them after a timeout or when the buffer is full.
It is based on the `Chunks` adaptor of [futures-util](https://github.com/rust-lang-nursery/futures-rs/blob/4613193023dd4071bbd32b666e3b85efede3a725/futures-util/src/stream/chunks.rs), to which we added a timeout.
Expand All @@ -22,8 +20,7 @@ use futures_batch::ChunksTimeoutStreamExt;

#[tokio::main]
async fn main() {
let iter = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_iter();
let results = stream::iter(iter)
let results = stream::iter(0..10)
.chunks_timeout(5, Duration::new(10, 0))
.collect::<Vec<_>>();

Expand All @@ -32,7 +29,7 @@ async fn main() {
```

The above code iterates over a stream and creates chunks of size 5 with a timeout of 10 seconds.
*Note:* This is using the [`futures 0.3`](https://crates.io/crates/futures) crate.
_Note:_ This is using the [`futures 0.3`](https://crates.io/crates/futures) crate.

## Performance

Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
//!
//! #[tokio::main]
//! async fn main() {
//! let iter = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_iter();
//! let results = stream::iter(iter)
//! let results = stream::iter(0..10)
//! .chunks_timeout(5, Duration::new(10, 0))
//! .collect::<Vec<_>>();
//!
Expand Down Expand Up @@ -237,8 +236,7 @@ mod tests {

#[tokio::test]
async fn message_chunks() {
let iter = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_iter();
let stream = stream::iter(iter);
let stream = stream::iter(0..10);

let chunk_stream = ChunksTimeout::new(stream, 5, Duration::new(1, 0));
assert_eq!(
Expand Down

0 comments on commit e00c007

Please sign in to comment.