diff --git a/Cargo.lock b/Cargo.lock index f0d46c5..1e2c719 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -103,6 +103,37 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + [[package]] name = "futures" version = "0.3.30" @@ -371,6 +402,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.5.1" @@ -386,6 +437,7 @@ version = "0.1.0" dependencies = [ "chrono", "futures", + "rayon", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index b8a9a9a..bf45dfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] chrono = "0.4.23" futures = "0.3.26" +rayon = "1.6.1" tokio = { version = "1.25.0", features = ["full"]} diff --git a/src/main.rs b/src/main.rs index b3a80e4..514017c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ use chrono::{DateTime, Datelike, Days, NaiveDate, NaiveTime, Weekday}; -use futures::future::join_all; +use rayon::prelude::*; use std::collections::BTreeMap; +use std::thread; use std::time::Duration; -#[tokio::main] -async fn main() { +fn main() { println!("Hello, world!"); let start = NaiveDate::from_isoywd_opt(2023, 1, Weekday::Mon) @@ -24,13 +24,8 @@ async fn main() { .map(DateTime::date_naive) .collect(); - let futures = dates.iter().map(fetch); - - let result: Vec<(&NaiveDate, Vec)> = join_all(futures) - .await - .into_iter() - .filter_map(Result::ok) - .collect(); + let result: Vec<(&NaiveDate, Vec)> = + dates.par_iter().map(fetch).filter_map(Result::ok).collect(); let map = BTreeMap::from_iter(result); @@ -43,9 +38,9 @@ struct Response { day: u8, } -async fn fetch(date: &NaiveDate) -> Result<(&NaiveDate, Vec), ()> { +fn fetch(date: &NaiveDate) -> Result<(&NaiveDate, Vec), ()> { println!("Sleeping..."); - tokio::time::sleep(Duration::from_secs(1)).await; + thread::sleep(Duration::from_secs(1)); let responses = vec![ Response {