Browse Source

No options for recursive calls

master
Julio Biason 1 year ago
parent
commit
6d1514029c
  1. 12
      tokiostreamtest/Cargo.lock
  2. 1
      tokiostreamtest/Cargo.toml
  3. 36
      tokiostreamtest/src/main.rs

12
tokiostreamtest/Cargo.lock generated

@ -17,6 +17,17 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 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]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.5" version = "0.3.5"
@ -237,6 +248,7 @@ dependencies = [
name = "tokiostreamtest" name = "tokiostreamtest"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-recursion",
"async-stream", "async-stream",
"futures-core", "futures-core",
"futures-util", "futures-util",

1
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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
async-recursion = "1.0.5"
async-stream = "0.3.5" async-stream = "0.3.5"
futures-core = "0.3.28" futures-core = "0.3.28"
futures-util = "0.3.28" futures-util = "0.3.28"

36
tokiostreamtest/src/main.rs

@ -4,7 +4,8 @@ use async_stream::stream;
use futures_core::Stream; use futures_core::Stream;
use futures_util::{StreamExt, pin_mut}; use futures_util::{StreamExt, pin_mut};
fn filenames(start: &Path) -> impl Stream<Item = PathBuf> + '_ { #[async_recursion::async_recursion]
async fn filenames(start: &Path) -> impl Stream<Item = PathBuf> + '_ {
stream! { stream! {
let contents = tokio::fs::read_dir(start).await; let contents = tokio::fs::read_dir(start).await;
if let Ok(mut reader) = contents { if let Ok(mut reader) = contents {
@ -12,6 +13,35 @@ fn filenames(start: &Path) -> impl Stream<Item = PathBuf> + '_ {
let entry = entry.path(); let entry = entry.path();
if entry.is_file() { if entry.is_file() {
yield entry; 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<Item = PathBuf> + '_ {
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<Item = PathBuf> + '_ {
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { 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); pin_mut!(name_stream);
while let Some(name) = name_stream.next().await { while let Some(name) = name_stream.next().await {

Loading…
Cancel
Save