Sha256: c666196690c4d45991374b03b27f0838b33e98d0be2838638d4de4ff856894b7

Contents?: true

Size: 1.05 KB

Versions: 28

Compression:

Stored size: 1.05 KB

Contents

use crate::fs::asyncify;

use std::io;
use std::path::Path;

/// 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)`.
///
/// This is the async equivalent of [`std::path::Path::try_exists`][std].
///
/// [std]: fn@std::path::Path::try_exists
///
/// # Examples
///
/// ```no_run
/// use tokio::fs;
///
/// # async fn dox() -> std::io::Result<()> {
/// fs::try_exists("foo.txt").await?;
/// # Ok(())
/// # }
/// ```
pub async fn try_exists(path: impl AsRef<Path>) -> io::Result<bool> {
    let path = path.as_ref().to_owned();
    // std's Path::try_exists is not available for current Rust min supported version.
    // Current implementation is based on its internal implementation instead.
    match asyncify(move || std::fs::metadata(path)).await {
        Ok(_) => Ok(true),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(error),
    }
}

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
wasmtime-25.0.1 ./ext/cargo-vendor/tokio-1.39.3/src/fs/try_exists.rs
wasmtime-25.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/fs/try_exists.rs
wasmtime-24.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/fs/try_exists.rs
wasmtime-23.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/fs/try_exists.rs
wasmtime-22.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/fs/try_exists.rs
wasmtime-21.0.1 ./ext/cargo-vendor/tokio-1.36.0/src/fs/try_exists.rs
wasmtime-20.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/fs/try_exists.rs
wasmtime-20.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/fs/try_exists.rs
wasmtime-18.0.3 ./ext/cargo-vendor/tokio-1.36.0/src/fs/try_exists.rs
wasmtime-17.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/fs/try_exists.rs
wasmtime-17.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/fs/try_exists.rs
wasmtime-16.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/fs/try_exists.rs
wasmtime-15.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/fs/try_exists.rs
wasmtime-15.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/fs/try_exists.rs
wasmtime-14.0.4 ./ext/cargo-vendor/tokio-1.33.0/src/fs/try_exists.rs
wasmtime-14.0.3 ./ext/cargo-vendor/tokio-1.33.0/src/fs/try_exists.rs
wasmtime-14.0.1 ./ext/cargo-vendor/tokio-1.33.0/src/fs/try_exists.rs
wasmtime-14.0.0 ./ext/cargo-vendor/tokio-1.33.0/src/fs/try_exists.rs
wasmtime-13.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/fs/try_exists.rs
wasmtime-12.0.1 ./ext/cargo-vendor/tokio-1.32.0/src/fs/try_exists.rs