use futures_core::stream::Stream;
use std::ffi::OsString;
use std::fs::{self, DirEntry as StdDirEntry, FileType, Metadata, ReadDir as StdReadDir};
use std::future::Future;
use std::io;
#[cfg(unix)]
use std::os::unix::fs::DirEntryExt;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
/// Returns a stream over the entries within a directory.
///
/// This is an async version of [`std::fs::read_dir`][std]
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.read_dir.html
pub fn read_dir
(path: P) -> ReadDirFuture
where
P: AsRef + Send + 'static,
{
ReadDirFuture::new(path)
}
/// Future returned by `read_dir`.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReadDirFuture
where
P: AsRef + Send + 'static,
{
type Output = io::Result;
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll {
crate::blocking_io(|| Ok(ReadDir(fs::read_dir(&self.path)?)))
}
}
/// Stream of the entries in a directory.
///
/// This stream is returned from the [`read_dir`] function of this module and
/// will yield instances of [`DirEntry`]. Through a [`DirEntry`]
/// information like the entry's path and possibly other metadata can be
/// learned.
///
/// # Errors
///
/// This [`Stream`] will return an [`Err`] if there's some sort of intermittent
/// IO error during iteration.
///
/// [`read_dir`]: fn.read_dir.html
/// [`DirEntry`]: struct.DirEntry.html
/// [`Stream`]: ../futures/stream/trait.Stream.html
/// [`Err`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct ReadDir(StdReadDir);
impl Stream for ReadDir {
type Item = io::Result;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll