Skip to content

Commit 3aaa809

Browse files
authored
Rollup merge of #42926 - Havvy:doc-path-ext, r=steveklabnik
Document what happens on failure in path ext is_file is_dir r? @steveklabnik Also, what other ways could there be an error that gets discarded and returns false? Should we list them all? Should we say that any errors trying to access the metadata at that path causes it to return false, even if there might be a file or directory there? Should I add a See also link to the original functions that do return Results?
2 parents b2c0707 + a01c91c commit 3aaa809

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libstd/path.rs

+34
Original file line numberDiff line numberDiff line change
@@ -2216,12 +2216,22 @@ impl Path {
22162216
/// This function will traverse symbolic links to query information about the
22172217
/// destination file. In case of broken symbolic links this will return `false`.
22182218
///
2219+
/// If you cannot access the directory containing the file, e.g. because of a
2220+
/// permission error, this will return `false`.
2221+
///
22192222
/// # Examples
22202223
///
22212224
/// ```no_run
22222225
/// use std::path::Path;
22232226
/// assert_eq!(Path::new("does_not_exist.txt").exists(), false);
22242227
/// ```
2228+
///
2229+
/// # See Also
2230+
///
2231+
/// This is a convenience function that coerces errors to false. If you want to
2232+
/// check errors, call [fs::metadata].
2233+
///
2234+
/// [fs::metadata]: ../../std/fs/fn.metadata.html
22252235
#[stable(feature = "path_ext", since = "1.5.0")]
22262236
pub fn exists(&self) -> bool {
22272237
fs::metadata(self).is_ok()
@@ -2232,13 +2242,25 @@ impl Path {
22322242
/// This function will traverse symbolic links to query information about the
22332243
/// destination file. In case of broken symbolic links this will return `false`.
22342244
///
2245+
/// If you cannot access the directory containing the file, e.g. because of a
2246+
/// permission error, this will return `false`.
2247+
///
22352248
/// # Examples
22362249
///
22372250
/// ```no_run
22382251
/// use std::path::Path;
22392252
/// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
22402253
/// assert_eq!(Path::new("a_file.txt").is_file(), true);
22412254
/// ```
2255+
///
2256+
/// # See Also
2257+
///
2258+
/// This is a convenience function that coerces errors to false. If you want to
2259+
/// check errors, call [fs::metadata] and handle its Result. Then call
2260+
/// [fs::Metadata::is_file] if it was Ok.
2261+
///
2262+
/// [fs::metadata]: ../../std/fs/fn.metadata.html
2263+
/// [fs::Metadata::is_file]: ../../std/fs/struct.Metadata.html#method.is_file
22422264
#[stable(feature = "path_ext", since = "1.5.0")]
22432265
pub fn is_file(&self) -> bool {
22442266
fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
@@ -2249,13 +2271,25 @@ impl Path {
22492271
/// This function will traverse symbolic links to query information about the
22502272
/// destination file. In case of broken symbolic links this will return `false`.
22512273
///
2274+
/// If you cannot access the directory containing the file, e.g. because of a
2275+
/// permission error, this will return `false`.
2276+
///
22522277
/// # Examples
22532278
///
22542279
/// ```no_run
22552280
/// use std::path::Path;
22562281
/// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
22572282
/// assert_eq!(Path::new("a_file.txt").is_dir(), false);
22582283
/// ```
2284+
///
2285+
/// # See Also
2286+
///
2287+
/// This is a convenience function that coerces errors to false. If you want to
2288+
/// check errors, call [fs::metadata] and handle its Result. Then call
2289+
/// [fs::Metadata::is_dir] if it was Ok.
2290+
///
2291+
/// [fs::metadata]: ../../std/fs/fn.metadata.html
2292+
/// [fs::Metadata::is_dir]: ../../std/fs/struct.Metadata.html#method.is_dir
22592293
#[stable(feature = "path_ext", since = "1.5.0")]
22602294
pub fn is_dir(&self) -> bool {
22612295
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)

0 commit comments

Comments
 (0)