Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::time::Duration;
- // Requires:
- // cargo add trpl
- // Warn: pulls in large number of dependencies
- // Reminder: cargo clean
- fn main() {
- // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-9
- trpl::run(async {
- let (tx, mut rx) = trpl::channel();
- let val = String::from("hi");
- tx.send(val).unwrap();
- let received = rx.recv().await.unwrap();
- println!("Got: {received}");
- });
- // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-10
- // Everything runs sequentially--only 1 outer async.
- trpl::run(async {
- let (tx, mut rx) = trpl::channel();
- let vals = vec![
- String::from("hi"),
- String::from("from"),
- String::from("the"),
- String::from("future"),
- ];
- for val in vals {
- tx.send(val).unwrap();
- trpl::sleep(Duration::from_millis(500)).await;
- }
- // Force tx to close so rx.recv() gets None after all messages delivered.
- drop(tx);
- while let Some(value) = rx.recv().await {
- println!("Got: {value}");
- }
- });
- // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-11
- // Then
- // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-12a
- // where `move` was added for the tx_fut async block. This allows tx to drop automatically
- // when it goes out of scope.
- //
- // Each tx mesage is sent w/ 500 ms delay as intended as a lazy defined future
- // which runs in the trpl::join().
- // rx.recv() no longer blocks forever as tx is dropped after the for loop is done sending
- // val messages
- trpl::run(async {
- let (tx, mut rx) = trpl::channel();
- let tx_fut = async move {
- let vals = vec![
- String::from("hi"),
- String::from("from"),
- String::from("the"),
- String::from("future"),
- ];
- for val in vals {
- tx.send(val).unwrap();
- trpl::sleep(Duration::from_millis(500)).await;
- }
- };
- let rx_fut = async {
- while let Some(value) = rx.recv().await {
- println!("received '{value}'");
- }
- };
- trpl::join(tx_fut, rx_fut).await;
- });
- // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-13
- // Like 17-12, except with multiple async tx_fut's
- // Then used join! macro in place of join3 per
- // https://doc.rust-lang.org/book/ch17-03-more-futures.html#listing-17-14
- // TODO: convert code to use join_all() in place of join! macro
- trpl::run(async {
- let (tx, mut rx) = trpl::channel();
- let tx1 = tx.clone();
- let tx1_fut = 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_millis(500)).await;
- }
- };
- let tx_fut = async move {
- 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_millis(1500)).await;
- }
- };
- let rx_fut = async {
- while let Some(value) = rx.recv().await {
- println!("received '{value}'");
- }
- };
- //trpl::join3(tx1_fut, tx_fut, rx_fut).await;
- trpl::join!(tx1_fut, tx_fut, rx_fut);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment