@@ -14,10 +14,11 @@ mod tests;
14
14
use crate :: ffi:: OsString ;
15
15
use crate :: fmt;
16
16
use crate :: io:: { self , BorrowedCursor , IoSlice , IoSliceMut , Read , Seek , SeekFrom , Write } ;
17
- use crate :: path:: { Path , PathBuf } ;
17
+ use crate :: path:: { AsPath , Path , PathBuf } ;
18
18
use crate :: sealed:: Sealed ;
19
19
use crate :: sync:: Arc ;
20
- use crate :: sys:: fs as fs_imp;
20
+ use crate :: sys;
21
+ use crate :: sys:: fs:: fs_imp;
21
22
use crate :: sys_common:: { AsInner , AsInnerMut , FromInner , IntoInner } ;
22
23
use crate :: time:: SystemTime ;
23
24
@@ -256,16 +257,16 @@ pub struct DirBuilder {
256
257
/// }
257
258
/// ```
258
259
#[ stable( feature = "fs_read_write_bytes" , since = "1.26.0" ) ]
259
- pub fn read < P : AsRef < Path > > ( path : P ) -> io:: Result < Vec < u8 > > {
260
- fn inner ( path : & Path ) -> io:: Result < Vec < u8 > > {
261
- let mut file = File :: open ( path) ?;
260
+ pub fn read < P : AsPath > ( path : P ) -> io:: Result < Vec < u8 > > {
261
+ fn inner ( mut file : File ) -> io:: Result < Vec < u8 > > {
262
262
let size = file. metadata ( ) . map ( |m| m. len ( ) as usize ) . ok ( ) ;
263
263
let mut bytes = Vec :: new ( ) ;
264
264
bytes. try_reserve_exact ( size. unwrap_or ( 0 ) ) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
265
265
io:: default_read_to_end ( & mut file, & mut bytes, size) ?;
266
266
Ok ( bytes)
267
267
}
268
- inner ( path. as_ref ( ) )
268
+ let file = File :: open ( path) ?;
269
+ inner ( file)
269
270
}
270
271
271
272
/// Read the entire contents of a file into a string.
@@ -299,16 +300,16 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
299
300
/// }
300
301
/// ```
301
302
#[ stable( feature = "fs_read_write" , since = "1.26.0" ) ]
302
- pub fn read_to_string < P : AsRef < Path > > ( path : P ) -> io:: Result < String > {
303
- fn inner ( path : & Path ) -> io:: Result < String > {
304
- let mut file = File :: open ( path) ?;
303
+ pub fn read_to_string < P : AsPath > ( path : P ) -> io:: Result < String > {
304
+ fn inner ( mut file : File ) -> io:: Result < String > {
305
305
let size = file. metadata ( ) . map ( |m| m. len ( ) as usize ) . ok ( ) ;
306
306
let mut string = String :: new ( ) ;
307
307
string. try_reserve_exact ( size. unwrap_or ( 0 ) ) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
308
308
io:: default_read_to_string ( & mut file, & mut string, size) ?;
309
309
Ok ( string)
310
310
}
311
- inner ( path. as_ref ( ) )
311
+ let file = File :: open ( path) ?;
312
+ inner ( file)
312
313
}
313
314
314
315
/// Write a slice as the entire contents of a file.
@@ -336,11 +337,12 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
336
337
/// }
337
338
/// ```
338
339
#[ stable( feature = "fs_read_write_bytes" , since = "1.26.0" ) ]
339
- pub fn write < P : AsRef < Path > , C : AsRef < [ u8 ] > > ( path : P , contents : C ) -> io:: Result < ( ) > {
340
- fn inner ( path : & Path , contents : & [ u8 ] ) -> io:: Result < ( ) > {
341
- File :: create ( path ) ? . write_all ( contents)
340
+ pub fn write < P : AsPath , C : AsRef < [ u8 ] > > ( path : P , contents : C ) -> io:: Result < ( ) > {
341
+ fn inner ( mut file : File , contents : & [ u8 ] ) -> io:: Result < ( ) > {
342
+ file . write_all ( contents)
342
343
}
343
- inner ( path. as_ref ( ) , contents. as_ref ( ) )
344
+ let file = File :: create ( path) ?;
345
+ inner ( file, contents. as_ref ( ) )
344
346
}
345
347
346
348
impl File {
@@ -371,8 +373,8 @@ impl File {
371
373
/// }
372
374
/// ```
373
375
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
374
- pub fn open < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
375
- OpenOptions :: new ( ) . read ( true ) . open ( path. as_ref ( ) )
376
+ pub fn open < P : AsPath > ( path : P ) -> io:: Result < File > {
377
+ OpenOptions :: new ( ) . read ( true ) . open ( path)
376
378
}
377
379
378
380
/// Opens a file in write-only mode.
@@ -400,8 +402,8 @@ impl File {
400
402
/// }
401
403
/// ```
402
404
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
403
- pub fn create < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
404
- OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
405
+ pub fn create < P : AsPath > ( path : P ) -> io:: Result < File > {
406
+ OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path)
405
407
}
406
408
407
409
/// Creates a new file in read-write mode; error if the file exists.
@@ -429,8 +431,8 @@ impl File {
429
431
/// }
430
432
/// ```
431
433
#[ stable( feature = "file_create_new" , since = "1.77.0" ) ]
432
- pub fn create_new < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
433
- OpenOptions :: new ( ) . read ( true ) . write ( true ) . create_new ( true ) . open ( path. as_ref ( ) )
434
+ pub fn create_new < P : AsPath > ( path : P ) -> io:: Result < File > {
435
+ OpenOptions :: new ( ) . read ( true ) . write ( true ) . create_new ( true ) . open ( path)
434
436
}
435
437
436
438
/// Returns a new OpenOptions object.
@@ -1127,12 +1129,12 @@ impl OpenOptions {
1127
1129
/// [`NotFound`]: io::ErrorKind::NotFound
1128
1130
/// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
1129
1131
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1130
- pub fn open < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < File > {
1131
- self . _open ( path. as_ref ( ) )
1132
+ pub fn open < P : AsPath > ( & self , path : P ) -> io:: Result < File > {
1133
+ path . with_native_path ( | path| self . _open ( path ) )
1132
1134
}
1133
1135
1134
- fn _open ( & self , path : & Path ) -> io:: Result < File > {
1135
- fs_imp:: File :: open ( path, & self . 0 ) . map ( |inner| File { inner } )
1136
+ fn _open ( & self , path : & sys :: path :: NativePath ) -> io:: Result < File > {
1137
+ fs_imp:: File :: open_native ( path, & self . 0 ) . map ( |inner| File { inner } )
1136
1138
}
1137
1139
}
1138
1140
@@ -1884,8 +1886,8 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
1884
1886
/// ```
1885
1887
#[ doc( alias = "rm" , alias = "unlink" , alias = "DeleteFile" ) ]
1886
1888
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1887
- pub fn remove_file < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
1888
- fs_imp:: unlink ( path. as_ref ( ) )
1889
+ pub fn remove_file < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
1890
+ fs_imp:: remove_file ( path)
1889
1891
}
1890
1892
1891
1893
/// Given a path, query the file system to get information about a file,
@@ -1923,8 +1925,8 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
1923
1925
/// ```
1924
1926
#[ doc( alias = "stat" ) ]
1925
1927
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1926
- pub fn metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1927
- fs_imp:: stat ( path. as_ref ( ) ) . map ( Metadata )
1928
+ pub fn metadata < P : AsPath > ( path : P ) -> io:: Result < Metadata > {
1929
+ fs_imp:: metadata ( path) . map ( Metadata )
1928
1930
}
1929
1931
1930
1932
/// Query the metadata about a file without following symlinks.
@@ -1958,8 +1960,8 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1958
1960
/// ```
1959
1961
#[ doc( alias = "lstat" ) ]
1960
1962
#[ stable( feature = "symlink_metadata" , since = "1.1.0" ) ]
1961
- pub fn symlink_metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1962
- fs_imp:: lstat ( path. as_ref ( ) ) . map ( Metadata )
1963
+ pub fn symlink_metadata < P : AsPath > ( path : P ) -> io:: Result < Metadata > {
1964
+ fs_imp:: symlink_metadata ( path) . map ( Metadata )
1963
1965
}
1964
1966
1965
1967
/// Rename a file or directory to a new name, replacing the original file if
@@ -2002,8 +2004,8 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
2002
2004
/// ```
2003
2005
#[ doc( alias = "mv" , alias = "MoveFile" , alias = "MoveFileEx" ) ]
2004
2006
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2005
- pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < ( ) > {
2006
- fs_imp:: rename ( from. as_ref ( ) , to. as_ref ( ) )
2007
+ pub fn rename < P : AsPath , Q : AsPath > ( from : P , to : Q ) -> io:: Result < ( ) > {
2008
+ fs_imp:: rename ( from, to)
2007
2009
}
2008
2010
2009
2011
/// Copies the contents of one file to another. This function will also
@@ -2063,8 +2065,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
2063
2065
#[ doc( alias = "CopyFile" , alias = "CopyFileEx" ) ]
2064
2066
#[ doc( alias = "fclonefileat" , alias = "fcopyfile" ) ]
2065
2067
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2066
- pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < u64 > {
2067
- fs_imp:: copy ( from. as_ref ( ) , to. as_ref ( ) )
2068
+ pub fn copy < P : AsPath , Q : AsPath > ( from : P , to : Q ) -> io:: Result < u64 > {
2069
+ fs_imp:: copy ( from, to)
2068
2070
}
2069
2071
2070
2072
/// Creates a new hard link on the filesystem.
@@ -2108,8 +2110,8 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
2108
2110
/// ```
2109
2111
#[ doc( alias = "CreateHardLink" , alias = "linkat" ) ]
2110
2112
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2111
- pub fn hard_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
2112
- fs_imp:: link ( original. as_ref ( ) , link. as_ref ( ) )
2113
+ pub fn hard_link < P : AsPath , Q : AsPath > ( original : P , link : Q ) -> io:: Result < ( ) > {
2114
+ fs_imp:: hard_link ( original, link)
2113
2115
}
2114
2116
2115
2117
/// Creates a new symbolic link on the filesystem.
@@ -2140,8 +2142,8 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2140
2142
note = "replaced with std::os::unix::fs::symlink and \
2141
2143
std::os::windows::fs::{symlink_file, symlink_dir}"
2142
2144
) ]
2143
- pub fn soft_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
2144
- fs_imp:: symlink ( original. as_ref ( ) , link. as_ref ( ) )
2145
+ pub fn soft_link < P : AsPath , Q : AsPath > ( original : P , link : Q ) -> io:: Result < ( ) > {
2146
+ fs_imp:: soft_link ( original, link)
2145
2147
}
2146
2148
2147
2149
/// Reads a symbolic link, returning the file that the link points to.
@@ -2174,8 +2176,8 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2174
2176
/// }
2175
2177
/// ```
2176
2178
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2177
- pub fn read_link < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2178
- fs_imp:: readlink ( path. as_ref ( ) )
2179
+ pub fn read_link < P : AsPath > ( path : P ) -> io:: Result < PathBuf > {
2180
+ fs_imp:: read_link ( path)
2179
2181
}
2180
2182
2181
2183
/// Returns the canonical, absolute form of a path with all intermediate
@@ -2217,8 +2219,8 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2217
2219
#[ doc( alias = "realpath" ) ]
2218
2220
#[ doc( alias = "GetFinalPathNameByHandle" ) ]
2219
2221
#[ stable( feature = "fs_canonicalize" , since = "1.5.0" ) ]
2220
- pub fn canonicalize < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2221
- fs_imp:: canonicalize ( path. as_ref ( ) )
2222
+ pub fn canonicalize < P : AsPath > ( path : P ) -> io:: Result < PathBuf > {
2223
+ fs_imp:: canonicalize ( path)
2222
2224
}
2223
2225
2224
2226
/// Creates a new, empty directory at the provided path
@@ -2259,8 +2261,8 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2259
2261
#[ doc( alias = "mkdir" , alias = "CreateDirectory" ) ]
2260
2262
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2261
2263
#[ cfg_attr( not( test) , rustc_diagnostic_item = "fs_create_dir" ) ]
2262
- pub fn create_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2263
- DirBuilder :: new ( ) . create ( path. as_ref ( ) )
2264
+ pub fn create_dir < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2265
+ DirBuilder :: new ( ) . create ( path)
2264
2266
}
2265
2267
2266
2268
/// Recursively create a directory and all of its parent components if they
@@ -2303,8 +2305,8 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2303
2305
/// }
2304
2306
/// ```
2305
2307
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2306
- pub fn create_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2307
- DirBuilder :: new ( ) . recursive ( true ) . create ( path. as_ref ( ) )
2308
+ pub fn create_dir_all < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2309
+ DirBuilder :: new ( ) . recursive ( true ) . create ( path)
2308
2310
}
2309
2311
2310
2312
/// Removes an empty directory.
@@ -2339,8 +2341,8 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2339
2341
/// ```
2340
2342
#[ doc( alias = "rmdir" , alias = "RemoveDirectory" ) ]
2341
2343
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2342
- pub fn remove_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2343
- fs_imp:: rmdir ( path. as_ref ( ) )
2344
+ pub fn remove_dir < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2345
+ fs_imp:: remove_dir ( path)
2344
2346
}
2345
2347
2346
2348
/// Removes a directory at this path, after removing all its contents. Use
@@ -2386,8 +2388,8 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2386
2388
/// }
2387
2389
/// ```
2388
2390
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2389
- pub fn remove_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2390
- fs_imp:: remove_dir_all ( path. as_ref ( ) )
2391
+ pub fn remove_dir_all < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2392
+ fs_imp:: remove_dir_all ( path)
2391
2393
}
2392
2394
2393
2395
/// Returns an iterator over the entries within a directory.
@@ -2462,8 +2464,8 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2462
2464
/// ```
2463
2465
#[ doc( alias = "ls" , alias = "opendir" , alias = "FindFirstFile" , alias = "FindNextFile" ) ]
2464
2466
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2465
- pub fn read_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ReadDir > {
2466
- fs_imp:: readdir ( path. as_ref ( ) ) . map ( ReadDir )
2467
+ pub fn read_dir < P : AsPath > ( path : P ) -> io:: Result < ReadDir > {
2468
+ fs_imp:: read_dir ( path) . map ( ReadDir )
2467
2469
}
2468
2470
2469
2471
/// Changes the permissions found on a file or a directory.
@@ -2498,8 +2500,8 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
2498
2500
/// ```
2499
2501
#[ doc( alias = "chmod" , alias = "SetFileAttributes" ) ]
2500
2502
#[ stable( feature = "set_permissions" , since = "1.1.0" ) ]
2501
- pub fn set_permissions < P : AsRef < Path > > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2502
- fs_imp:: set_perm ( path. as_ref ( ) , perm. 0 )
2503
+ pub fn set_permissions < P : AsPath > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2504
+ fs_imp:: set_permissions ( path, perm. 0 )
2503
2505
}
2504
2506
2505
2507
impl DirBuilder {
@@ -2558,8 +2560,8 @@ impl DirBuilder {
2558
2560
/// assert!(fs::metadata(path).unwrap().is_dir());
2559
2561
/// ```
2560
2562
#[ stable( feature = "dir_builder" , since = "1.6.0" ) ]
2561
- pub fn create < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
2562
- self . _create ( path. as_ref ( ) )
2563
+ pub fn create < P : AsPath > ( & self , path : P ) -> io:: Result < ( ) > {
2564
+ path . with_path ( | path| self . _create ( path ) )
2563
2565
}
2564
2566
2565
2567
fn _create ( & self , path : & Path ) -> io:: Result < ( ) > {
@@ -2630,6 +2632,6 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
2630
2632
// instead.
2631
2633
#[ unstable( feature = "fs_try_exists" , issue = "83186" ) ]
2632
2634
#[ inline]
2633
- pub fn try_exists < P : AsRef < Path > > ( path : P ) -> io:: Result < bool > {
2634
- fs_imp:: try_exists ( path. as_ref ( ) )
2635
+ pub fn try_exists < P : AsPath > ( path : P ) -> io:: Result < bool > {
2636
+ fs_imp:: try_exists ( path)
2635
2637
}
0 commit comments