From b15f60d0d74227c01860193ca9a8518ab35475ce Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Tue, 5 Jul 2022 19:18:45 +0300 Subject: Implement basic stream chaining --- README.md | 7 +++++++ src/main.rs | 27 ++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c039fc1..6d1d8d4 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,10 @@ Meanwhile it seems some features aren't fully stable in Rust just yet. Some crat https://github.com/taiki-e/futures-async-stream https://github.com/tokio-rs/async-stream + +This page has a good review https://blog.yoshuawuyts.com/rust-streams/ + + +## Design + +Each block processes input and out asynchronously. diff --git a/src/main.rs b/src/main.rs index 52dc625..54e8529 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use async_stream::stream; +use futures::stream::Stream; + use futures_util::pin_mut; use futures_util::stream::StreamExt; @@ -26,18 +28,25 @@ impl Future for Delay { } } -#[tokio::main] -async fn main() { - let s = stream! { - let mut when = Instant::now(); +fn number_source() -> impl Stream { + stream! { for i in 0..10 { - let delay = Delay { when }; - delay.await; - yield i; - when += Duration::from_millis(1000); + yield i } - }; + } +} +fn double>(input: S) -> impl Stream { + stream! { + for await val in input { + yield val * 2 + } + } +} + +#[tokio::main] +async fn main() { + let s = double(number_source()); pin_mut!(s); // needed for iteration while let Some(value) = s.next().await { -- cgit v1.3.1