File tree 7 files changed +52
-1
lines changed
7 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -754,6 +754,19 @@ impl Dir {
754
754
self . metadata ( path) . await . is_ok ( )
755
755
}
756
756
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
+
757
770
/// Returns `true` if the path exists on disk and is pointing at a regular
758
771
/// file.
759
772
///
Original file line number Diff line number Diff line change @@ -546,6 +546,15 @@ impl Dir {
546
546
}
547
547
}
548
548
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
+
549
558
/// Returns `true` if the path exists on disk and is pointing at a regular
550
559
/// file.
551
560
///
Original file line number Diff line number Diff line change @@ -584,6 +584,19 @@ impl Dir {
584
584
self . metadata ( path) . is_ok ( )
585
585
}
586
586
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
+
587
600
/// Returns `true` if the path exists on disk and is pointing at a regular
588
601
/// file.
589
602
///
Original file line number Diff line number Diff line change @@ -533,6 +533,15 @@ impl Dir {
533
533
}
534
534
}
535
535
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
+
536
545
/// Returns `true` if the path exists on disk and is pointing at a regular
537
546
/// file.
538
547
///
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ fn remove_file() {
11
11
let file = tempdir. create ( "file" ) . expect ( "create file to delete" ) ;
12
12
drop ( file) ;
13
13
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" ) ;
15
15
}
16
16
17
17
#[ test]
Original file line number Diff line number Diff line change @@ -191,6 +191,12 @@ fn optionally_recursive_mkdir() {
191
191
assert ! ( tmpdir. is_dir( dir) ) ;
192
192
}
193
193
194
+ #[ test]
195
+ fn try_exists ( ) {
196
+ let tmpdir = tmpdir ( ) ;
197
+ assert_eq ! ( tmpdir. try_exists( "somefile" ) . unwrap( ) , false ) ;
198
+ }
199
+
194
200
#[ test]
195
201
fn optionally_nonrecursive_mkdir ( ) {
196
202
let tmpdir = tmpdir ( ) ;
Original file line number Diff line number Diff line change @@ -458,6 +458,7 @@ fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
458
458
let file = "fileinfo_check_exists_b_and_a.txt" ;
459
459
check ! ( check!( tmpdir. create( file) ) . write( b"foo" ) ) ;
460
460
assert ! ( tmpdir. exists( file) ) ;
461
+ assert ! ( tmpdir. try_exists( file) . unwrap( ) ) ;
461
462
check ! ( tmpdir. remove_file( file) ) ;
462
463
assert ! ( !tmpdir. exists( file) ) ;
463
464
}
You can’t perform that action at this time.
0 commit comments