@@ -14,7 +14,7 @@ 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:: { NativePath , Path , PathBuf , PathLike } ;
18
18
use crate :: sys:: fs as fs_imp;
19
19
use crate :: sys_common:: { AsInner , AsInnerMut , FromInner , IntoInner } ;
20
20
use crate :: time:: SystemTime ;
@@ -246,15 +246,15 @@ pub struct DirBuilder {
246
246
/// }
247
247
/// ```
248
248
#[ stable( feature = "fs_read_write_bytes" , since = "1.26.0" ) ]
249
- pub fn read < P : AsRef < Path > > ( path : P ) -> io:: Result < Vec < u8 > > {
249
+ pub fn read < P : PathLike > ( path : P ) -> io:: Result < Vec < u8 > > {
250
250
fn inner ( path : & Path ) -> io:: Result < Vec < u8 > > {
251
251
let mut file = File :: open ( path) ?;
252
252
let size = file. metadata ( ) . map ( |m| m. len ( ) ) . unwrap_or ( 0 ) ;
253
253
let mut bytes = Vec :: with_capacity ( size as usize ) ;
254
254
io:: default_read_to_end ( & mut file, & mut bytes) ?;
255
255
Ok ( bytes)
256
256
}
257
- inner ( path. as_ref ( ) )
257
+ path. with_path ( inner )
258
258
}
259
259
260
260
/// Read the entire contents of a file into a string.
@@ -286,15 +286,15 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
286
286
/// }
287
287
/// ```
288
288
#[ stable( feature = "fs_read_write" , since = "1.26.0" ) ]
289
- pub fn read_to_string < P : AsRef < Path > > ( path : P ) -> io:: Result < String > {
289
+ pub fn read_to_string < P : PathLike > ( path : P ) -> io:: Result < String > {
290
290
fn inner ( path : & Path ) -> io:: Result < String > {
291
291
let mut file = File :: open ( path) ?;
292
292
let size = file. metadata ( ) . map ( |m| m. len ( ) ) . unwrap_or ( 0 ) ;
293
293
let mut string = String :: with_capacity ( size as usize ) ;
294
294
io:: default_read_to_string ( & mut file, & mut string) ?;
295
295
Ok ( string)
296
296
}
297
- inner ( path. as_ref ( ) )
297
+ path. with_path ( inner )
298
298
}
299
299
300
300
/// Write a slice as the entire contents of a file.
@@ -322,11 +322,11 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
322
322
/// }
323
323
/// ```
324
324
#[ stable( feature = "fs_read_write_bytes" , since = "1.26.0" ) ]
325
- pub fn write < P : AsRef < Path > , C : AsRef < [ u8 ] > > ( path : P , contents : C ) -> io:: Result < ( ) > {
325
+ pub fn write < P : PathLike , C : AsRef < [ u8 ] > > ( path : P , contents : C ) -> io:: Result < ( ) > {
326
326
fn inner ( path : & Path , contents : & [ u8 ] ) -> io:: Result < ( ) > {
327
327
File :: create ( path) ?. write_all ( contents)
328
328
}
329
- inner ( path. as_ref ( ) , contents. as_ref ( ) )
329
+ path . with_path ( |path| inner ( path. as_ref ( ) , contents. as_ref ( ) ) )
330
330
}
331
331
332
332
impl File {
@@ -357,8 +357,8 @@ impl File {
357
357
/// }
358
358
/// ```
359
359
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
360
- pub fn open < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
361
- OpenOptions :: new ( ) . read ( true ) . open ( path. as_ref ( ) )
360
+ pub fn open < P : PathLike > ( path : P ) -> io:: Result < File > {
361
+ OpenOptions :: new ( ) . read ( true ) . open ( path)
362
362
}
363
363
364
364
/// Opens a file in write-only mode.
@@ -386,8 +386,8 @@ impl File {
386
386
/// }
387
387
/// ```
388
388
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
389
- pub fn create < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
390
- OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
389
+ pub fn create < P : PathLike > ( path : P ) -> io:: Result < File > {
390
+ OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path)
391
391
}
392
392
393
393
/// Creates a new file in read-write mode; error if the file exists.
@@ -417,8 +417,8 @@ impl File {
417
417
/// }
418
418
/// ```
419
419
#[ unstable( feature = "file_create_new" , issue = "105135" ) ]
420
- pub fn create_new < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
421
- OpenOptions :: new ( ) . read ( true ) . write ( true ) . create_new ( true ) . open ( path. as_ref ( ) )
420
+ pub fn create_new < P : PathLike > ( path : P ) -> io:: Result < File > {
421
+ OpenOptions :: new ( ) . read ( true ) . write ( true ) . create_new ( true ) . open ( path)
422
422
}
423
423
424
424
/// Returns a new OpenOptions object.
@@ -1073,12 +1073,12 @@ impl OpenOptions {
1073
1073
/// [`NotFound`]: io::ErrorKind::NotFound
1074
1074
/// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
1075
1075
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1076
- pub fn open < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < File > {
1077
- self . _open ( path. as_ref ( ) )
1076
+ pub fn open < P : PathLike > ( & self , path : P ) -> io:: Result < File > {
1077
+ path . with_native_path ( | path| self . _open ( path ) )
1078
1078
}
1079
1079
1080
- fn _open ( & self , path : & Path ) -> io:: Result < File > {
1081
- fs_imp:: File :: open ( path, & self . 0 ) . map ( |inner| File { inner } )
1080
+ fn _open ( & self , path : & NativePath ) -> io:: Result < File > {
1081
+ fs_imp:: File :: open_native ( path, & self . 0 ) . map ( |inner| File { inner } )
1082
1082
}
1083
1083
}
1084
1084
@@ -1805,8 +1805,8 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
1805
1805
/// }
1806
1806
/// ```
1807
1807
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1808
- pub fn remove_file < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
1809
- fs_imp:: unlink ( path . as_ref ( ) )
1808
+ pub fn remove_file < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
1809
+ path . with_path ( fs_imp:: unlink)
1810
1810
}
1811
1811
1812
1812
/// Given a path, query the file system to get information about a file,
@@ -1843,8 +1843,8 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
1843
1843
/// }
1844
1844
/// ```
1845
1845
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1846
- pub fn metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1847
- fs_imp:: stat ( path . as_ref ( ) ) . map ( Metadata )
1846
+ pub fn metadata < P : PathLike > ( path : P ) -> io:: Result < Metadata > {
1847
+ path . with_path ( fs_imp:: stat) . map ( Metadata )
1848
1848
}
1849
1849
1850
1850
/// Query the metadata about a file without following symlinks.
@@ -1877,8 +1877,8 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1877
1877
/// }
1878
1878
/// ```
1879
1879
#[ stable( feature = "symlink_metadata" , since = "1.1.0" ) ]
1880
- pub fn symlink_metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1881
- fs_imp:: lstat ( path . as_ref ( ) ) . map ( Metadata )
1880
+ pub fn symlink_metadata < P : PathLike > ( path : P ) -> io:: Result < Metadata > {
1881
+ path . with_path ( fs_imp:: lstat) . map ( Metadata )
1882
1882
}
1883
1883
1884
1884
/// Rename a file or directory to a new name, replacing the original file if
@@ -1920,8 +1920,8 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1920
1920
/// }
1921
1921
/// ```
1922
1922
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1923
- pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < ( ) > {
1924
- fs_imp:: rename ( from. as_ref ( ) , to. as_ref ( ) )
1923
+ pub fn rename < P : PathLike , Q : PathLike > ( from : P , to : Q ) -> io:: Result < ( ) > {
1924
+ from . with_path ( |from| to . with_path ( |to| fs_imp:: rename ( from, to) ) )
1925
1925
}
1926
1926
1927
1927
/// Copies the contents of one file to another. This function will also
@@ -1978,8 +1978,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
1978
1978
/// }
1979
1979
/// ```
1980
1980
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1981
- pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < u64 > {
1982
- fs_imp:: copy ( from. as_ref ( ) , to. as_ref ( ) )
1981
+ pub fn copy < P : PathLike , Q : PathLike > ( from : P , to : Q ) -> io:: Result < u64 > {
1982
+ from . with_path ( |from| to . with_path ( |to| fs_imp:: copy ( from, to) ) )
1983
1983
}
1984
1984
1985
1985
/// Creates a new hard link on the filesystem.
@@ -2022,8 +2022,8 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
2022
2022
/// }
2023
2023
/// ```
2024
2024
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2025
- pub fn hard_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
2026
- fs_imp:: link ( original. as_ref ( ) , link. as_ref ( ) )
2025
+ pub fn hard_link < P : PathLike , Q : PathLike > ( original : P , link : Q ) -> io:: Result < ( ) > {
2026
+ original . with_path ( |original| link . with_path ( |link| fs_imp:: link ( original, link) ) )
2027
2027
}
2028
2028
2029
2029
/// Creates a new symbolic link on the filesystem.
@@ -2054,8 +2054,8 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2054
2054
note = "replaced with std::os::unix::fs::symlink and \
2055
2055
std::os::windows::fs::{symlink_file, symlink_dir}"
2056
2056
) ]
2057
- pub fn soft_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
2058
- fs_imp:: symlink ( original. as_ref ( ) , link. as_ref ( ) )
2057
+ pub fn soft_link < P : PathLike , Q : PathLike > ( original : P , link : Q ) -> io:: Result < ( ) > {
2058
+ original . with_path ( |original| link . with_path ( |link| fs_imp:: symlink ( original, link) ) )
2059
2059
}
2060
2060
2061
2061
/// Reads a symbolic link, returning the file that the link points to.
@@ -2088,8 +2088,8 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2088
2088
/// }
2089
2089
/// ```
2090
2090
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2091
- pub fn read_link < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2092
- fs_imp:: readlink ( path . as_ref ( ) )
2091
+ pub fn read_link < P : PathLike > ( path : P ) -> io:: Result < PathBuf > {
2092
+ path . with_path ( fs_imp:: readlink)
2093
2093
}
2094
2094
2095
2095
/// Returns the canonical, absolute form of a path with all intermediate
@@ -2131,8 +2131,8 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2131
2131
#[ doc( alias = "realpath" ) ]
2132
2132
#[ doc( alias = "GetFinalPathNameByHandle" ) ]
2133
2133
#[ stable( feature = "fs_canonicalize" , since = "1.5.0" ) ]
2134
- pub fn canonicalize < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2135
- fs_imp:: canonicalize ( path . as_ref ( ) )
2134
+ pub fn canonicalize < P : PathLike > ( path : P ) -> io:: Result < PathBuf > {
2135
+ path . with_path ( fs_imp:: canonicalize)
2136
2136
}
2137
2137
2138
2138
/// Creates a new, empty directory at the provided path
@@ -2172,8 +2172,8 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2172
2172
/// ```
2173
2173
#[ doc( alias = "mkdir" ) ]
2174
2174
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2175
- pub fn create_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2176
- DirBuilder :: new ( ) . create ( path. as_ref ( ) )
2175
+ pub fn create_dir < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2176
+ DirBuilder :: new ( ) . create ( path)
2177
2177
}
2178
2178
2179
2179
/// Recursively create a directory and all of its parent components if they
@@ -2216,8 +2216,8 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2216
2216
/// }
2217
2217
/// ```
2218
2218
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2219
- pub fn create_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2220
- DirBuilder :: new ( ) . recursive ( true ) . create ( path. as_ref ( ) )
2219
+ pub fn create_dir_all < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2220
+ DirBuilder :: new ( ) . recursive ( true ) . create ( path)
2221
2221
}
2222
2222
2223
2223
/// Removes an empty directory.
@@ -2252,8 +2252,8 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2252
2252
/// ```
2253
2253
#[ doc( alias = "rmdir" ) ]
2254
2254
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2255
- pub fn remove_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2256
- fs_imp:: rmdir ( path . as_ref ( ) )
2255
+ pub fn remove_dir < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2256
+ path . with_path ( fs_imp:: rmdir)
2257
2257
}
2258
2258
2259
2259
/// Removes a directory at this path, after removing all its contents. Use
@@ -2294,8 +2294,8 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2294
2294
/// }
2295
2295
/// ```
2296
2296
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2297
- pub fn remove_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2298
- fs_imp:: remove_dir_all ( path . as_ref ( ) )
2297
+ pub fn remove_dir_all < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2298
+ path . with_path ( fs_imp:: remove_dir_all)
2299
2299
}
2300
2300
2301
2301
/// Returns an iterator over the entries within a directory.
@@ -2369,8 +2369,8 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2369
2369
/// }
2370
2370
/// ```
2371
2371
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2372
- pub fn read_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ReadDir > {
2373
- fs_imp:: readdir ( path . as_ref ( ) ) . map ( ReadDir )
2372
+ pub fn read_dir < P : PathLike > ( path : P ) -> io:: Result < ReadDir > {
2373
+ path . with_path ( fs_imp:: readdir) . map ( ReadDir )
2374
2374
}
2375
2375
2376
2376
/// Changes the permissions found on a file or a directory.
@@ -2404,8 +2404,8 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
2404
2404
/// }
2405
2405
/// ```
2406
2406
#[ stable( feature = "set_permissions" , since = "1.1.0" ) ]
2407
- pub fn set_permissions < P : AsRef < Path > > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2408
- fs_imp:: set_perm ( path. as_ref ( ) , perm. 0 )
2407
+ pub fn set_permissions < P : PathLike > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2408
+ path . with_path ( |path| fs_imp:: set_perm ( path, perm. 0 ) )
2409
2409
}
2410
2410
2411
2411
impl DirBuilder {
@@ -2464,8 +2464,8 @@ impl DirBuilder {
2464
2464
/// assert!(fs::metadata(path).unwrap().is_dir());
2465
2465
/// ```
2466
2466
#[ stable( feature = "dir_builder" , since = "1.6.0" ) ]
2467
- pub fn create < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
2468
- self . _create ( path. as_ref ( ) )
2467
+ pub fn create < P : PathLike > ( & self , path : P ) -> io:: Result < ( ) > {
2468
+ path . with_path ( | path| self . _create ( path ) )
2469
2469
}
2470
2470
2471
2471
fn _create ( & self , path : & Path ) -> io:: Result < ( ) > {
@@ -2534,6 +2534,6 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
2534
2534
// instead.
2535
2535
#[ unstable( feature = "fs_try_exists" , issue = "83186" ) ]
2536
2536
#[ inline]
2537
- pub fn try_exists < P : AsRef < Path > > ( path : P ) -> io:: Result < bool > {
2538
- fs_imp:: try_exists ( path . as_ref ( ) )
2537
+ pub fn try_exists < P : PathLike > ( path : P ) -> io:: Result < bool > {
2538
+ path . with_path ( fs_imp:: try_exists)
2539
2539
}
0 commit comments