Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions cap-async-std/src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,21 @@ impl Dir {
self.metadata(path).await.is_ok()
}

/// Returns `true` if the path points at an existing entity.
///
/// This is an asynchronous version of [`std::fs::try_exists`], and also only
/// accesses paths relative to `self`.
///
/// NOTE: This API is not yet part of `async_std`.
#[inline]
pub async fn try_exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
match self.metadata(path.as_ref()).await {
Ok(_) => Ok(true),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
}

/// Returns `true` if the path exists on disk and is pointing at a regular
/// file.
///
Expand Down
9 changes: 9 additions & 0 deletions cap-async-std/src/fs_utf8/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,15 @@ impl Dir {
}
}

/// Returns `true` if the path points at an existing entity.
///
/// This corresponds to [`async_std::path::Path::exists`], but only
/// accesses paths relative to `self`.
#[inline]
pub async fn try_exists<P: AsRef<Utf8Path>>(&self, path: P) -> io::Result<bool> {
self.cap_std.try_exists(from_utf8(path)?).await
}

/// Returns `true` if the path exists on disk and is pointing at a regular
/// file.
///
Expand Down
18 changes: 18 additions & 0 deletions cap-std/src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,24 @@ impl Dir {
self.metadata(path).is_ok()
}

/// Returns `true` if the path points at an existing entity.
///
/// This corresponds to [`std::fs::try_exists`], but only
/// accesses paths relative to `self`.
///
/// # API correspondence with `std`
///
/// This API is not yet stable in `std`, but is likely to be. For more
/// information, see the [tracker issue](https://github.com/rust-lang/rust/issues/83186).
#[inline]
pub fn try_exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
match self.metadata(path) {
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.
///
Expand Down
9 changes: 9 additions & 0 deletions cap-std/src/fs_utf8/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,15 @@ impl Dir {
}
}

/// Returns `true` if the path points at an existing entity.
///
/// This corresponds to [`std::path::Path::exists`], but only
/// accesses paths relative to `self`.
#[inline]
pub fn try_exists<P: AsRef<Utf8Path>>(&self, path: P) -> io::Result<bool> {
self.cap_std.try_exists(from_utf8(path)?)
}

/// Returns `true` if the path exists on disk and is pointing at a regular
/// file.
///
Expand Down
15 changes: 15 additions & 0 deletions tests/fs_additional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ fn optionally_recursive_mkdir() {
assert!(tmpdir.is_dir(dir));
}

#[test]
fn try_exists() {
let tmpdir = tmpdir();
assert_eq!(tmpdir.try_exists("somefile").unwrap(), false);
let dir = Path::new("d1/d2");
let parent = dir.parent().unwrap();
assert_eq!(tmpdir.try_exists(parent).unwrap(), false);
assert_eq!(tmpdir.try_exists(dir).unwrap(), false);
check!(tmpdir.create_dir(parent));
assert_eq!(tmpdir.try_exists(parent).unwrap(), true);
assert_eq!(tmpdir.try_exists(dir).unwrap(), false);
check!(tmpdir.create_dir(dir));
assert_eq!(tmpdir.try_exists(dir).unwrap(), true);
}

#[test]
fn optionally_nonrecursive_mkdir() {
let tmpdir = tmpdir();
Expand Down
2 changes: 2 additions & 0 deletions tests/fs_utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,10 @@ fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
let file = "fileinfo_check_exists_b_and_a.txt";
check!(check!(tmpdir.create(file)).write(b"foo"));
assert!(tmpdir.exists(file));
assert!(tmpdir.try_exists(file).unwrap());
check!(tmpdir.remove_file(file));
assert!(!tmpdir.exists(file));
assert!(!tmpdir.try_exists(file).unwrap());
}

#[test]
Expand Down