From 89c0f50b09e1f37b605f262aa8ffd3d2f4ee1c18 Mon Sep 17 00:00:00 2001 From: Max Wase Date: Thu, 27 May 2021 16:04:08 +0300 Subject: [PATCH] Fix `is_symlink()` method for `Path` using added `is_symlink()` method for `Metadata` --- library/std/src/fs.rs | 24 ++++++++++++++++++++++++ library/std/src/path.rs | 5 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index a1636e2f604..97b5e26bd0b 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1007,6 +1007,30 @@ impl Metadata { self.file_type().is_file() } + /// Returns `true` if this metadata is for a symbolic link file. + /// + /// # Examples + /// + /// ```no_run + /// use std::fs; + /// use std::path::Path; + /// use std::os::unix::fs::symlink; + /// + /// fn main() -> std::io::Result<()> { + /// let link_path = Path::new("/link"); + /// symlink("/origin_does_not_exists/", link_path)?; + /// + /// let metadata = fs::symlink_metadata(link_path)?; + /// + /// assert!(metadata.is_symlink()); + /// Ok(()) + /// } + /// ``` + #[unstable(feature = "is_symlink", issue = "none")] + pub fn is_symlink(&self) -> bool { + self.file_type().is_symlink() + } + /// Returns the size of the file, in bytes, this metadata is for. /// /// # Examples diff --git a/library/std/src/path.rs b/library/std/src/path.rs index efee31f9915..2023a8448aa 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2588,10 +2588,9 @@ impl Path { /// assert_eq!(link_path.is_symlink(), true); /// assert_eq!(link_path.exists(), false); /// ``` - #[unstable(feature = "path_ext", issue = "none")] - #[inline] + #[unstable(feature = "is_symlink", issue = "none")] pub fn is_symlink(&self) -> bool { - fs::symlink_metadata(self).is_ok() + fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false) } /// Converts a [`Box`](Box) into a [`PathBuf`] without copying or