Add an async stream demo

This commit is contained in:
Manuel Thalmann 2025-04-10 08:18:53 +02:00
parent ea7aea8efc
commit bd4528edf0
Signed by: manuth
SSH key fingerprint: SHA256:HsMLC+7kJWALP6YCYCoopxNbUnghwSGLVcG76SECT5c
4 changed files with 2160 additions and 0 deletions

View file

@ -1,5 +1,8 @@
{
"folders": [
{
"path": "./stream-composition"
},
{
"path": "./async-streams"
},

2128
stream-composition/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,7 @@
[package]
name = "stream-composition"
version = "0.1.0"
edition = "2024"
[dependencies]
trpl = "0.2.0"

View file

@ -0,0 +1,22 @@
use trpl::{ReceiverStream, Stream, StreamExt};
fn main() {
trpl::run(async {
let mut messages = get_messages();
while let Some(message) = messages.next().await {
println!("{message}");
}
});
}
fn get_messages() -> impl Stream<Item = String> {
let (tx, rx) = trpl::channel();
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
for message in messages {
tx.send(format!("Message: '{message}'")).unwrap();
}
ReceiverStream::new(rx)
}