Skip to content

Commit

Permalink
add exists function in fs
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Jan 4, 2021
1 parent 3b6bee8 commit dd80958
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tokio/src/fs/exists.rs
Original file line number Diff line number Diff line change
@@ -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<Path>) -> bool {
super::metadata(&path).await.is_ok()
}
3 changes: 3 additions & 0 deletions tokio/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit dd80958

Please sign in to comment.