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 documentation to point to File::open or OpenOptions::open instead of is_file to check read/write possibility #73243

Merged
merged 5 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 16 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,16 @@ impl Metadata {
/// [`is_dir`], and will be false for symlink metadata
/// obtained from [`symlink_metadata`].
///
/// When the goal is simply to read from (or write to) the source, the most
/// reliable way to test the source can be read (or written to) is to open
/// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
/// a Unix-like system for example. See [`File::open`] or
/// [`OpenOptions::open`] for more information.
///
/// [`is_dir`]: struct.Metadata.html#method.is_dir
/// [`symlink_metadata`]: fn.symlink_metadata.html
/// [`File::open`]: struct.File.html#method.open
/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
///
/// # Examples
///
Expand Down Expand Up @@ -1307,8 +1315,16 @@ impl FileType {
/// [`is_dir`] and [`is_symlink`]; only zero or one of these
/// tests may pass.
///
/// When the goal is simply to read from (or write to) the source, the most
/// reliable way to test the source can be read (or written to) is to open
/// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
/// a Unix-like system for example. See [`File::open`] or
/// [`OpenOptions::open`] for more information.
///
/// [`is_dir`]: struct.FileType.html#method.is_dir
/// [`is_symlink`]: struct.FileType.html#method.is_symlink
/// [`File::open`]: struct.File.html#method.open
/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
///
/// # Examples
///
Expand Down
12 changes: 8 additions & 4 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2503,11 +2503,15 @@ impl Path {
/// # See Also
///
/// This is a convenience function that coerces errors to false. If you want to
/// check errors, call [fs::metadata] and handle its Result. Then call
/// [fs::Metadata::is_file] if it was Ok.
/// check errors, call [`fs::metadata`] and handle its Result. Then call
/// [`fs::Metadata::is_file`] if it was Ok.
///
/// [fs::metadata]: ../../std/fs/fn.metadata.html
/// [fs::Metadata::is_file]: ../../std/fs/struct.Metadata.html#method.is_file
/// Note that the explanation about using `open` instead of `is_file`
/// that is present in the [`fs::Metadata`] documentation also applies here.
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`fs::metadata`]: ../../std/fs/fn.metadata.html
/// [`fs::Metadata`]: ../../std/fs/struct.Metadata.html
/// [`fs::Metadata::is_file`]: ../../std/fs/struct.Metadata.html#method.is_file
#[stable(feature = "path_ext", since = "1.5.0")]
pub fn is_file(&self) -> bool {
fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
Expand Down