mirror of
https://github.com/rust-lang/book.git
synced 2026-04-02 02:22:28 -04:00
Remove listings no longer used in Ch 17
This commit is contained in:
committed by
Carol (Nichols || Goulding)
parent
0d5a0dd395
commit
f427cc8425
1591
listings/ch17-async-await/listing-17-14/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-14/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,48 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let (tx, mut rx) = trpl::channel();
|
||||
|
||||
let tx1 = tx.clone();
|
||||
let tx1_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("hi"),
|
||||
String::from("from"),
|
||||
String::from("the"),
|
||||
String::from("future"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx1.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
let rx_fut = async {
|
||||
while let Some(value) = rx.recv().await {
|
||||
println!("received '{value}'");
|
||||
}
|
||||
};
|
||||
|
||||
let tx_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("more"),
|
||||
String::from("messages"),
|
||||
String::from("for"),
|
||||
String::from("you"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
// ANCHOR: here
|
||||
trpl::join!(tx1_fut, tx_fut, rx_fut);
|
||||
// ANCHOR_END: here
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-15/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-15/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,51 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
trpl::block_on(async {
|
||||
let (tx, mut rx) = trpl::channel();
|
||||
|
||||
let tx1 = tx.clone();
|
||||
let tx1_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("hi"),
|
||||
String::from("from"),
|
||||
String::from("the"),
|
||||
String::from("future"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx1.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
let rx_fut = async {
|
||||
while let Some(value) = rx.recv().await {
|
||||
println!("received '{value}'");
|
||||
}
|
||||
};
|
||||
|
||||
let tx_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("more"),
|
||||
String::from("messages"),
|
||||
String::from("for"),
|
||||
String::from("you"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
// ANCHOR: here
|
||||
let futures: Vec<Box<dyn Future<Output = ()>>> =
|
||||
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
|
||||
|
||||
trpl::join_all(futures).await;
|
||||
// ANCHOR_END: here
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-17/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-17/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1 +0,0 @@
|
||||
$
|
||||
@@ -1,51 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let (tx, mut rx) = trpl::channel();
|
||||
|
||||
let tx1 = tx.clone();
|
||||
let tx1_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("hi"),
|
||||
String::from("from"),
|
||||
String::from("the"),
|
||||
String::from("future"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx1.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
let rx_fut = async {
|
||||
while let Some(value) = rx.recv().await {
|
||||
println!("received '{value}'");
|
||||
}
|
||||
};
|
||||
|
||||
let tx_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("more"),
|
||||
String::from("messages"),
|
||||
String::from("for"),
|
||||
String::from("you"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
// ANCHOR: here
|
||||
let futures: Vec<Box<dyn Future<Output = ()>>> =
|
||||
vec![Box::new(tx1_fut), Box::new(rx_fut), Box::new(tx_fut)];
|
||||
// ANCHOR_END: here
|
||||
|
||||
trpl::join_all(futures).await;
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-18/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-18/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,57 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
// ANCHOR: here
|
||||
use std::pin::Pin;
|
||||
|
||||
// -- snip --
|
||||
|
||||
// ANCHOR_END: here
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let (tx, mut rx) = trpl::channel();
|
||||
|
||||
let tx1 = tx.clone();
|
||||
let tx1_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("hi"),
|
||||
String::from("from"),
|
||||
String::from("the"),
|
||||
String::from("future"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx1.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
let rx_fut = async {
|
||||
while let Some(value) = rx.recv().await {
|
||||
println!("received '{value}'");
|
||||
}
|
||||
};
|
||||
|
||||
let tx_fut = async move {
|
||||
let vals = vec![
|
||||
String::from("more"),
|
||||
String::from("messages"),
|
||||
String::from("for"),
|
||||
String::from("you"),
|
||||
];
|
||||
|
||||
for val in vals {
|
||||
tx.send(val).unwrap();
|
||||
trpl::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
};
|
||||
|
||||
// ANCHOR: here
|
||||
let futures: Vec<Pin<Box<dyn Future<Output = ()>>>> =
|
||||
vec![Box::pin(tx1_fut), Box::pin(rx_fut), Box::pin(tx_fut)];
|
||||
// ANCHOR_END: here
|
||||
|
||||
trpl::join_all(futures).await;
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-20/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-20/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,14 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
// ANCHOR: here
|
||||
let a = async { 1u32 };
|
||||
let b = async { "Hello!" };
|
||||
let c = async { true };
|
||||
|
||||
let (a_result, b_result, c_result) = trpl::join!(a, b, c);
|
||||
println!("{a_result}, {b_result}, {c_result}");
|
||||
// ANCHOR_END: here
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-21/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-21/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,23 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
// ANCHOR: here
|
||||
let slow = async {
|
||||
println!("'slow' started.");
|
||||
trpl::sleep(Duration::from_millis(100)).await;
|
||||
println!("'slow' finished.");
|
||||
};
|
||||
|
||||
let fast = async {
|
||||
println!("'fast' started.");
|
||||
trpl::sleep(Duration::from_millis(50)).await;
|
||||
println!("'fast' finished.");
|
||||
};
|
||||
|
||||
trpl::race(slow, fast).await;
|
||||
// ANCHOR_END: here
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-26/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-26/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,36 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
// ANCHOR: here
|
||||
let one_ns = Duration::from_nanos(1);
|
||||
let start = Instant::now();
|
||||
async {
|
||||
for _ in 1..1000 {
|
||||
trpl::sleep(one_ns).await;
|
||||
}
|
||||
}
|
||||
.await;
|
||||
let time = Instant::now() - start;
|
||||
println!(
|
||||
"'sleep' version finished after {} seconds.",
|
||||
time.as_secs_f32()
|
||||
);
|
||||
|
||||
let start = Instant::now();
|
||||
async {
|
||||
for _ in 1..1000 {
|
||||
trpl::yield_now().await;
|
||||
}
|
||||
}
|
||||
.await;
|
||||
let time = Instant::now() - start;
|
||||
println!(
|
||||
"'yield' version finished after {} seconds.",
|
||||
time.as_secs_f32()
|
||||
);
|
||||
// ANCHOR_END: here
|
||||
});
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-32/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-32/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,20 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
// ANCHOR: all
|
||||
use trpl::StreamExt;
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let values = 1..101;
|
||||
let iter = values.map(|n| n * 2);
|
||||
let stream = trpl::stream_from_iter(iter);
|
||||
|
||||
let mut filtered =
|
||||
stream.filter(|value| value % 3 == 0 || value % 5 == 0);
|
||||
|
||||
while let Some(value) = filtered.next().await {
|
||||
println!("The value was: {value}");
|
||||
}
|
||||
});
|
||||
}
|
||||
// ANCHOR_END: all
|
||||
1591
listings/ch17-async-await/listing-17-33/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-33/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,26 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
// ANCHOR: all
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let mut messages = get_messages();
|
||||
|
||||
while let Some(message) = messages.next().await {
|
||||
println!("{message}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for message in messages {
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
// ANCHOR_END: all
|
||||
1591
listings/ch17-async-await/listing-17-34/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-34/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,31 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
// ANCHOR: timeout
|
||||
use std::{pin::pin, time::Duration};
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let mut messages =
|
||||
pin!(get_messages().timeout(Duration::from_millis(200)));
|
||||
|
||||
while let Some(result) = messages.next().await {
|
||||
match result {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// ANCHOR_END: timeout
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for message in messages {
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-35/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-35/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,37 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let mut messages =
|
||||
pin!(get_messages().timeout(Duration::from_millis(200)));
|
||||
|
||||
while let Some(result) = messages.next().await {
|
||||
match result {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ANCHOR: messages
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
// ANCHOR_END: messages
|
||||
1591
listings/ch17-async-await/listing-17-36/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-36/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,52 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let mut messages =
|
||||
pin!(get_messages().timeout(Duration::from_millis(200)));
|
||||
|
||||
while let Some(result) = messages.next().await {
|
||||
match result {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
|
||||
// ANCHOR: intervals
|
||||
fn get_intervals() -> impl Stream<Item = u32> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
trpl::sleep(Duration::from_millis(1)).await;
|
||||
count += 1;
|
||||
tx.send(count).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
// ANCHOR_END: intervals
|
||||
1591
listings/ch17-async-await/listing-17-37/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-37/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,53 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
// ANCHOR: main
|
||||
let messages = get_messages().timeout(Duration::from_millis(200));
|
||||
let intervals = get_intervals();
|
||||
let merged = messages.merge(intervals);
|
||||
|
||||
while let Some(result) = merged.next().await {
|
||||
// ANCHOR_END: main
|
||||
match result {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
|
||||
fn get_intervals() -> impl Stream<Item = u32> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
trpl::sleep(Duration::from_millis(1)).await;
|
||||
count += 1;
|
||||
tx.send(count).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-38/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-38/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,56 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
// ANCHOR: main
|
||||
let messages = get_messages().timeout(Duration::from_millis(200));
|
||||
let intervals = get_intervals()
|
||||
.map(|count| format!("Interval: {count}"))
|
||||
.timeout(Duration::from_secs(10));
|
||||
let merged = messages.merge(intervals);
|
||||
let mut stream = pin!(merged);
|
||||
|
||||
while let Some(result) = stream.next().await {
|
||||
// ANCHOR_END: main
|
||||
match result {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
|
||||
fn get_intervals() -> impl Stream<Item = u32> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
trpl::sleep(Duration::from_millis(1)).await;
|
||||
count += 1;
|
||||
tx.send(count).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-39/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-39/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,57 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
// ANCHOR: throttle
|
||||
let messages = get_messages().timeout(Duration::from_millis(200));
|
||||
let intervals = get_intervals()
|
||||
.map(|count| format!("Interval: {count}"))
|
||||
.throttle(Duration::from_millis(100))
|
||||
.timeout(Duration::from_secs(10));
|
||||
let merged = messages.merge(intervals).take(20);
|
||||
let mut stream = pin!(merged);
|
||||
// ANCHOR_END: throttle
|
||||
|
||||
while let Some(result) = stream.next().await {
|
||||
match result {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
tx.send(format!("Message: '{message}'")).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
|
||||
fn get_intervals() -> impl Stream<Item = u32> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
trpl::sleep(Duration::from_millis(1)).await;
|
||||
count += 1;
|
||||
tx.send(count).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
1591
listings/ch17-async-await/listing-17-40/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-40/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,65 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let messages = get_messages().timeout(Duration::from_millis(200));
|
||||
let intervals = get_intervals()
|
||||
.map(|count| format!("Interval #{count}"))
|
||||
.throttle(Duration::from_millis(500))
|
||||
.timeout(Duration::from_secs(10));
|
||||
let merged = messages.merge(intervals).take(20);
|
||||
let mut stream = pin!(merged);
|
||||
|
||||
while let Some(result) = stream.next().await {
|
||||
match result {
|
||||
Ok(item) => println!("{item}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ANCHOR: errors
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
if let Err(send_error) = tx.send(format!("Message: '{message}'")) {
|
||||
eprintln!("Cannot send message '{message}': {send_error}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
|
||||
fn get_intervals() -> impl Stream<Item = u32> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
trpl::sleep(Duration::from_millis(1)).await;
|
||||
count += 1;
|
||||
|
||||
if let Err(send_error) = tx.send(count) {
|
||||
eprintln!("Could not send interval {count}: {send_error}");
|
||||
break;
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
// ANCHOR_END: errors
|
||||
1591
listings/ch17-async-await/listing-17-41/Cargo.lock
generated
1591
listings/ch17-async-await/listing-17-41/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "async_await"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
trpl = { path = "../../../packages/trpl" }
|
||||
@@ -1,67 +0,0 @@
|
||||
extern crate trpl; // required for mdbook test
|
||||
|
||||
use std::{pin::pin, thread, time::Duration};
|
||||
|
||||
use trpl::{ReceiverStream, Stream, StreamExt};
|
||||
|
||||
fn main() {
|
||||
trpl::run(async {
|
||||
let messages = get_messages().timeout(Duration::from_millis(200));
|
||||
let intervals = get_intervals()
|
||||
.map(|count| format!("Interval #{count}"))
|
||||
.throttle(Duration::from_millis(500))
|
||||
.timeout(Duration::from_secs(10));
|
||||
let merged = messages.merge(intervals).take(20);
|
||||
let mut stream = pin!(merged);
|
||||
|
||||
while let Some(result) = stream.next().await {
|
||||
match result {
|
||||
Ok(item) => println!("{item}"),
|
||||
Err(reason) => eprintln!("Problem: {reason:?}"),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn get_messages() -> impl Stream<Item = String> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
trpl::spawn_task(async move {
|
||||
let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
|
||||
|
||||
for (index, message) in messages.into_iter().enumerate() {
|
||||
let time_to_sleep = if index % 2 == 0 { 100 } else { 300 };
|
||||
trpl::sleep(Duration::from_millis(time_to_sleep)).await;
|
||||
|
||||
if let Err(send_error) = tx.send(format!("Message: '{message}'")) {
|
||||
eprintln!("Cannot send message '{message}': {send_error}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
|
||||
// ANCHOR: threads
|
||||
fn get_intervals() -> impl Stream<Item = u32> {
|
||||
let (tx, rx) = trpl::channel();
|
||||
|
||||
// This is *not* `trpl::spawn` but `std::thread::spawn`!
|
||||
thread::spawn(move || {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
// Likewise, this is *not* `trpl::sleep` but `std::thread::sleep`!
|
||||
thread::sleep(Duration::from_millis(1));
|
||||
count += 1;
|
||||
|
||||
if let Err(send_error) = tx.send(count) {
|
||||
eprintln!("Could not send interval {count}: {send_error}");
|
||||
break;
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
ReceiverStream::new(rx)
|
||||
}
|
||||
// ANCHOR_END: threads
|
||||
Reference in New Issue
Block a user