Skip to content

Commit

Permalink
Ch. 17 §03: support code for motivating Pin
Browse files Browse the repository at this point in the history
- Rexport `tokio::fs::read_to_string` as `trpl::read_to_string`.
- Add a no-listing example for the mutable borrow example. This will
  keep us honest that the code there itself compiles just fine!
  • Loading branch information
chriskrycho committed Jun 13, 2024
1 parent 2b65e53 commit 67bd0a0
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 20 deletions.
280 changes: 280 additions & 0 deletions listings/ch17-async-await/no-listing-borrow-mutable-vec/Cargo.lock

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "async_await"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
trpl = { path = "../../../packages/trpl" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn main() {
trpl::block_on({
// ANCHOR: here
async {
let mut strings = vec![];

let a = trpl::read_to_string("test-data/hello.txt").await.unwrap();
strings.push(a.trim());

let b = trpl::read_to_string("test-data/world.txt").await.unwrap();
strings.push(b.trim());

let combined = strings.join(" ");
println!("{combined}");
}
// ANCHOR_END: here
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
World
1 change: 1 addition & 0 deletions packages/trpl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
futures = "0.3.30"
tokio = { version = "1", default-features = false, features = [
"fs",
"rt-multi-thread",
"sync",
"time",
Expand Down
1 change: 1 addition & 0 deletions packages/trpl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use futures::{
join,
};
pub use tokio::{
fs::read_to_string,
runtime::Runtime,
// We use the `unbounded` variants because they most closely match the APIs
// from `std::sync::mpsc::channel`. Tokio's API choices are interesting:
Expand Down
11 changes: 11 additions & 0 deletions packages/trpl/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,14 @@ fn yield_now() {

assert_eq!(result, "done");
}

#[test]
fn read_to_string() {
let result = trpl::block_on(async {
trpl::read_to_string("tests/integration/to-read.txt")
.await
.unwrap()
});

assert_eq!(result, String::from("This is some text!\n"));
}
1 change: 1 addition & 0 deletions packages/trpl/tests/integration/to-read.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is some text!
Loading

0 comments on commit 67bd0a0

Please sign in to comment.