Commit Graph

20 Commits

Author SHA1 Message Date
Chris Krycho
cc5db97693 Ch. 17: rewrite 17.01 with a better example
Add `reqwest` and `scraper` dependencies to the `trpl` crate. Wrap them
in `trpl` re-exports which keep the API surface low.

Rewrite the whole first section to use `race` along with those `trpl`
re-exports to show a more “real” example of async code right form the
start, including actual concurrency, unlike the previous introduction.

Update 17.03 to account for having introduced `race` already, and update
listing numbers for rewritten 17.01.

The *inclues* for them were fixed already, but not these!
2024-09-20 16:46:37 -06:00
Chris Krycho
c552952e9f Ch. 17: rename trpl::block_on to trpl::run
The `block_on` name is what both Tokio and smol use, but it is a bit
obscure from the point of view of introducing this material. `run` says
much more clearly what it does *for the level we care about here*, I
think.
2024-09-12 11:40:54 -06:00
Chris Krycho
22b04c1385 infra: prepare trpl crate for 0.1.0 release 2024-07-16 09:00:23 -06:00
Chris Krycho
92c19ce9cf Ch. 17§06 initial edits 2024-07-12 15:40:22 -06:00
Chris Krycho
ef1ed96053 Ch. 17§04 initial edits on Building Our Own Async Abstractions
Eliminate `trpl::timeout` entirely. Instead, just build the `timeout`
directly (as the rest of the section already did). Restructure the
listings as well, eliminating duplication and extraneous bits.
2024-07-09 09:22:58 -06:00
Chris Krycho
f023032403 Ch. 17: add some more explanatory comments to trpl 2024-07-02 09:12:03 -06:00
Chris Krycho
b4f5763368 Ch. 17: final example for §05, with more re-exported streams
Use `IntervalStream` and `ReceiverStream` to show the composition of
multiple streams, along with throttling and timeouts. This will also
provide a useful foundation for discussing the relationships between
futures, tasks, and threads in the final sections of the book, since
you can accomplish the same basic API by simply substituting threads
for tasks—but with different tradeoffs!
2024-06-25 14:57:45 -06:00
Chris Krycho
e790c2cc90 Ch. 17: re-export Stream, StreamExt, and stream_iter in trpl 2024-06-20 13:45:50 -06:00
Chris Krycho
67bd0a08bc Ch. 17 §03: support code for motivating Pin
- 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!
2024-06-13 13:38:38 -06:00
Chris Krycho
c310f54aab Ch. 17: Finish (first pass) documentation for trpl::race
Explain how it relates to `futures::future::select` and why `trpl` uses
`race` semantics instead.
2024-06-13 08:14:22 -06:00
Chris Krycho
71acd6d091 Ch. 17 §02: Write about race and yield_now
- Update the implementation for `race` to use `futures::future::Either`
  instead of duplicating the type, and update the test not to try to use
  equality.
- Add a `pub use` for `tokio::task::yield_now`.
2024-06-12 13:42:09 -06:00
Chris Krycho
7da440af2e Ch. 17 §02: Implement timeout example and building blocks
Create a `trpl::race` function which simplifies the `select` API by
ignoring the future which resolves second. Use the `race` function to
show how you can implement an even simpler `timeout` function on top of
it, i.e. showing how futures can compose nicely. With that in place,
there is enough to be able to “work up to it” in the body of the text.
2024-06-12 13:42:09 -06:00
Chris Krycho
c58f94376c Ch. 17 §02: Start on example of select. 2024-06-12 13:42:09 -06:00
Chris Krycho
c6965ca057 Ch. 17 §02: Start showing timeouts
This also has the setup for showing `select`, but currently jumps ahead
to showing a `timeout` example.
2024-06-12 13:42:09 -06:00
Chris Krycho
068f30b406 Ch 17.02: motivating futures::join! and Box::pin
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.
2024-06-12 13:42:09 -06:00
Chris Krycho
4b7886e092 Ch. 17: Finish a pass on Message Passing example
- 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.
2024-06-12 13:42:09 -06:00
Chris Krycho
c1c3c7fac8 Ch. 17: start on message-passing and async move in §2
- Introduce the relevant supporting features in `trpl`.
- Add a couple listings to show how things do or do not work.
2024-06-12 13:42:09 -06:00
Chris Krycho
f418ceba2e Ch. 17: abandon attempt to re-export tokio::main
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.
2024-06-12 13:42:09 -06:00
Chris Krycho
95105d23d7 Ch. 17: Add futures to trpl crate for re-exports 2024-06-12 13:42:09 -06:00
Chris Krycho
40df04923b Prepare to integrate into TRPL repo 2024-05-09 17:08:35 -06:00