Debian Codegraph @

third_party/rust/tokio/src/fs/try_exists.rs

raw
1 use crate::fs::asyncify;
2
3 use std::io;
4 use std::path::Path;
5
6 /// Returns `Ok(true)` if the path points at an existing entity.
7 ///
8 /// This function will traverse symbolic links to query information about the
9 /// destination file. In case of broken symbolic links this will return `Ok(false)`.
10 ///
11 /// This is the async equivalent of [`std::path::Path::try_exists`][std].
12 ///
13 /// [std]: fn@std::path::Path::try_exists
14 ///
15 /// # Examples
16 ///
17 /// ```no_run
18 /// use tokio::fs;
19 ///
20 /// # async fn dox() -> std::io::Result<()> {
21 /// fs::try_exists("foo.txt").await?;
22 /// # Ok(())
23 /// # }
24 /// ```
25 pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> {
26 let path = path.as_ref().to_owned();
27 // std's Path::try_exists is not available for current Rust min supported version.
28 // Current implementation is based on its internal implementation instead.
29 match asyncify(move || std::fs::metadata(path)).await {
30 Ok(_) => Ok(true),
31 Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
32 Err(error) => Err(error),
33 }
34 }