diff --git a/tokio/src/fs/exists.rs b/tokio/src/fs/exists.rs new file mode 100644 index 00000000000..d6fa5ba03d8 --- /dev/null +++ b/tokio/src/fs/exists.rs @@ -0,0 +1,27 @@ +use std::path::Path; + +/// Returns `true` if the path points at an existing entity. +/// +/// This function will traverse symbolic links to query information about the +/// destination file. +/// +/// # Examples +/// +/// ```rust,no_run +/// # use tokio::fs; +/// +/// # #[tokio::main] +/// # async fn main() { +/// let exists = fs::exists("/some/file/path.txt").await; +/// // in case the `/some/file/path.txt` points to existing path +/// assert_eq!(exists, true); +/// # } +/// ``` +/// +/// # Note +/// +/// This method returns false in case of errors thus ignoring them. Use [`fs::metadata`][super::metadata()] +/// if you want to check for errors. +pub async fn exists(path: impl AsRef) -> bool { + super::metadata(&path).await.is_ok() +} diff --git a/tokio/src/fs/mod.rs b/tokio/src/fs/mod.rs index d4f00749028..990158287c5 100644 --- a/tokio/src/fs/mod.rs +++ b/tokio/src/fs/mod.rs @@ -36,6 +36,9 @@ pub use self::create_dir_all::create_dir_all; mod dir_builder; pub use self::dir_builder::DirBuilder; +mod exists; +pub use self::exists::exists; + mod file; pub use self::file::File;