From 7d4b9279eebaff46ab017ea8ce3913d52bb70722 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu@web.de> Date: Sat, 28 Nov 2020 16:42:26 +0100 Subject: [PATCH] Terminate once all tasks are complete --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 9 +++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d58b33b..c4caed9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,7 @@ name = "KIT-ILIAS-downloader" version = "0.2.10" dependencies = [ "anyhow", + "futures", "futures-channel", "futures-util", "ignore", @@ -374,6 +375,21 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b3b0c040a1fe6529d30b3c5944b280c7f0dcb2930d2c3062bca967b602583d0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.8" @@ -381,6 +397,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b7109687aa4e177ef6fe84553af6280ef2778bdb7783ba44c9dc3399110fe64" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -389,6 +406,23 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748" +[[package]] +name = "futures-executor" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4caa2b2b68b880003057c1dd49f1ed937e38f22fcf6c212188a121f08cf40a65" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb" + [[package]] name = "futures-macro" version = "0.3.8" @@ -422,9 +456,13 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d304cff4a7b99cfb7986f7d43fbe93d175e72e704a8860787cc95e9ffd85cbd2" dependencies = [ + "futures-channel", "futures-core", + "futures-io", "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project 1.0.2", "pin-utils", "proc-macro-hack", diff --git a/Cargo.toml b/Cargo.toml index 2bc7454..3b7775f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ tokio = { version = "0.2", features = ["full"] } serde_json = "1.0.51" scraper = "0.12.0" url = "2.1.1" +futures = "0.3.8" futures-util = "0.3.8" futures-channel = "0.3.8" regex = "1.3.7" diff --git a/src/main.rs b/src/main.rs index e5eef11..df28fe5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use anyhow::{Context, Result, anyhow}; +use futures::future::{self, Either}; use futures_channel::mpsc::UnboundedSender; use futures_util::{StreamExt, stream::TryStreamExt}; use ignore::gitignore::Gitignore; @@ -94,8 +95,12 @@ async fn main() { }, Err(e) => println!("{:?}", e) } - while let Some(task) = rx.next().await { - let _ = task.await; + loop { + match future::select(rx.next(), future::ready(())).await { + // if the channel is empty, all tasks are completed + Either::Left((task, _)) => if let Some(task) = task { let _ = task.await; } else { break }, + Either::Right(_) => break + } } if ilias.opt.content_tree { // restore fast page loading times