about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-03-16 16:37:43 +0000
committerbors <bors@rust-lang.org>2021-03-16 16:37:43 +0000
commit1d6754d6eb1ac349477760a2afcd51cf75ac1a1c (patch)
treeb958b90861f439b1a0009d2cf852fa24afff63f8 /library/std/src
parentf24ce9b0140d9be5a336954e878d0c1522966bb8 (diff)
parentec074276ab1272bb42b66407529431dfecf639a0 (diff)
downloadrust-1d6754d6eb1ac349477760a2afcd51cf75ac1a1c.tar.gz
rust-1d6754d6eb1ac349477760a2afcd51cf75ac1a1c.zip
Auto merge of #83199 - JohnTitor:rollup-zrfk94a, r=JohnTitor
Rollup of 10 pull requests

Successful merges:

 - #81822 (Added `try_exists()` method to `std::path::Path`)
 - #83072 (Update `Vec` docs)
 - #83077 (rustdoc: reduce GC work during search)
 - #83091 (Constify `copy` related functions)
 - #83156 (Fall-back to sans-serif if Arial is not available)
 - #83157 (No background for code in portability snippets)
 - #83160 (Deprecate RustcEncodable and RustcDecodable.)
 - #83162 (Specify *.woff2 files as binary)
 - #83172 (More informative diagnotic from `x.py test` attempt atop beta checkout)
 - #83196 (Use delay_span_bug instead of panic in layout_scalar_valid_range)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/path.rs30
-rw-r--r--library/std/src/prelude/v1.rs2
2 files changed, 31 insertions, 1 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index de3b57df44e..57c892f32b1 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -2472,6 +2472,36 @@ impl Path {
         fs::metadata(self).is_ok()
     }
 
+    /// Returns `Ok(true)` if the path points at an existing entity.
+    ///
+    /// This function will traverse symbolic links to query information about the
+    /// destination file. In case of broken symbolic links this will return `Ok(false)`.
+    ///
+    /// As opposed to the `exists()` method, this one doesn't silently ignore errors
+    /// unrelated to the path not existing. (E.g. it will return `Err(_)` in case of permission
+    /// denied on some of the parent directories.)
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(path_try_exists)]
+    ///
+    /// use std::path::Path;
+    /// assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
+    /// assert!(Path::new("/root/secret_file.txt").try_exists().is_err());
+    /// ```
+    // FIXME: stabilization should modify documentation of `exists()` to recommend this method
+    // instead.
+    #[unstable(feature = "path_try_exists", issue = "83186")]
+    #[inline]
+    pub fn try_exists(&self) -> io::Result<bool> {
+        match fs::metadata(self) {
+            Ok(_) => Ok(true),
+            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
+            Err(error) => Err(error),
+        }
+    }
+
     /// Returns `true` if the path exists on disk and is pointing at a regular file.
     ///
     /// This function will traverse symbolic links to query information about the
diff --git a/library/std/src/prelude/v1.rs b/library/std/src/prelude/v1.rs
index ec89bb6d2a4..c5b871edbf2 100644
--- a/library/std/src/prelude/v1.rs
+++ b/library/std/src/prelude/v1.rs
@@ -48,7 +48,7 @@ pub use core::prelude::v1::{
 // FIXME: Attribute and internal derive macros are not documented because for them rustdoc generates
 // dead links which fail link checker testing.
 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
-#[allow(deprecated)]
+#[allow(deprecated, deprecated_in_future)]
 #[doc(hidden)]
 pub use core::prelude::v1::{
     bench, global_allocator, test, test_case, RustcDecodable, RustcEncodable,