Skip to content

Commit 5345532

Browse files
committed
add exists function in fs
Fixes: #3373
1 parent 3b6bee8 commit 5345532

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tokio/src/fs/exists.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::path::Path;
2+
3+
/// Returns `true` if the path points at an existing entity.
4+
///
5+
/// This function will traverse symbolic links to query information about the
6+
/// destination file.
7+
///
8+
/// # Examples
9+
///
10+
/// ```rust,no_run
11+
/// # use tokio::fs;
12+
///
13+
/// # #[tokio::main]
14+
/// # async fn main() {
15+
/// let exists = fs::exists("/some/file/path.txt").await;
16+
/// // in case the `/some/file/path.txt` points to existing path
17+
/// assert_eq!(exists, true);
18+
/// # }
19+
/// ```
20+
///
21+
/// # Note
22+
///
23+
/// This method returns false in case of errors thus ignoring them. Use [`fs::metadata`][super::metadata]
24+
/// if you want to check for errors.
25+
pub async fn exists(path: impl AsRef<Path>) -> bool {
26+
super::metadata(&path).await.is_ok()
27+
}

tokio/src/fs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub use self::create_dir_all::create_dir_all;
3636
mod dir_builder;
3737
pub use self::dir_builder::DirBuilder;
3838

39+
mod exists;
40+
pub use self::exists::exists;
41+
3942
mod file;
4043
pub use self::file::File;
4144

0 commit comments

Comments
 (0)