From 6d1514029ce263dc63e7c1b7a3f3830b3a5559ea Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 11 Sep 2023 11:51:28 -0300 Subject: [PATCH] No options for recursive calls --- tokiostreamtest/Cargo.lock | 12 ++++++++++++ tokiostreamtest/Cargo.toml | 1 + tokiostreamtest/src/main.rs | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/tokiostreamtest/Cargo.lock b/tokiostreamtest/Cargo.lock index 622efda..35bd6be 100644 --- a/tokiostreamtest/Cargo.lock +++ b/tokiostreamtest/Cargo.lock @@ -17,6 +17,17 @@ 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 = "async-stream" version = "0.3.5" @@ -237,6 +248,7 @@ dependencies = [ name = "tokiostreamtest" version = "0.1.0" dependencies = [ + "async-recursion", "async-stream", "futures-core", "futures-util", diff --git a/tokiostreamtest/Cargo.toml b/tokiostreamtest/Cargo.toml index ce8183c..73abb41 100644 --- a/tokiostreamtest/Cargo.toml +++ b/tokiostreamtest/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-recursion = "1.0.5" async-stream = "0.3.5" futures-core = "0.3.28" futures-util = "0.3.28" diff --git a/tokiostreamtest/src/main.rs b/tokiostreamtest/src/main.rs index 51128bd..73758ea 100644 --- a/tokiostreamtest/src/main.rs +++ b/tokiostreamtest/src/main.rs @@ -4,7 +4,8 @@ use async_stream::stream; use futures_core::Stream; use futures_util::{StreamExt, pin_mut}; -fn filenames(start: &Path) -> impl Stream + '_ { +#[async_recursion::async_recursion] +async fn filenames(start: &Path) -> impl Stream + '_ { stream! { let contents = tokio::fs::read_dir(start).await; if let Ok(mut reader) = contents { @@ -12,6 +13,35 @@ fn filenames(start: &Path) -> impl Stream + '_ { let entry = entry.path(); if entry.is_file() { yield entry; + } else if entry.is_dir() { + // And this doesn't work 'cause we can't yield its results and we can't return + // it due the return types being different. + let under_dir = filenames(&entry).await; + pin_mut!(under_dir); + while let Some(name) = under_dir.next().await { + yield name + } + } + } + } + } +} + +fn sync_filenames(start: &Path) -> impl Stream + '_ { + stream! { + let contents = start.read_dir(); + if let Ok(reader) = contents { + for entry in reader { + let entry = entry.unwrap().path(); + if entry.is_file() { + yield entry; + } else if entry.is_dir() { + let under_dir = sync_filenames(&entry); + pin_mut!(under_dir); + + while let Some(name) = under_dir.next().await { + yield name + } } } } @@ -20,7 +50,9 @@ fn filenames(start: &Path) -> impl Stream + '_ { #[tokio::main(flavor = "current_thread")] async fn main() { - let name_stream = filenames(Path::new("src")); + // let name_stream = filenames(Path::new(".")).await; + // pin_mut!(name_stream); + let name_stream = sync_filenames(Path::new(".")); pin_mut!(name_stream); while let Some(name) = name_stream.next().await {