Skip to content

Commit d90055b

Browse files
committed
dir: Add try_exists()
I want to use this for the same reason as rust-lang/rust#81822 was added to std. While it's currently unstable, I can't imagine it would change.
1 parent 0271b8a commit d90055b

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

cap-async-std/src/fs/dir.rs

+13
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,19 @@ impl Dir {
754754
self.metadata(path).await.is_ok()
755755
}
756756

757+
/// Returns `true` if the path points at an existing entity.
758+
///
759+
/// This corresponds to [`async_std::path::Path::exists`], but only
760+
/// accesses paths relative to `self`.
761+
#[inline]
762+
pub async fn try_exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
763+
match self.metadata(path.as_ref()).await {
764+
Ok(_) => Ok(true),
765+
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
766+
Err(e) => Err(e),
767+
}
768+
}
769+
757770
/// Returns `true` if the path exists on disk and is pointing at a regular
758771
/// file.
759772
///

cap-async-std/src/fs_utf8/dir.rs

+9
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,15 @@ impl Dir {
546546
}
547547
}
548548

549+
/// Returns `true` if the path points at an existing entity.
550+
///
551+
/// This corresponds to [`async_std::path::Path::exists`], but only
552+
/// accesses paths relative to `self`.
553+
#[inline]
554+
pub async fn try_exists<P: AsRef<Utf8Path>>(&self, path: P) -> io::Result<bool> {
555+
self.cap_std.try_exists(from_utf8(path)?).await
556+
}
557+
549558
/// Returns `true` if the path exists on disk and is pointing at a regular
550559
/// file.
551560
///

cap-std/src/fs/dir.rs

+13
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,19 @@ impl Dir {
584584
self.metadata(path).is_ok()
585585
}
586586

587+
/// Returns `true` if the path points at an existing entity.
588+
///
589+
/// This corresponds to [`std::path::Path::try_exists`], but only
590+
/// accesses paths relative to `self`.
591+
#[inline]
592+
pub fn try_exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
593+
match self.metadata(path) {
594+
Ok(_) => Ok(true),
595+
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
596+
Err(error) => Err(error),
597+
}
598+
}
599+
587600
/// Returns `true` if the path exists on disk and is pointing at a regular
588601
/// file.
589602
///

cap-std/src/fs_utf8/dir.rs

+9
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,15 @@ impl Dir {
533533
}
534534
}
535535

536+
/// Returns `true` if the path points at an existing entity.
537+
///
538+
/// This corresponds to [`std::path::Path::exists`], but only
539+
/// accesses paths relative to `self`.
540+
#[inline]
541+
pub fn try_exists<P: AsRef<Utf8Path>>(&self, path: P) -> io::Result<bool> {
542+
self.cap_std.try_exists(from_utf8(path)?)
543+
}
544+
536545
/// Returns `true` if the path exists on disk and is pointing at a regular
537546
/// file.
538547
///

tests/dir-ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn remove_file() {
1111
let file = tempdir.create("file").expect("create file to delete");
1212
drop(file);
1313
tempdir.remove_file_or_symlink("file").expect("delete file");
14-
assert!(!tempdir.exists("file"), "deletion worked");
14+
assert!(!tempdir.try_exists("file").unwrap(), "deletion worked");
1515
}
1616

1717
#[test]

tests/fs_utf8.rs

+1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
458458
let file = "fileinfo_check_exists_b_and_a.txt";
459459
check!(check!(tmpdir.create(file)).write(b"foo"));
460460
assert!(tmpdir.exists(file));
461+
assert!(tmpdir.try_exists(file).unwrap());
461462
check!(tmpdir.remove_file(file));
462463
assert!(!tmpdir.exists(file));
463464
}

0 commit comments

Comments
 (0)