Skip to content

Commit 16a7dae

Browse files
cgwalterssunfishcode
authored andcommitted
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 cff5979 commit 16a7dae

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,21 @@ 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 is an asynchronous version of [`std::fs::try_exists`], and also only
760+
/// accesses paths relative to `self`.
761+
///
762+
/// NOTE: This API is not yet part of `async_std`.
763+
#[inline]
764+
pub async fn try_exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
765+
match self.metadata(path.as_ref()).await {
766+
Ok(_) => Ok(true),
767+
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
768+
Err(e) => Err(e),
769+
}
770+
}
771+
757772
/// Returns `true` if the path exists on disk and is pointing at a regular
758773
/// file.
759774
///

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

+18
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,24 @@ 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::fs::try_exists`], but only
590+
/// accesses paths relative to `self`.
591+
///
592+
/// # API correspondence with `std`
593+
///
594+
/// This API is not yet stable in `std`, but is likely to be. For more
595+
/// information, see the [tracker issue](https://github.com/rust-lang/rust/issues/83186).
596+
#[inline]
597+
pub fn try_exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
598+
match self.metadata(path) {
599+
Ok(_) => Ok(true),
600+
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
601+
Err(error) => Err(error),
602+
}
603+
}
604+
587605
/// Returns `true` if the path exists on disk and is pointing at a regular
588606
/// file.
589607
///

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/fs_additional.rs

+15
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ fn optionally_recursive_mkdir() {
191191
assert!(tmpdir.is_dir(dir));
192192
}
193193

194+
#[test]
195+
fn try_exists() {
196+
let tmpdir = tmpdir();
197+
assert_eq!(tmpdir.try_exists("somefile").unwrap(), false);
198+
let dir = Path::new("d1/d2");
199+
let parent = dir.parent().unwrap();
200+
assert_eq!(tmpdir.try_exists(parent).unwrap(), false);
201+
assert_eq!(tmpdir.try_exists(dir).unwrap(), false);
202+
check!(tmpdir.create_dir(parent));
203+
assert_eq!(tmpdir.try_exists(parent).unwrap(), true);
204+
assert_eq!(tmpdir.try_exists(dir).unwrap(), false);
205+
check!(tmpdir.create_dir(dir));
206+
assert_eq!(tmpdir.try_exists(dir).unwrap(), true);
207+
}
208+
194209
#[test]
195210
fn optionally_nonrecursive_mkdir() {
196211
let tmpdir = tmpdir();

tests/fs_utf8.rs

+2
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,10 @@ 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));
464+
assert!(!tmpdir.try_exists(file).unwrap());
463465
}
464466

465467
#[test]

0 commit comments

Comments
 (0)