From e690b55b18f086fa4aa1adf9833bb3603e0e7872 Mon Sep 17 00:00:00 2001 From: Wouter Geraedts Date: Sun, 13 Oct 2019 13:52:51 +0200 Subject: [PATCH] Implemented fs::metadata and Path::exists --- src/fs/metadata.rs | 5 ++--- src/path/path.rs | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/fs/metadata.rs b/src/fs/metadata.rs index 2c9e41e..ef99a56 100644 --- a/src/fs/metadata.rs +++ b/src/fs/metadata.rs @@ -1,8 +1,7 @@ -use std::path::Path; - use cfg_if::cfg_if; use crate::io; +use crate::path::Path; use crate::task::blocking; /// Reads metadata for a path. @@ -36,7 +35,7 @@ use crate::task::blocking; /// # Ok(()) }) } /// ``` pub async fn metadata>(path: P) -> io::Result { - 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::metadata(path) }).await } diff --git a/src/path/path.rs b/src/path/path.rs index 0a93ac6..e7353e9 100644 --- a/src/path/path.rs +++ b/src/path/path.rs @@ -75,6 +75,31 @@ impl Path { self.inner.components() } + /// Returns `true` if the path points at an existing entity. + /// + /// This function will traverse symbolic links to query information about the + /// destination file. In case of broken symbolic links this will return `false`. + /// + /// If you cannot access the directory containing the file, e.g., because of a + /// permission error, this will return `false`. + /// + /// # Examples + /// + /// ```no_run + /// use async_std::path::Path; + /// assert_eq!(Path::new("does_not_exist.txt").exists(), false); + /// ``` + /// + /// # See Also + /// + /// This is a convenience function that coerces errors to false. If you want to + /// check errors, call [fs::metadata]. + /// + /// [fs::metadata]: ../fs/fn.metadata.html + pub async fn exists(&self) -> bool { + fs::metadata(self).await.is_ok() + } + /// Directly wraps a string slice as a `Path` slice. /// /// This is a cost-free conversion.