From a3463adee5cfcbc422886653787fee12e594d5f6 Mon Sep 17 00:00:00 2001 From: hybrid Date: Sat, 7 Nov 2026 05:53:30 +0300 Subject: add: streams, thread2async, oop, blog --- src/streams/Cargo.toml | 7 ++++++ src/streams/src/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/streams/Cargo.toml create mode 100644 src/streams/src/main.rs (limited to 'src/streams') 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>> = + vec![tx1_fut, rx_fut, tx_fut]; + + trpl::join_all(futures).await; + }); +} -- cgit v1.3.1