Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fs::exists function #3375

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
///
/// This is an async version of [`std::path::Path::exists`][std]

/// This function will traverse symbolic links to query information about the
/// destination file.
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
///
/// [std]: std::path::Path::exists

/// # Examples
///
/// ```rust,no_run
/// # use tokio::fs;
///
/// # #[tokio::main]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example in src/fs/metadata.rs does not hide these lines. Maybe we should be consistent and show these lines?

/// # 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
super::metadata(&path).await.is_ok()
let path = path.as_ref().to_owned();
asyncify(|| path.exists()).await

Why not directly use the exists method from std?

}
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