Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ unsynchronized
Unyank
UpperCamelCase
URIs
urls
UsefulType
username
USERPROFILE
Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use trpl::Html;
fn main() {
let args: Vec<String> = std::env::args().collect();

trpl::run(async {
trpl::block_on(async {
let url = &args[1];
match page_title(url).await {
Some(title) => println!("The title for {url} was {title}"),
Expand Down
4 changes: 2 additions & 2 deletions listings/ch17-async-await/listing-17-05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use trpl::{Either, Html};
fn main() {
let args: Vec<String> = std::env::args().collect();

trpl::run(async {
trpl::block_on(async {
let title_fut_1 = page_title(&args[1]);
let title_fut_2 = page_title(&args[2]);

let (url, maybe_title) =
match trpl::race(title_fut_1, title_fut_2).await {
match trpl::select(title_fut_1, title_fut_2).await {
Either::Left(left) => left,
Either::Right(right) => right,
};
Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
trpl::spawn_task(async {
for i in 1..10 {
println!("hi number {i} from the first task!");
Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-07/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
// ANCHOR: handle
let handle = trpl::spawn_task(async {
for i in 1..10 {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-08/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
// ANCHOR: join
let fut1 = async {
for i in 1..10 {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-09/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate trpl; // required for mdbook test

fn main() {
trpl::run(async {
trpl::block_on(async {
// ANCHOR: channel
let (tx, mut rx) = trpl::channel();

Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-10/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
// ANCHOR: many-messages
let (tx, mut rx) = trpl::channel();

Expand Down
2 changes: 1 addition & 1 deletion listings/ch17-async-await/listing-17-11/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
let (tx, mut rx) = trpl::channel();

// ANCHOR: futures
Expand Down
5 changes: 3 additions & 2 deletions listings/ch17-async-await/listing-17-12/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
// ANCHOR: with-move
let (tx, mut rx) = trpl::channel();

let tx_fut = async move {
// --snip--
// ANCHOR_END: with-move
let vals = vec![
String::from("hi"),
String::from("from"),
Expand All @@ -28,6 +30,5 @@ fn main() {
};

trpl::join(tx_fut, rx_fut).await;
// ANCHOR_END: with-move
});
}
4 changes: 2 additions & 2 deletions listings/ch17-async-await/listing-17-13/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate trpl; // required for mdbook test
use std::time::Duration;

fn main() {
trpl::run(async {
trpl::block_on(async {
// ANCHOR: here
let (tx, mut rx) = trpl::channel();

Expand Down Expand Up @@ -42,7 +42,7 @@ fn main() {
}
};

trpl::join3(tx1_fut, tx_fut, rx_fut).await;
trpl::join!(tx1_fut, tx_fut, rx_fut);
// ANCHOR_END: here
});
}
52 changes: 10 additions & 42 deletions listings/ch17-async-await/listing-17-14/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,16 @@
extern crate trpl; // required for mdbook test

use std::time::Duration;
use std::{thread, time::Duration};

fn main() {
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_secs(1)).await;
}
};

let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
};

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_secs(1)).await;
}
};

// ANCHOR: here
trpl::join!(tx1_fut, tx_fut, rx_fut);
// ANCHOR_END: here
trpl::block_on(async {
// We will call `slow` here later
});
}

// ANCHOR: slow
fn slow(name: &str, ms: u64) {
thread::sleep(Duration::from_millis(ms));
println!("'{name}' ran for {ms}ms");
}
// ANCHOR_END: slow
65 changes: 25 additions & 40 deletions listings/ch17-async-await/listing-17-15/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,35 @@
extern crate trpl; // required for mdbook test

use std::time::Duration;
use std::{thread, time::Duration};

fn main() {
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_secs(1)).await;
}
};

let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
trpl::block_on(async {
// ANCHOR: slow-futures
let a = async {
println!("'a' started.");
slow("a", 30);
slow("a", 10);
slow("a", 20);
trpl::sleep(Duration::from_millis(50)).await;
println!("'a' finished.");
};

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_secs(1)).await;
}
let b = async {
println!("'b' started.");
slow("b", 75);
slow("b", 10);
slow("b", 15);
slow("b", 350);
trpl::sleep(Duration::from_millis(50)).await;
println!("'b' finished.");
};

// ANCHOR: here
let futures = vec![tx1_fut, rx_fut, tx_fut];

trpl::join_all(futures).await;
// ANCHOR_END: here
trpl::select(a, b).await;
// ANCHOR_END: slow-futures
});
}

fn slow(name: &str, ms: u64) {
thread::sleep(Duration::from_millis(ms));
println!("'{name}' ran for {ms}ms");
}
73 changes: 32 additions & 41 deletions listings/ch17-async-await/listing-17-16/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,42 @@
extern crate trpl; // required for mdbook test

use std::time::Duration;
use std::{thread, time::Duration};

fn main() {
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_secs(1)).await;
}
};

let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
trpl::block_on(async {
// ANCHOR: here
let one_ms = Duration::from_millis(1);

let a = async {
println!("'a' started.");
slow("a", 30);
trpl::sleep(one_ms).await;
slow("a", 10);
trpl::sleep(one_ms).await;
slow("a", 20);
trpl::sleep(one_ms).await;
println!("'a' finished.");
};

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_secs(1)).await;
}
let b = async {
println!("'b' started.");
slow("b", 75);
trpl::sleep(one_ms).await;
slow("b", 10);
trpl::sleep(one_ms).await;
slow("b", 15);
trpl::sleep(one_ms).await;
slow("b", 350);
trpl::sleep(one_ms).await;
println!("'b' finished.");
};

// ANCHOR: here
let futures =
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];

trpl::join_all(futures).await;
// ANCHOR_END: here

trpl::select(a, b).await;
});
}

fn slow(name: &str, ms: u64) {
thread::sleep(Duration::from_millis(ms));
println!("'{name}' ran for {ms}ms");
}
4 changes: 2 additions & 2 deletions listings/ch17-async-await/listing-17-17/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion listings/ch17-async-await/listing-17-17/output.txt

This file was deleted.

Loading