mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-30 17:25:32 +00:00
Implemented Path::read_dir
This commit is contained in:
parent
141954d205
commit
89f73d3eda
2 changed files with 40 additions and 2 deletions
|
@ -1,9 +1,9 @@
|
||||||
use std::path::Path;
|
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use crate::fs::DirEntry;
|
use crate::fs::DirEntry;
|
||||||
use crate::future::Future;
|
use crate::future::Future;
|
||||||
use crate::io;
|
use crate::io;
|
||||||
|
use crate::path::Path;
|
||||||
use crate::stream::Stream;
|
use crate::stream::Stream;
|
||||||
use crate::task::{blocking, Context, JoinHandle, Poll};
|
use crate::task::{blocking, Context, JoinHandle, Poll};
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll};
|
||||||
/// # Ok(()) }) }
|
/// # Ok(()) }) }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
|
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
|
||||||
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) })
|
blocking::spawn(async move { std::fs::read_dir(path) })
|
||||||
.await
|
.await
|
||||||
.map(ReadDir::new)
|
.map(ReadDir::new)
|
||||||
|
|
|
@ -511,6 +511,38 @@ impl Path {
|
||||||
self.inner.parent().map(|p| p.into())
|
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::ReadDir> {
|
||||||
|
fs::read_dir(self).await
|
||||||
|
}
|
||||||
|
|
||||||
/// Queries the metadata about a file without following symlinks.
|
/// Queries the metadata about a file without following symlinks.
|
||||||
///
|
///
|
||||||
/// This is an alias to [`fs::symlink_metadata`].
|
/// This is an alias to [`fs::symlink_metadata`].
|
||||||
|
@ -586,3 +618,9 @@ impl AsRef<Path> for str {
|
||||||
Path::new(self)
|
Path::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<Path> for String {
|
||||||
|
fn as_ref(&self) -> &Path {
|
||||||
|
Path::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue