Crate async_walkdir
source ·Expand description
An utility for walking through a directory asynchronously and recursively.
Based on async-fs and blocking, it uses a thread pool to handle blocking IOs. Please refere to those crates for the rationale. This crate is compatible with any async runtime based on futures 0.3, which includes tokio, async-std and smol.
Symbolic links are walked through but they are not followed.
§Example
Recursively traverse a directory:
use async_walkdir::WalkDir;
use futures_lite::future::block_on;
use futures_lite::stream::StreamExt;
block_on(async {
let mut entries = WalkDir::new("my_directory");
loop {
match entries.next().await {
Some(Ok(entry)) => println!("file: {}", entry.path().display()),
Some(Err(e)) => {
eprintln!("error: {}", e);
break;
}
None => break,
}
}
});
Do not recurse through directories whose name starts with ‘.’:
use async_walkdir::{Filtering, WalkDir};
use futures_lite::future::block_on;
use futures_lite::stream::StreamExt;
block_on(async {
let mut entries = WalkDir::new("my_directory").filter(|entry| async move {
if let Some(true) = entry
.path()
.file_name()
.map(|f| f.to_string_lossy().starts_with('.'))
{
return Filtering::IgnoreDir;
}
Filtering::Continue
});
loop {
match entries.next().await {
Some(Ok(entry)) => println!("file: {}", entry.path().display()),
Some(Err(e)) => {
eprintln!("error: {}", e);
break;
}
None => break,
}
}
});
Re-exports§
pub use async_fs::DirEntry;
Structs§
- An error produced during a directory tree traversal.
- A
Stream
ofDirEntry
generated from recursively traversing a directory.
Enums§
- Sets the filtering behavior.
Type Aliases§
- A specialized
Result
type.