From 3089b5677832cb54d38f8e41f3328a4da234bf3f Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 11 Apr 2024 09:54:56 -0300 Subject: [PATCH] Made a simple async iterator out of pure async --- asyncnext/Cargo.lock | 157 ++++++++++++++++++++++++++++++++++++++++++ asyncnext/Cargo.toml | 9 +++ asyncnext/README.md | 3 + asyncnext/src/main.rs | 54 +++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 asyncnext/Cargo.lock create mode 100644 asyncnext/Cargo.toml create mode 100644 asyncnext/README.md create mode 100644 asyncnext/src/main.rs diff --git a/asyncnext/Cargo.lock b/asyncnext/Cargo.lock new file mode 100644 index 0000000..19c853c --- /dev/null +++ b/asyncnext/Cargo.lock @@ -0,0 +1,157 @@ +# 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 = "asyncnext" +version = "0.1.0" +dependencies = [ + "tokio", +] + +[[package]] +name = "backtrace" +version = "0.3.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "cc" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2678b2e3449475e95b0aa6f9b506a28e61b3dc8996592b983695e8ebb58a8b41" + +[[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.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + +[[package]] +name = "miniz_oxide" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +dependencies = [ + "adler", +] + +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "proc-macro2" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +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.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +dependencies = [ + "backtrace", + "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/asyncnext/Cargo.toml b/asyncnext/Cargo.toml new file mode 100644 index 0000000..339eabd --- /dev/null +++ b/asyncnext/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "asyncnext" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1.37.0", features = ["rt", "fs", "macros"] } diff --git a/asyncnext/README.md b/asyncnext/README.md new file mode 100644 index 0000000..7527dc8 --- /dev/null +++ b/asyncnext/README.md @@ -0,0 +1,3 @@ +# AsyncNext + +Trying to write my own async next() thingy. diff --git a/asyncnext/src/main.rs b/asyncnext/src/main.rs new file mode 100644 index 0000000..16243dd --- /dev/null +++ b/asyncnext/src/main.rs @@ -0,0 +1,54 @@ +use std::path::Path; +use std::path::PathBuf; + +use tokio::fs::ReadDir; + +struct Walker { + path: PathBuf, +} + +impl Walker { + pub fn new(base: &Path) -> Self { + Self { + path: base.to_path_buf(), + } + } + + pub async fn children(&self) -> WalkerIterator { + WalkerIterator::new(&self.path).await + } +} + +struct WalkerIterator { + walking: ReadDir, +} + +impl WalkerIterator { + async fn new(path: &Path) -> Self { + Self { + walking: tokio::fs::read_dir(&path).await.unwrap(), + } + } + + async fn next(&mut self) -> Option { + let current = self.walking.next_entry().await.ok()??; + Some(current.file_name().to_string_lossy().to_string()) + } +} + +#[tokio::main(flavor = "current_thread")] +async fn main() { + let mut args = std::env::args(); + let _ = args.next(); + let base_path = args.next().unwrap(); + + let walker = Walker::new(&Path::new(&base_path).canonicalize().unwrap()); + let mut iter = walker.children().await; + while let Some(child) = iter.next().await { + println!("{child}"); + } + + //while let Some(child) = walker.children().await.next().await { + // println!("{child}") + //} +}