Skip to content

Commit d349333

Browse files
committed
Implemented Path::read_link
1 parent 89f73d3 commit d349333

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/fs/read_link.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::path::{Path, PathBuf};
2-
31
use crate::io;
2+
use crate::path::{Path, PathBuf};
43
use crate::task::blocking;
54

65
/// Reads a symbolic link and returns the path it points to.
@@ -28,6 +27,8 @@ use crate::task::blocking;
2827
/// # Ok(()) }) }
2928
/// ```
3029
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
31-
let path = path.as_ref().to_owned();
32-
blocking::spawn(async move { std::fs::read_link(path) }).await
30+
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
31+
Ok(blocking::spawn(async move { std::fs::read_link(path) })
32+
.await?
33+
.into())
3334
}

src/path/path.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ impl Path {
529529
/// #
530530
/// use async_std::path::Path;
531531
/// use async_std::fs;
532+
/// use futures_util::stream::StreamExt;
532533
///
533534
/// let path = Path::new("/laputa");
534535
/// let mut dir = fs::read_dir(&path).await.expect("read_dir call failed");
@@ -543,6 +544,28 @@ impl Path {
543544
fs::read_dir(self).await
544545
}
545546

547+
/// Reads a symbolic link, returning the file that the link points to.
548+
///
549+
/// This is an alias to [`fs::read_link`].
550+
///
551+
/// [`fs::read_link`]: ../fs/fn.read_link.html
552+
///
553+
/// # Examples
554+
///
555+
/// ```no_run
556+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
557+
/// #
558+
/// use async_std::path::Path;
559+
///
560+
/// let path = Path::new("/laputa/sky_castle.rs");
561+
/// let path_link = path.read_link().await.expect("read_link call failed");
562+
/// #
563+
/// # Ok(()) }) }
564+
/// ```
565+
pub async fn read_link(&self) -> io::Result<PathBuf> {
566+
fs::read_link(self).await
567+
}
568+
546569
/// Queries the metadata about a file without following symlinks.
547570
///
548571
/// This is an alias to [`fs::symlink_metadata`].

0 commit comments

Comments
 (0)