Store Future
values on the stack
This commit is contained in:
parent
b7ff776e06
commit
c86f38aa32
1 changed files with 9 additions and 9 deletions
|
@ -1,11 +1,11 @@
|
||||||
use std::{pin::Pin, time::Duration};
|
use std::{pin::Pin, pin::pin, time::Duration};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
trpl::run(async {
|
trpl::run(async {
|
||||||
let (tx, mut rx) = trpl::channel();
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
let tx1 = tx.clone();
|
let tx1 = tx.clone();
|
||||||
let tx1_fut = async move {
|
let tx1_fut = pin!(async move {
|
||||||
let vals = vec![
|
let vals = vec![
|
||||||
String::from("hi"),
|
String::from("hi"),
|
||||||
String::from("from"),
|
String::from("from"),
|
||||||
|
@ -17,15 +17,15 @@ fn main() {
|
||||||
tx1.send(val).unwrap();
|
tx1.send(val).unwrap();
|
||||||
trpl::sleep(Duration::from_millis(500)).await;
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
let rx_fut = async {
|
let rx_fut = pin!(async {
|
||||||
while let Some(value) = rx.recv().await {
|
while let Some(value) = rx.recv().await {
|
||||||
println!("received '{value}'");
|
println!("received '{value}'");
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
let tx_fut = async move {
|
let tx_fut = pin!(async move {
|
||||||
let vals = vec![
|
let vals = vec![
|
||||||
String::from("more"),
|
String::from("more"),
|
||||||
String::from("messages"),
|
String::from("messages"),
|
||||||
|
@ -37,10 +37,10 @@ fn main() {
|
||||||
tx.send(val).unwrap();
|
tx.send(val).unwrap();
|
||||||
trpl::sleep(Duration::from_millis(1500)).await;
|
trpl::sleep(Duration::from_millis(1500)).await;
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
let futures: Vec<Pin<Box<dyn Future<Output = ()>>>> =
|
let futures: Vec<Pin<&mut dyn Future<Output = ()>>> =
|
||||||
vec![Box::pin(tx1_fut), Box::pin(rx_fut), Box::pin(tx_fut)];
|
vec![tx1_fut, rx_fut, tx_fut];
|
||||||
|
|
||||||
trpl::join_all(futures).await;
|
trpl::join_all(futures).await;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue