Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 17: Async and Await #4037

Merged
merged 280 commits into from
Oct 9, 2024
Merged

Chapter 17: Async and Await #4037

merged 280 commits into from
Oct 9, 2024

Conversation

chriskrycho
Copy link
Contributor

@chriskrycho chriskrycho commented Sep 23, 2024

A working draft of a new chapter on async and await! If you have been invited to give comments, please do so! If you’re not someone we have actively invited, we kindly ask you to hold off: although we are working in public, we cannot absorb too much feedback at once. Thanks!

Status: this has been through multiple revisions, including integrating feedback from the first several rounds of review, and is very nearly ready to publish. 🎉 🚀 It still has a few small items to tackle before we can merge it and ship the chapter to the general public:

  • Further feedback from expressly requested reviewers (currently: just @carols10cents, though further feedback from @timClicks, @willcrichton, or @jamesmunns is also welcome).
  • Add one or more diagrams to the section on pinning so the memory layout can be easier for people to grok.
  • Add a couple code samples to the pinning section to avoid the wall-of-text problem Will identified in the first round of reviews!

Note

This branch does not include changes to the listings for the chapters after this new chapter.

Additionally, we will need to make some other small changes to other chapters (and possibly some significant ones to the “final project” chapter) to account for having this new material in place. Those are all also out of scope for this PR!

This PR replaces and supersedes #3909. It intentionally does not yet include the changes from #3908, which will need to be recreated on top of this, likely as a series of separate pull requests to make the reviews possible.

- Tie off the discussion about needing a runtime so that it is clear
  what is needed in `main` and therefore why `main` cannot natively be
  `async` itself.
- Correct the description of what `.await` compiles to.
- Extend the note about the “under the hood” bits: mention generators so
  people know what to go looking for if they are curious.
- Rewrite the existing introduction of the `#[async_main]` macro to lean
  on the material now covered in the previous chapter.
Keep both references, but make the first one a simple definition, and
the second one the “ah, now we can make sense of that definition”.
Re-exporting the macro does not work unless you have `tokio` as a direct
dependency, because its expansion is in terms of Tokio doc-hidden items.
Add some listings which I can actually run, with `TODO` paths in them
since I do not know what the numbers will be, since I have not actually
finished with §0 or §1 yet!
Also rename the files to match their actual titles now that I know them.
Or at least: know a good first pass for them.
- Introduce the relevant supporting features in `trpl`.
- Add a couple listings to show how things do or do not work.
- Incorporate a good discusion of the need to make sure that the `tx`
  in this example gets dropped.
- Add more listings which show borrowing vs. moving a `tx`, covering
  the full territory in that example.
- Add and test more re-exports in `trpl`.

I made a conscious choice here *not* to use `future::join_all()` because
that ends up getting into a discussion of `Pin`. I left a TODO item here
for now because I think it is probably worth getting into, and that
could be a good thing to transition to *after* this section.
It should manage its own dependencies.
Wherever it makes sense to elide these for the sake of clarity, do so!
I am not 100% sure we will keep these, but right now they feel useful for
helping see how `async` can help solve some of these problems.
This lets me combine the examples of trying to get data “out” with the
existing network socket example, rather than introducing yet another
imaginary API, and gives us a final API that looks much closer to what
users will actually see with a real world socket.
§03 is the current place I expect to tackle the distinctions between
threads, tasks, and futures, and is therefore a reasonable home for a
brief discussion on these two big ideas, I think?
- Move the heading out from §03.
- Add this to the summary.
Per the [async working group][wg], the _status quo_ for this is unlikely
to change before we go to print. We can of course update the online copy
as soon as it *does* change, and if it happens to change such that we
can sneak it in before going to press, great.

[wg]: https://rust-lang.zulipchat.com/#narrow/stream/187312-wg-async/topic/Rename.20AsyncIterator.20back.20to.20Stream.2C.20introduce.20an.20AFIT-based/near/475300446
This gives people a better chance of understanding how the pieces fit
together.
These made more sense when this section was all about big ideas and the
runtimes; now it’s needless since we actually spent the section building
something non-trivial.
- One example compiles that I used to think didn’t. Probably the result
  of some renumbering. Need to check *all* of those.
- Don’t repeat the same basic “you <verb>” form so much.
- Fix the numbers on the SVG files so they match up again after having
  added a new 17-3 earlier in the chapter.
- Actually explain what `Context` is *for*, even though we don’t get
  into the details of using it.
@chriskrycho chriskrycho merged commit 68b4d35 into main Oct 9, 2024
6 checks passed
@chriskrycho chriskrycho deleted the async-chapter branch October 9, 2024 19:16
@chriskrycho chriskrycho changed the title Draft: Chapter 17: Async and Await Chapter 17: Async and Await Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants