Skip to content

Commit

Permalink
[wip] MOAR streams stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho committed Jun 21, 2024
1 parent ce9b286 commit 43ccd5e
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 4 deletions.
292 changes: 292 additions & 0 deletions listings/ch17-async-await/listing-17-41/Cargo.lock

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

8 changes: 8 additions & 0 deletions listings/ch17-async-await/listing-17-41/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[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" }
18 changes: 18 additions & 0 deletions listings/ch17-async-await/listing-17-41/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn main() {
trpl::block_on(async {
let iter_a = [slow("a1", 100), slow("a2", 250), slow("a3", 100)];
let stream_a = trpl::stream_from_iter(iter_a);

let iter_b = [slow("b1", 100), slow("b2", 250), slow("b3", 100)];
let stream_b = trpl::stream_from_iter(iter_b);
})
}

fn slow(name: &str, ms: u64) -> impl Fn() + '_ {
use std::{thread, time::Duration};

move || {
thread::sleep(Duration::from_millis(ms));
println!("'{name}' ran for {ms}ms");
}
}
6 changes: 2 additions & 4 deletions src/ch17-05-streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ need to do, then, is add a `use` statement for `trpl::StreamExt`, as in Listing
</Listing>

Of course, in the real world, the only time we would be directly converting an
iterator to a stream like this is to help break up longer chunks of work, like
iterator to a stream like this is to help break up longer chunks of work, like
we discussed in the previous section. There are more interesting things we can
do with streams, though!

Expand All @@ -203,9 +203,7 @@ system at a time, or data arriving over the network over time. For another
thing, since streams are futures, we can use them with any other kind of
future, and we can combine them in interesting ways.

This is the kind of thing we might actually do to help break up longer chunks of
work, like we discussed in the previous section—though of course, presumably
with more interesting iterators than this one!
For example, we can take two streams and merge them together. For example, if we

<!--
- Maybe motivate with “doing some work” where that work is simple enough in
Expand Down

0 comments on commit 43ccd5e

Please sign in to comment.