Compare commits

..

4 Commits

4 changed files with 2207 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
trpl = "0.2.0"

View File

@ -1,6 +1,85 @@
//! Stopped here:
//! https://rust-book.cs.brown.edu/ch17-02-concurrency-with-async.html
//! Concurrency With Async
//!
//! Similar to working with threads.
extern crate trpl;
use std::time::Duration;
fn main() {
println!("Hello, world!");
// trpl::run(async {
// let handle = trpl::spawn_task(async {
// for i in 1..10 {
// println!("hi number {i} from the first task!");
// trpl::sleep(Duration::from_millis(500)).await;
// }
// });
// for i in 1..5 {
// println!("hi number {i} from the second task!");
// trpl::sleep(Duration::from_millis(500)).await;
// }
// // Makes it so the program runs until both of these loops are
// // finished.
// handle.await.unwrap();
// });
trpl::run(async {
let fut1 = async {
for i in 1..10 {
println!("hi number {i} from the first task!");
trpl::sleep(Duration::from_millis(500)).await;
}
};
let fut2 = async {
for i in 1..5 {
println!("hi number {i} from the second task!");
trpl::sleep(Duration::from_millis(500)).await;
}
};
// `trpl::join` function is fair meaning: checks each
// future equally often, alternating between them, never
// lets one race ahead if the other is ready.
trpl::join(fut1, fut2).await;
// message passing
//
// Can also share data between futures like this.
// async version of mutliple-producer, single consumer.
// trpl::run(async {
let (tx, mut rx) = trpl::channel();
let tx_fut = async {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("future"),
];
for val in vals {
tx.send(val).unwrap();
trpl::sleep(Duration::from_millis(500)).await;
}
};
let rx_fut = async {
while let Some(value) = rx.recv().await {
println!("received '{value}'");
}
};
trpl::join(tx_fut, rx_fut).await;
// let val = String::from("hi");
// tx.send(val).unwrap();
// let received = rx.recv().await.unwrap();
// println!("Got: {received}");
// });
});
}

View File

@ -10,6 +10,7 @@
{
devShells."${system}".default =
pkgs.mkShellNoCC {
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [ pkgs.bashInteractive ];
packages = with pkgs; [
rustc
@ -20,6 +21,7 @@
rust-analyzer
lldb
jetbrains.rust-rover
openssl
(vscode-with-extensions.override {
vscode = vscodium;
vscodeExtensions = with vscode-extensions; [
@ -27,6 +29,7 @@
];
})
];
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
shellHook = ''
export SHELL=${pkgs.lib.getExe pkgs.bashInteractive}
'';