File tree 6 files changed +68
-0
lines changed
6 files changed +68
-0
lines changed Original file line number Diff line number Diff line change @@ -754,6 +754,21 @@ 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 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
+
757
772
/// Returns `true` if the path exists on disk and is pointing at a regular
758
773
/// file.
759
774
///
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,24 @@ 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::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
+
587
605
/// Returns `true` if the path exists on disk and is pointing at a regular
588
606
/// file.
589
607
///
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 @@ -191,6 +191,21 @@ 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
+ 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
+
194
209
#[ test]
195
210
fn optionally_nonrecursive_mkdir ( ) {
196
211
let tmpdir = tmpdir ( ) ;
Original file line number Diff line number Diff line change @@ -458,8 +458,10 @@ 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) ) ;
464
+ assert ! ( !tmpdir. try_exists( file) . unwrap( ) ) ;
463
465
}
464
466
465
467
#[ test]
You can’t perform that action at this time.
0 commit comments