Skip to content

Commit 89f73d3

Browse files
committed
Implemented Path::read_dir
1 parent 141954d commit 89f73d3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/fs/read_dir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::path::Path;
21
use std::pin::Pin;
32

43
use crate::fs::DirEntry;
54
use crate::future::Future;
65
use crate::io;
6+
use crate::path::Path;
77
use crate::stream::Stream;
88
use crate::task::{blocking, Context, JoinHandle, Poll};
99

@@ -44,7 +44,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll};
4444
/// # Ok(()) }) }
4545
/// ```
4646
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
47-
let path = path.as_ref().to_owned();
47+
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
4848
blocking::spawn(async move { std::fs::read_dir(path) })
4949
.await
5050
.map(ReadDir::new)

src/path/path.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,38 @@ impl Path {
511511
self.inner.parent().map(|p| p.into())
512512
}
513513

514+
/// Returns an iterator over the entries within a directory.
515+
///
516+
/// The iterator will yield instances of [`io::Result`]`<`[`DirEntry`]`>`. New
517+
/// errors may be encountered after an iterator is initially constructed.
518+
///
519+
/// This is an alias to [`fs::read_dir`].
520+
///
521+
/// [`io::Result`]: ../io/type.Result.html
522+
/// [`DirEntry`]: ../fs/struct.DirEntry.html
523+
/// [`fs::read_dir`]: ../fs/fn.read_dir.html
524+
///
525+
/// # Examples
526+
///
527+
/// ```no_run
528+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
529+
/// #
530+
/// use async_std::path::Path;
531+
/// use async_std::fs;
532+
///
533+
/// let path = Path::new("/laputa");
534+
/// let mut dir = fs::read_dir(&path).await.expect("read_dir call failed");
535+
/// while let Some(res) = dir.next().await {
536+
/// let entry = res?;
537+
/// println!("{}", entry.file_name().to_string_lossy());
538+
/// }
539+
/// #
540+
/// # Ok(()) }) }
541+
/// ```
542+
pub async fn read_dir(&self) -> io::Result<fs::ReadDir> {
543+
fs::read_dir(self).await
544+
}
545+
514546
/// Queries the metadata about a file without following symlinks.
515547
///
516548
/// This is an alias to [`fs::symlink_metadata`].
@@ -586,3 +618,9 @@ impl AsRef<Path> for str {
586618
Path::new(self)
587619
}
588620
}
621+
622+
impl AsRef<Path> for String {
623+
fn as_ref(&self) -> &Path {
624+
Path::new(self)
625+
}
626+
}

0 commit comments

Comments
 (0)