How to read a directory in tokio

I'm trying to read a directory using async I/O in tokio. This is as close as I've got:

    async fn read_dir() -> std::io::Result<()> {
        let mut reader = tokio::fs::read_dir("dir_name").await?;
        loop {
            if let Some(f) = reader.next().await? {
                // TODO
            } else {
                break;
            }
        }
        Ok(())
    }

The compiler error says that tokio::ReadDir does not satisfy the trait bound ReadDir : Stream, but there is such an implementation here, so I'm baffled.

Any help appreciated.

Those are the docs of tokio_fs, and its crates.io page says

This crate has been deprecated in tokio 0.2.x and has been moved into tokio::fs behind the fs feature flag.

tokio's docs say

A ReadDir can be turned into a Stream with ReadDirStream .

though if you don't already depend on tokio_stream it might be easier to just use ReadDir::next_entry in this case.

1 Like