From a35be4e535039bb082b4d107bf18ce67805daeb0 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 11 Sep 2023 14:44:44 -0300 Subject: [PATCH] Trying block_on --- blockontest/Cargo.lock | 179 ++++++++++++++++++++++++++++++++++++++++ blockontest/Cargo.toml | 10 +++ blockontest/README.md | 4 + blockontest/src/main.rs | 57 +++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 blockontest/Cargo.lock create mode 100644 blockontest/Cargo.toml create mode 100644 blockontest/README.md create mode 100644 blockontest/src/main.rs diff --git a/blockontest/Cargo.lock b/blockontest/Cargo.lock new file mode 100644 index 0000000..dfb6368 --- /dev/null +++ b/blockontest/Cargo.lock @@ -0,0 +1,179 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "async-recursion" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd55a5ba1179988837d24ab4c7cc8ed6efdeff578ede0416b4225a5fca35bd0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "blockontest" +version = "0.1.0" +dependencies = [ + "async-recursion", + "tokio", +] + +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "gimli" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" + +[[package]] +name = "memchr" +version = "2.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" + +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + +[[package]] +name = "object" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" +dependencies = [ + "memchr", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "proc-macro2" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + +[[package]] +name = "syn" +version = "2.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17ed6077ed6cd6c74735e21f37eb16dc3935f96878b1fe961074089cc80893f9" +dependencies = [ + "backtrace", + "bytes", + "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" diff --git a/blockontest/Cargo.toml b/blockontest/Cargo.toml new file mode 100644 index 0000000..121afd6 --- /dev/null +++ b/blockontest/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "blockontest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-recursion = "1.0.5" +tokio = { version = "1.32.0", features = ["rt", "fs", "io-util", "macros"] } diff --git a/blockontest/README.md b/blockontest/README.md new file mode 100644 index 0000000..007441d --- /dev/null +++ b/blockontest/README.md @@ -0,0 +1,4 @@ +# BlockOnTest + +Checking if `block_on` can run the inner context as async while the outer +context gets paused. diff --git a/blockontest/src/main.rs b/blockontest/src/main.rs new file mode 100644 index 0000000..1bf5d40 --- /dev/null +++ b/blockontest/src/main.rs @@ -0,0 +1,57 @@ +use std::{ + future::Future, + path::{Path, PathBuf}, +}; + +use tokio::{ + fs::File, + io::{AsyncBufReadExt, BufReader}, runtime::Handle, +}; + +pub async fn async_walk_files(start: &Path, on_file: T) +where + T: Fn(PathBuf) -> F + Send + std::marker::Sync + std::marker::Copy, + F: Future + Send, +{ + inner_async_walk_files(start, start, on_file).await; +} + +#[async_recursion::async_recursion] +async fn inner_async_walk_files(base: &Path, current: &Path, on_file: T) +where + T: Fn(PathBuf) -> F + Send + std::marker::Sync + std::marker::Copy, + F: Future + Send, +{ + println!("Walk: {:?}", current); + let dir_contents = tokio::fs::read_dir(current).await; + if let Ok(mut content) = dir_contents { + while let Ok(Some(entry)) = content.next_entry().await { + let entry = entry.path(); + if entry.is_file() { + on_file(entry).await; + } else if entry.is_dir() { + inner_async_walk_files(base, &entry, on_file).await; + } + } + } +} + +#[tokio::main(flavor = "current_thread")] +async fn main() { + async_walk_files(Path::new(".."), |path| async move { + // This doesn't work 'cause, on a single thread runtime, the working thread would be + // blocked, and Tokio doesn't like it. + let handle = Handle::current(); + handle.block_on(async move { + println!("File: {:?}", path); + let file = File::open(&path).await.unwrap(); + let reader = BufReader::new(file); + let mut lines = reader.lines(); + + while let Ok(Some(line)) = lines.next_line().await { + println!("Line: {path:?} -> {line:?}"); + } + }) + }) + .await; +}