Add an async stream demo
This commit is contained in:
parent
ea7aea8efc
commit
bd4528edf0
4 changed files with 2160 additions and 0 deletions
|
@ -1,5 +1,8 @@
|
|||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "./stream-composition"
|
||||
},
|
||||
{
|
||||
"path": "./async-streams"
|
||||
},
|
||||
|
|
2128
stream-composition/Cargo.lock
generated
Normal file
2128
stream-composition/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
7
stream-composition/Cargo.toml
Normal file
7
stream-composition/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "stream-composition"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
trpl = "0.2.0"
|
22
stream-composition/src/main.rs
Normal file
22
stream-composition/src/main.rs
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue