@@ -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:: { 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,14 +246,14 @@ 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 mut bytes = Vec :: new ( ) ;
253
253
file. read_to_end ( & mut bytes) ?;
254
254
Ok ( bytes)
255
255
}
256
- inner ( path. as_ref ( ) )
256
+ path. with_path ( inner )
257
257
}
258
258
259
259
/// Read the entire contents of a file into a string.
@@ -285,14 +285,14 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
285
285
/// }
286
286
/// ```
287
287
#[ stable( feature = "fs_read_write" , since = "1.26.0" ) ]
288
- pub fn read_to_string < P : AsRef < Path > > ( path : P ) -> io:: Result < String > {
288
+ pub fn read_to_string < P : PathLike > ( path : P ) -> io:: Result < String > {
289
289
fn inner ( path : & Path ) -> io:: Result < String > {
290
290
let mut file = File :: open ( path) ?;
291
291
let mut string = String :: new ( ) ;
292
292
file. read_to_string ( & mut string) ?;
293
293
Ok ( string)
294
294
}
295
- inner ( path. as_ref ( ) )
295
+ path. with_path ( inner )
296
296
}
297
297
298
298
/// Write a slice as the entire contents of a file.
@@ -320,11 +320,11 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
320
320
/// }
321
321
/// ```
322
322
#[ stable( feature = "fs_read_write_bytes" , since = "1.26.0" ) ]
323
- pub fn write < P : AsRef < Path > , C : AsRef < [ u8 ] > > ( path : P , contents : C ) -> io:: Result < ( ) > {
323
+ pub fn write < P : PathLike , C : AsRef < [ u8 ] > > ( path : P , contents : C ) -> io:: Result < ( ) > {
324
324
fn inner ( path : & Path , contents : & [ u8 ] ) -> io:: Result < ( ) > {
325
325
File :: create ( path) ?. write_all ( contents)
326
326
}
327
- inner ( path. as_ref ( ) , contents. as_ref ( ) )
327
+ path . with_path ( |path| inner ( path. as_ref ( ) , contents. as_ref ( ) ) )
328
328
}
329
329
330
330
impl File {
@@ -348,8 +348,8 @@ impl File {
348
348
/// }
349
349
/// ```
350
350
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
351
- pub fn open < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
352
- OpenOptions :: new ( ) . read ( true ) . open ( path. as_ref ( ) )
351
+ pub fn open < P : PathLike > ( path : P ) -> io:: Result < File > {
352
+ OpenOptions :: new ( ) . read ( true ) . open ( path)
353
353
}
354
354
355
355
/// Opens a file in write-only mode.
@@ -373,8 +373,8 @@ impl File {
373
373
/// }
374
374
/// ```
375
375
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
376
- pub fn create < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
377
- OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
376
+ pub fn create < P : PathLike > ( path : P ) -> io:: Result < File > {
377
+ OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path)
378
378
}
379
379
380
380
/// Creates a new file in read-write mode; error if the file exists.
@@ -402,8 +402,8 @@ impl File {
402
402
/// }
403
403
/// ```
404
404
#[ unstable( feature = "file_create_new" , issue = "none" ) ]
405
- pub fn create_new < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
406
- OpenOptions :: new ( ) . read ( true ) . write ( true ) . create_new ( true ) . open ( path. as_ref ( ) )
405
+ pub fn create_new < P : PathLike > ( path : P ) -> io:: Result < File > {
406
+ OpenOptions :: new ( ) . read ( true ) . write ( true ) . create_new ( true ) . open ( path)
407
407
}
408
408
409
409
/// Returns a new OpenOptions object.
@@ -1052,8 +1052,8 @@ impl OpenOptions {
1052
1052
/// [`NotFound`]: io::ErrorKind::NotFound
1053
1053
/// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
1054
1054
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1055
- pub fn open < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < File > {
1056
- self . _open ( path. as_ref ( ) )
1055
+ pub fn open < P : PathLike > ( & self , path : P ) -> io:: Result < File > {
1056
+ path . with_path ( | path| self . _open ( path ) )
1057
1057
}
1058
1058
1059
1059
fn _open ( & self , path : & Path ) -> io:: Result < File > {
@@ -1723,8 +1723,8 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
1723
1723
/// }
1724
1724
/// ```
1725
1725
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1726
- pub fn remove_file < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
1727
- fs_imp:: unlink ( path . as_ref ( ) )
1726
+ pub fn remove_file < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
1727
+ path . with_path ( fs_imp:: unlink)
1728
1728
}
1729
1729
1730
1730
/// Given a path, query the file system to get information about a file,
@@ -1761,8 +1761,8 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
1761
1761
/// }
1762
1762
/// ```
1763
1763
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1764
- pub fn metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1765
- fs_imp:: stat ( path . as_ref ( ) ) . map ( Metadata )
1764
+ pub fn metadata < P : PathLike > ( path : P ) -> io:: Result < Metadata > {
1765
+ path . with_path ( fs_imp:: stat) . map ( Metadata )
1766
1766
}
1767
1767
1768
1768
/// Query the metadata about a file without following symlinks.
@@ -1795,8 +1795,8 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1795
1795
/// }
1796
1796
/// ```
1797
1797
#[ stable( feature = "symlink_metadata" , since = "1.1.0" ) ]
1798
- pub fn symlink_metadata < P : AsRef < Path > > ( path : P ) -> io:: Result < Metadata > {
1799
- fs_imp:: lstat ( path . as_ref ( ) ) . map ( Metadata )
1798
+ pub fn symlink_metadata < P : PathLike > ( path : P ) -> io:: Result < Metadata > {
1799
+ path . with_path ( fs_imp:: lstat) . map ( Metadata )
1800
1800
}
1801
1801
1802
1802
/// Rename a file or directory to a new name, replacing the original file if
@@ -1838,8 +1838,8 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1838
1838
/// }
1839
1839
/// ```
1840
1840
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1841
- pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < ( ) > {
1842
- fs_imp:: rename ( from. as_ref ( ) , to. as_ref ( ) )
1841
+ pub fn rename < P : PathLike , Q : PathLike > ( from : P , to : Q ) -> io:: Result < ( ) > {
1842
+ from . with_path ( |from| to . with_path ( |to| fs_imp:: rename ( from, to) ) )
1843
1843
}
1844
1844
1845
1845
/// Copies the contents of one file to another. This function will also
@@ -1896,8 +1896,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
1896
1896
/// }
1897
1897
/// ```
1898
1898
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1899
- pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < u64 > {
1900
- fs_imp:: copy ( from. as_ref ( ) , to. as_ref ( ) )
1899
+ pub fn copy < P : PathLike , Q : PathLike > ( from : P , to : Q ) -> io:: Result < u64 > {
1900
+ from . with_path ( |from| to . with_path ( |to| fs_imp:: copy ( from, to) ) )
1901
1901
}
1902
1902
1903
1903
/// Creates a new hard link on the filesystem.
@@ -1940,8 +1940,8 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
1940
1940
/// }
1941
1941
/// ```
1942
1942
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1943
- pub fn hard_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
1944
- fs_imp:: link ( original. as_ref ( ) , link. as_ref ( ) )
1943
+ pub fn hard_link < P : PathLike , Q : PathLike > ( original : P , link : Q ) -> io:: Result < ( ) > {
1944
+ original . with_path ( |original| link . with_path ( |link| fs_imp:: link ( original, link) ) )
1945
1945
}
1946
1946
1947
1947
/// Creates a new symbolic link on the filesystem.
@@ -1972,8 +1972,8 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
1972
1972
note = "replaced with std::os::unix::fs::symlink and \
1973
1973
std::os::windows::fs::{symlink_file, symlink_dir}"
1974
1974
) ]
1975
- pub fn soft_link < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) -> io:: Result < ( ) > {
1976
- fs_imp:: symlink ( original. as_ref ( ) , link. as_ref ( ) )
1975
+ pub fn soft_link < P : PathLike , Q : PathLike > ( original : P , link : Q ) -> io:: Result < ( ) > {
1976
+ original . with_path ( |original| link . with_path ( |link| fs_imp:: symlink ( original, link) ) )
1977
1977
}
1978
1978
1979
1979
/// Reads a symbolic link, returning the file that the link points to.
@@ -2006,8 +2006,8 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
2006
2006
/// }
2007
2007
/// ```
2008
2008
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2009
- pub fn read_link < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2010
- fs_imp:: readlink ( path . as_ref ( ) )
2009
+ pub fn read_link < P : PathLike > ( path : P ) -> io:: Result < PathBuf > {
2010
+ path . with_path ( fs_imp:: readlink)
2011
2011
}
2012
2012
2013
2013
/// Returns the canonical, absolute form of a path with all intermediate
@@ -2049,8 +2049,8 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2049
2049
#[ doc( alias = "realpath" ) ]
2050
2050
#[ doc( alias = "GetFinalPathNameByHandle" ) ]
2051
2051
#[ stable( feature = "fs_canonicalize" , since = "1.5.0" ) ]
2052
- pub fn canonicalize < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
2053
- fs_imp:: canonicalize ( path . as_ref ( ) )
2052
+ pub fn canonicalize < P : PathLike > ( path : P ) -> io:: Result < PathBuf > {
2053
+ path . with_path ( fs_imp:: canonicalize)
2054
2054
}
2055
2055
2056
2056
/// Creates a new, empty directory at the provided path
@@ -2090,8 +2090,8 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2090
2090
/// ```
2091
2091
#[ doc( alias = "mkdir" ) ]
2092
2092
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2093
- pub fn create_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2094
- DirBuilder :: new ( ) . create ( path. as_ref ( ) )
2093
+ pub fn create_dir < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2094
+ DirBuilder :: new ( ) . create ( path)
2095
2095
}
2096
2096
2097
2097
/// Recursively create a directory and all of its parent components if they
@@ -2134,8 +2134,8 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2134
2134
/// }
2135
2135
/// ```
2136
2136
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2137
- pub fn create_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2138
- DirBuilder :: new ( ) . recursive ( true ) . create ( path. as_ref ( ) )
2137
+ pub fn create_dir_all < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2138
+ DirBuilder :: new ( ) . recursive ( true ) . create ( path)
2139
2139
}
2140
2140
2141
2141
/// Removes an empty directory.
@@ -2170,8 +2170,8 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2170
2170
/// ```
2171
2171
#[ doc( alias = "rmdir" ) ]
2172
2172
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2173
- pub fn remove_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2174
- fs_imp:: rmdir ( path . as_ref ( ) )
2173
+ pub fn remove_dir < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2174
+ path . with_path ( fs_imp:: rmdir)
2175
2175
}
2176
2176
2177
2177
/// Removes a directory at this path, after removing all its contents. Use
@@ -2212,8 +2212,8 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2212
2212
/// }
2213
2213
/// ```
2214
2214
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2215
- pub fn remove_dir_all < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
2216
- fs_imp:: remove_dir_all ( path . as_ref ( ) )
2215
+ pub fn remove_dir_all < P : PathLike > ( path : P ) -> io:: Result < ( ) > {
2216
+ path . with_path ( fs_imp:: remove_dir_all)
2217
2217
}
2218
2218
2219
2219
/// Returns an iterator over the entries within a directory.
@@ -2287,8 +2287,8 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
2287
2287
/// }
2288
2288
/// ```
2289
2289
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2290
- pub fn read_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ReadDir > {
2291
- fs_imp:: readdir ( path . as_ref ( ) ) . map ( ReadDir )
2290
+ pub fn read_dir < P : PathLike > ( path : P ) -> io:: Result < ReadDir > {
2291
+ path . with_path ( fs_imp:: readdir) . map ( ReadDir )
2292
2292
}
2293
2293
2294
2294
/// Changes the permissions found on a file or a directory.
@@ -2322,8 +2322,8 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
2322
2322
/// }
2323
2323
/// ```
2324
2324
#[ stable( feature = "set_permissions" , since = "1.1.0" ) ]
2325
- pub fn set_permissions < P : AsRef < Path > > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2326
- fs_imp:: set_perm ( path. as_ref ( ) , perm. 0 )
2325
+ pub fn set_permissions < P : PathLike > ( path : P , perm : Permissions ) -> io:: Result < ( ) > {
2326
+ path . with_path ( |path| fs_imp:: set_perm ( path, perm. 0 ) )
2327
2327
}
2328
2328
2329
2329
impl DirBuilder {
@@ -2382,8 +2382,8 @@ impl DirBuilder {
2382
2382
/// assert!(fs::metadata(path).unwrap().is_dir());
2383
2383
/// ```
2384
2384
#[ stable( feature = "dir_builder" , since = "1.6.0" ) ]
2385
- pub fn create < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
2386
- self . _create ( path. as_ref ( ) )
2385
+ pub fn create < P : PathLike > ( & self , path : P ) -> io:: Result < ( ) > {
2386
+ path . with_path ( | path| self . _create ( path ) )
2387
2387
}
2388
2388
2389
2389
fn _create ( & self , path : & Path ) -> io:: Result < ( ) > {
@@ -2452,6 +2452,6 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
2452
2452
// instead.
2453
2453
#[ unstable( feature = "fs_try_exists" , issue = "83186" ) ]
2454
2454
#[ inline]
2455
- pub fn try_exists < P : AsRef < Path > > ( path : P ) -> io:: Result < bool > {
2456
- fs_imp:: try_exists ( path . as_ref ( ) )
2455
+ pub fn try_exists < P : PathLike > ( path : P ) -> io:: Result < bool > {
2456
+ path . with_path ( fs_imp:: try_exists)
2457
2457
}
0 commit comments