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

[WIP] async and .await chapter with listings updates #3908

Draft
wants to merge 76 commits into
base: main
Choose a base branch
from

Conversation

chriskrycho
Copy link
Contributor

@chriskrycho chriskrycho commented Apr 29, 2024

Note: Work is actually happening on #3909 – this one includes everything that one includes and the listings rework—but that means GitHub has a very bad time trying to render the diff. I am maintaining this branch and PR so that I can keep the listings in reasonably good shape as we make changes to src as it stands today, so that when we finally go to merge this thing it is not an extra pile of work all at once.


As of the time of opening the PR, this is the earliest possible draft material. 😅

A quick note for folks watching the repo: the state of this is roughly: Ready for @carols10cents to leave comments, and we’ll invite others to do so as well over time, but please don’t comment-bomb us at this phase! 😂

We will primarily just be re-exporting this, though we may also have a
couple cases where we choose to implement something small around it.
1. Introduce an integration tests crate. Structure it the way people
   *should* for large crates (although this is not that) and document
   why, including linking to a relevant post.
2. Add a basic integration test that verifies the re-export works as it
   should. (This is not exactly rocket science, but we want to make sure
   these things don’t just stop working on accident.)

An open question that remains here: do we want some structure to the
crate beyond the top level re-exports? My inclination at this moment is:
no, because we don’t have any *motivation* for that, and naming things
is difficult. (We cannot do `trpl::async`, for example, because `async`
is a keyword!)
- 17.00: Introduction to the conceptual machinery. This is very nascent
  but has some decent bones.
- 17.01: Trying to get at the foundations for tasks, laziness, etc.;
  this is *especially* incomplete.
- 17.02: Just a paragraph I do not want to lose, which I think *will*
  be useful… eventually.
Add a fair bit more material about the `futures` executor and why we
might prefer to use something else. With that motivation in place, have
the readers add our `trpl` crate. (We can of course rename that crate,
but it does the job for now.) Use it to get the equivalent of
`#[tokio::main]` equivalent and incorporate it into an example.
@carols10cents
Copy link
Member

😬 GitHub is straight up not having a good time rendering the full diff with all the listing changes... so I'm opening a new PR without the listings moving for now 😱

@chriskrycho
Copy link
Contributor Author

😂 Good job, GitHub!

- Gradually build up to the full thing, rather than dumping it all at
  once at the start.
- Show several more “false starts” along the way, using them to teach
  a couple key ideas about async and control flow.
- Update the listings to have their actual expected numbers.
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from 4a92b3b to c705651 Compare May 27, 2024 21:13
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch 3 times, most recently from bd66d14 to 4a0f865 Compare May 27, 2024 23:01
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from 4a0f865 to 628afeb Compare May 28, 2024 14:30
Up to this point, the chapter has stuck to `join` and `join3`, as simple
function-based APIs. The `join_all` API is obviously more convenient
than those *if you can use it*, but being able to use it requires having
something which `impl Iterator` of a given type, and therefore demands a
homogeneous type, which motivates introducing `Box::pin`.

That in turn is quite annoying to work with and requires `Output =
<same>` for all the futures in the collection, because of how `join_all`
is typed (Rust does not have the ability to do do variadic types, which
is what would be necessary for `join_all` to work the way we might
want). Thus, we get a motivation for `futures::join!`, which unlike
`join_all` *can* work with heterogeneous types.

This fills out a fair bit of the text here and adds a lot of the listing
support, and outlines what remains to do text-wise.
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from 628afeb to 1f9526a Compare May 28, 2024 19:41
This does not yet actually *explain* either of them, but it gets us to
the point where an explanation is well-motivated and can make some
sense, and it (more or less successfully) covers the set of errors that
gets us to that point.
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from 1f9526a to fea6825 Compare May 28, 2024 22:36
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from fea6825 to 2d40425 Compare May 28, 2024 22:43
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch 2 times, most recently from 56b66e1 to a443795 Compare May 28, 2024 22:44
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from a443795 to 05688ac Compare May 29, 2024 16:17
This is (a) far from perfect and (b) far from done, but it represents a
useful increment of work and includes a bunch of notes for where to go
next with this quite tricky section.

listings/ch17-async-await/listing-17-13/src/main.rs JJ:     M
listings/ch17-async-await/listing-17-14/src/main.rs JJ:     M
src/ch17-02-concurrency-with-async.md
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from 05688ac to aea1ef7 Compare May 29, 2024 22:47
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from aea1ef7 to fcf1e8a Compare May 30, 2024 18:44
This also has the setup for showing `select`, but currently jumps ahead
to showing a `timeout` example.
Tasks to be done:

- [ ] Update all listing *output* to match what it should be for the
    version of Rust we are using at the time we actually merge this.
- [ ] (Re-)Update all references to listings to be correct.
@chriskrycho chriskrycho force-pushed the chriskrycho/chapter-async-await branch from fcf1e8a to fc98bd0 Compare May 31, 2024 20:12
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.

None yet

2 participants