diff --git a/src/fs/read_dir.rs b/src/fs/read_dir.rs index 9b4269d..fe3ff2e 100644 --- a/src/fs/read_dir.rs +++ b/src/fs/read_dir.rs @@ -1,9 +1,9 @@ -use std::path::Path; use std::pin::Pin; use crate::fs::DirEntry; use crate::future::Future; use crate::io; +use crate::path::Path; use crate::stream::Stream; use crate::task::{blocking, Context, JoinHandle, Poll}; @@ -44,7 +44,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll}; /// # Ok(()) }) } /// ``` pub async fn read_dir>(path: P) -> io::Result { - let path = path.as_ref().to_owned(); + let path: std::path::PathBuf = path.as_ref().to_path_buf().into(); blocking::spawn(async move { std::fs::read_dir(path) }) .await .map(ReadDir::new) diff --git a/src/path/path.rs b/src/path/path.rs index 55a7922..88d7ba8 100644 --- a/src/path/path.rs +++ b/src/path/path.rs @@ -511,6 +511,38 @@ impl Path { self.inner.parent().map(|p| p.into()) } + /// Returns an iterator over the entries within a directory. + /// + /// The iterator will yield instances of [`io::Result`]`<`[`DirEntry`]`>`. New + /// errors may be encountered after an iterator is initially constructed. + /// + /// This is an alias to [`fs::read_dir`]. + /// + /// [`io::Result`]: ../io/type.Result.html + /// [`DirEntry`]: ../fs/struct.DirEntry.html + /// [`fs::read_dir`]: ../fs/fn.read_dir.html + /// + /// # Examples + /// + /// ```no_run + /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # + /// use async_std::path::Path; + /// use async_std::fs; + /// + /// let path = Path::new("/laputa"); + /// let mut dir = fs::read_dir(&path).await.expect("read_dir call failed"); + /// while let Some(res) = dir.next().await { + /// let entry = res?; + /// println!("{}", entry.file_name().to_string_lossy()); + /// } + /// # + /// # Ok(()) }) } + /// ``` + pub async fn read_dir(&self) -> io::Result { + fs::read_dir(self).await + } + /// Queries the metadata about a file without following symlinks. /// /// This is an alias to [`fs::symlink_metadata`]. @@ -586,3 +618,9 @@ impl AsRef for str { Path::new(self) } } + +impl AsRef for String { + fn as_ref(&self) -> &Path { + Path::new(self) + } +}