diff options
Diffstat (limited to '')
| -rw-r--r-- | src/streams/Cargo.toml | 7 | ||||
| -rw-r--r-- | src/streams/src/main.rs | 63 |
2 files changed, 70 insertions, 0 deletions
diff --git a/src/streams/Cargo.toml b/src/streams/Cargo.toml new file mode 100644 index 0000000..1b3c647 --- /dev/null +++ b/src/streams/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "streams" +version = "0.1.0" +edition = "2024" + +[dependencies] +trpl = "0.3.0" diff --git a/src/streams/src/main.rs b/src/streams/src/main.rs new file mode 100644 index 0000000..c7f9022 --- /dev/null +++ b/src/streams/src/main.rs @@ -0,0 +1,63 @@ +use std::{ + time::Duration, + pin::{Pin, pin}, +}; + + +use trpl; +use trpl::StreamExt; + +fn main() { + trpl::block_on(async { + let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let iter = values.iter().map(|n| n * 2); + let mut stream = trpl::stream_from_iter(iter); + + while let Some(value) = stream.next().await { + println!("The value was: {value}"); + } + + let (tx, mut rx) = trpl::channel(); + + let tx1 = tx.clone(); + let tx1_fut = pin!(async move { + let vals = vec![ + String::from("hi"), + String::from("from"), + String::from("the"), + String::from("future"), + ]; + + for val in vals { + tx1.send(val).unwrap(); + trpl::sleep(Duration::from_secs(1)).await; + } + }); + + let rx_fut = pin!(async { + while let Some(value) = rx.recv().await { + println!("received '{value}'"); + } + }); + + let tx_fut = pin!(async move { + // snip + let vals = vec![ + String::from("more"), + String::from("messages"), + String::from("for"), + String::from("you"), + ]; + + for val in vals { + tx.send(val).unwrap(); + trpl::sleep(Duration::from_secs(1)).await; + } + }); + + let futures: Vec<Pin<&mut dyn Future<Output = ()>>> = + vec![tx1_fut, rx_fut, tx_fut]; + + trpl::join_all(futures).await; + }); +} |
