Store Future values on the stack

This commit is contained in:
Manuel Thalmann 2025-04-06 23:11:56 +02:00
parent b7ff776e06
commit c86f38aa32
Signed by: manuth
SSH key fingerprint: SHA256:HsMLC+7kJWALP6YCYCoopxNbUnghwSGLVcG76SECT5c

View file

@ -1,11 +1,11 @@
use std::{pin::Pin, time::Duration};
use std::{pin::Pin, pin::pin, time::Duration};
fn main() {
trpl::run(async {
let (tx, mut rx) = trpl::channel();
let tx1 = tx.clone();
let tx1_fut = async move {
let tx1_fut = pin!(async move {
let vals = vec![
String::from("hi"),
String::from("from"),
@ -17,15 +17,15 @@ fn main() {
tx1.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
});
let rx_fut = async {
let rx_fut = pin!(async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
};
});
let tx_fut = async move {
let tx_fut = pin!(async move {
let vals = vec![
String::from("more"),
String::from("messages"),
@ -37,10 +37,10 @@ fn main() {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(1500)).await;
}
};
});
let futures: Vec<Pin<Box<dyn Future<Output = ()>>>> =
vec![Box::pin(tx1_fut), Box::pin(rx_fut), Box::pin(tx_fut)];
let futures: Vec<Pin<&mut dyn Future<Output = ()>>> =
vec![tx1_fut, rx_fut, tx_fut];
trpl::join_all(futures).await;
});