Implemented Path::read_link

yoshuawuyts-patch-1
Wouter Geraedts 5 years ago
parent 89f73d3eda
commit d349333a43

@ -1,6 +1,5 @@
use std::path::{Path, PathBuf};
use crate::io; use crate::io;
use crate::path::{Path, PathBuf};
use crate::task::blocking; use crate::task::blocking;
/// Reads a symbolic link and returns the path it points to. /// Reads a symbolic link and returns the path it points to.
@ -28,6 +27,8 @@ use crate::task::blocking;
/// # Ok(()) }) } /// # Ok(()) }) }
/// ``` /// ```
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> { pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
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_link(path) }).await Ok(blocking::spawn(async move { std::fs::read_link(path) })
.await?
.into())
} }

@ -529,6 +529,7 @@ impl Path {
/// # /// #
/// use async_std::path::Path; /// use async_std::path::Path;
/// use async_std::fs; /// use async_std::fs;
/// use futures_util::stream::StreamExt;
/// ///
/// let path = Path::new("/laputa"); /// let path = Path::new("/laputa");
/// let mut dir = fs::read_dir(&path).await.expect("read_dir call failed"); /// let mut dir = fs::read_dir(&path).await.expect("read_dir call failed");
@ -543,6 +544,28 @@ impl Path {
fs::read_dir(self).await fs::read_dir(self).await
} }
/// Reads a symbolic link, returning the file that the link points to.
///
/// This is an alias to [`fs::read_link`].
///
/// [`fs::read_link`]: ../fs/fn.read_link.html
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::path::Path;
///
/// let path = Path::new("/laputa/sky_castle.rs");
/// let path_link = path.read_link().await.expect("read_link call failed");
/// #
/// # Ok(()) }) }
/// ```
pub async fn read_link(&self) -> io::Result<PathBuf> {
fs::read_link(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`].

Loading…
Cancel
Save