Implemented Path::read_link

pull/320/head
Wouter Geraedts 5 years ago
parent 89f73d3eda
commit d349333a43

@ -1,6 +1,5 @@
use std::path::{Path, PathBuf};
use crate::io;
use crate::path::{Path, PathBuf};
use crate::task::blocking;
/// Reads a symbolic link and returns the path it points to.
@ -28,6 +27,8 @@ use crate::task::blocking;
/// # Ok(()) }) }
/// ```
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let path = path.as_ref().to_owned();
blocking::spawn(async move { std::fs::read_link(path) }).await
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
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::fs;
/// use futures_util::stream::StreamExt;
///
/// let path = Path::new("/laputa");
/// let mut dir = fs::read_dir(&path).await.expect("read_dir call failed");
@ -543,6 +544,28 @@ impl Path {
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.
///
/// This is an alias to [`fs::symlink_metadata`].

Loading…
Cancel
Save