@@ -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
@@ -1885,8 +1887,8 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
1885
1887
/// ```
1886
1888
#[ doc( alias = "rm" , alias = "unlink" , alias = "DeleteFile" ) ]
1887
1889
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1888
- pub fn remove_file < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
1889
- fs_imp:: unlink ( path. as_ref ( ) )
1890
+ pub fn remove_file < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
1891
+ fs_imp:: remove_file ( path)
1890
1892
}
1891
1893
1892
1894
/// Given a path, query the file system to get information about a file,
@@ -1924,8 +1926,8 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
1924
1926
/// ```
1925
1927
#[ doc( alias = "stat" ) ]
1926
1928
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1927
- pub fn metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1928
- fs_imp:: stat ( path. as_ref ( ) ) . map ( Metadata )
1929
+ pub fn metadata < P : AsPath > ( path : P ) -> io:: Result < Metadata > {
1930
+ fs_imp:: metadata ( path) . map ( Metadata )
1929
1931
}
1930
1932
1931
1933
/// Query the metadata about a file without following symlinks.
@@ -1959,8 +1961,8 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1959
1961
/// ```
1960
1962
#[ doc( alias = "lstat" ) ]
1961
1963
#[ stable( feature = "symlink_metadata" , since = "1.1.0" ) ]
1962
- pub fn symlink_metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1963
- fs_imp:: lstat ( path. as_ref ( ) ) . map ( Metadata )
1964
+ pub fn symlink_metadata < P : AsPath > ( path : P ) -> io:: Result < Metadata > {
1965
+ fs_imp:: symlink_metadata ( path) . map ( Metadata )
1964
1966
}
1965
1967
1966
1968
/// Rename a file or directory to a new name, replacing the original file if
@@ -2003,8 +2005,8 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
2003
2005
/// ```
2004
2006
#[ doc( alias = "mv" , alias = "MoveFile" , alias = "MoveFileEx" ) ]
2005
2007
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2006
- pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < ( ) > {
2007
- fs_imp:: rename ( from. as_ref ( ) , to. as_ref ( ) )
2008
+ pub fn rename < P : AsPath , Q : AsPath > ( from : P , to : Q ) -> io:: Result < ( ) > {
2009
+ fs_imp:: rename ( from, to)
2008
2010
}
2009
2011
2010
2012
/// Copies the contents of one file to another. This function will also
@@ -2064,8 +2066,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
2064
2066
#[ doc( alias = "CopyFile" , alias = "CopyFileEx" ) ]
2065
2067
#[ doc( alias = "fclonefileat" , alias = "fcopyfile" ) ]
2066
2068
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2067
- pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < u64 > {
2068
- fs_imp:: copy ( from. as_ref ( ) , to. as_ref ( ) )
2069
+ pub fn copy < P : AsPath , Q : AsPath > ( from : P , to : Q ) -> io:: Result < u64 > {
2070
+ fs_imp:: copy ( from, to)
2069
2071
}
2070
2072
2071
2073
/// Creates a new hard link on the filesystem.
@@ -2109,8 +2111,8 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
2109
2111
/// ```
2110
2112
#[ doc( alias = "CreateHardLink" , alias = "linkat" ) ]
2111
2113
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2112
- pub fn hard_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
2113
- fs_imp:: link ( original. as_ref ( ) , link. as_ref ( ) )
2114
+ pub fn hard_link < P : AsPath , Q : AsPath > ( original : P , link : Q ) -> io:: Result < ( ) > {
2115
+ fs_imp:: hard_link ( original, link)
2114
2116
}
2115
2117
2116
2118
/// Creates a new symbolic link on the filesystem.
@@ -2141,8 +2143,8 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2141
2143
note = "replaced with std::os::unix::fs::symlink and \
2142
2144
std::os::windows::fs::{symlink_file, symlink_dir}"
2143
2145
) ]
2144
- pub fn soft_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
2145
- fs_imp:: symlink ( original. as_ref ( ) , link. as_ref ( ) )
2146
+ pub fn soft_link < P : AsPath , Q : AsPath > ( original : P , link : Q ) -> io:: Result < ( ) > {
2147
+ fs_imp:: soft_link ( original, link)
2146
2148
}
2147
2149
2148
2150
/// Reads a symbolic link, returning the file that the link points to.
@@ -2175,8 +2177,8 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2175
2177
/// }
2176
2178
/// ```
2177
2179
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2178
- pub fn read_link < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2179
- fs_imp:: readlink ( path. as_ref ( ) )
2180
+ pub fn read_link < P : AsPath > ( path : P ) -> io:: Result < PathBuf > {
2181
+ fs_imp:: read_link ( path)
2180
2182
}
2181
2183
2182
2184
/// Returns the canonical, absolute form of a path with all intermediate
@@ -2218,8 +2220,8 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2218
2220
#[ doc( alias = "realpath" ) ]
2219
2221
#[ doc( alias = "GetFinalPathNameByHandle" ) ]
2220
2222
#[ stable( feature = "fs_canonicalize" , since = "1.5.0" ) ]
2221
- pub fn canonicalize < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2222
- fs_imp:: canonicalize ( path. as_ref ( ) )
2223
+ pub fn canonicalize < P : AsPath > ( path : P ) -> io:: Result < PathBuf > {
2224
+ fs_imp:: canonicalize ( path)
2223
2225
}
2224
2226
2225
2227
/// Creates a new, empty directory at the provided path
@@ -2260,8 +2262,8 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2260
2262
#[ doc( alias = "mkdir" , alias = "CreateDirectory" ) ]
2261
2263
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2262
2264
#[ cfg_attr( not( test) , rustc_diagnostic_item = "fs_create_dir" ) ]
2263
- pub fn create_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2264
- DirBuilder :: new ( ) . create ( path. as_ref ( ) )
2265
+ pub fn create_dir < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2266
+ DirBuilder :: new ( ) . create ( path)
2265
2267
}
2266
2268
2267
2269
/// Recursively create a directory and all of its parent components if they
@@ -2304,8 +2306,8 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2304
2306
/// }
2305
2307
/// ```
2306
2308
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2307
- pub fn create_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2308
- DirBuilder :: new ( ) . recursive ( true ) . create ( path. as_ref ( ) )
2309
+ pub fn create_dir_all < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2310
+ DirBuilder :: new ( ) . recursive ( true ) . create ( path)
2309
2311
}
2310
2312
2311
2313
/// Removes an empty directory.
@@ -2340,8 +2342,8 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2340
2342
/// ```
2341
2343
#[ doc( alias = "rmdir" , alias = "RemoveDirectory" ) ]
2342
2344
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2343
- pub fn remove_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2344
- fs_imp:: rmdir ( path. as_ref ( ) )
2345
+ pub fn remove_dir < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2346
+ fs_imp:: remove_dir ( path)
2345
2347
}
2346
2348
2347
2349
/// Removes a directory at this path, after removing all its contents. Use
@@ -2387,8 +2389,8 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2387
2389
/// }
2388
2390
/// ```
2389
2391
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2390
- pub fn remove_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2391
- fs_imp:: remove_dir_all ( path. as_ref ( ) )
2392
+ pub fn remove_dir_all < P : AsPath > ( path : P ) -> io:: Result < ( ) > {
2393
+ fs_imp:: remove_dir_all ( path)
2392
2394
}
2393
2395
2394
2396
/// Returns an iterator over the entries within a directory.
@@ -2463,8 +2465,8 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2463
2465
/// ```
2464
2466
#[ doc( alias = "ls" , alias = "opendir" , alias = "FindFirstFile" , alias = "FindNextFile" ) ]
2465
2467
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2466
- pub fn read_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ReadDir > {
2467
- fs_imp:: readdir ( path. as_ref ( ) ) . map ( ReadDir )
2468
+ pub fn read_dir < P : AsPath > ( path : P ) -> io:: Result < ReadDir > {
2469
+ fs_imp:: read_dir ( path) . map ( ReadDir )
2468
2470
}
2469
2471
2470
2472
/// Changes the permissions found on a file or a directory.
@@ -2499,8 +2501,8 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
2499
2501
/// ```
2500
2502
#[ doc( alias = "chmod" , alias = "SetFileAttributes" ) ]
2501
2503
#[ stable( feature = "set_permissions" , since = "1.1.0" ) ]
2502
- pub fn set_permissions < P : AsRef < Path > > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2503
- fs_imp:: set_perm ( path. as_ref ( ) , perm. 0 )
2504
+ pub fn set_permissions < P : AsPath > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2505
+ fs_imp:: set_permissions ( path, perm. 0 )
2504
2506
}
2505
2507
2506
2508
impl DirBuilder {
@@ -2559,8 +2561,8 @@ impl DirBuilder {
2559
2561
/// assert!(fs::metadata(path).unwrap().is_dir());
2560
2562
/// ```
2561
2563
#[ stable( feature = "dir_builder" , since = "1.6.0" ) ]
2562
- pub fn create < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
2563
- self . _create ( path. as_ref ( ) )
2564
+ pub fn create < P : AsPath > ( & self , path : P ) -> io:: Result < ( ) > {
2565
+ path . with_path ( | path| self . _create ( path ) )
2564
2566
}
2565
2567
2566
2568
fn _create ( & self , path : & Path ) -> io:: Result < ( ) > {
@@ -2631,6 +2633,6 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
2631
2633
// instead.
2632
2634
#[ unstable( feature = "fs_try_exists" , issue = "83186" ) ]
2633
2635
#[ inline]
2634
- pub fn try_exists < P : AsRef < Path > > ( path : P ) -> io:: Result < bool > {
2635
- fs_imp:: try_exists ( path. as_ref ( ) )
2636
+ pub fn try_exists < P : AsPath > ( path : P ) -> io:: Result < bool > {
2637
+ fs_imp:: try_exists ( path)
2636
2638
}
0 commit comments