@@ -1649,6 +1649,89 @@ impl Dir {
16491649 . open_file ( path. as_ref ( ) , & OpenOptions :: new ( ) . read ( true ) . 0 )
16501650 . map ( |f| File { inner : f } )
16511651 }
1652+
1653+ /// Attempts to open a file according to `opts` relative to this directory.
1654+ ///
1655+ /// # Errors
1656+ ///
1657+ /// This function will return an error if `path` does not point to an existing file.
1658+ /// Other errors may also be returned according to [`OpenOptions::open`].
1659+ ///
1660+ /// # Examples
1661+ ///
1662+ /// ```no_run
1663+ /// #![feature(dirfd)]
1664+ /// use std::{fs::{Dir, OpenOptions}, io::{self, Write}};
1665+ ///
1666+ /// fn main() -> io::Result<()> {
1667+ /// let dir = Dir::open("foo")?;
1668+ /// let mut opts = OpenOptions::new();
1669+ /// opts.read(true).write(true);
1670+ /// let mut f = dir.open_file_with("bar.txt", &opts)?;
1671+ /// f.write(b"Hello, world!")?;
1672+ /// let contents = io::read_to_string(f)?;
1673+ /// assert_eq!(contents, "Hello, world!");
1674+ /// Ok(())
1675+ /// }
1676+ /// ```
1677+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1678+ pub fn open_file_with < P : AsRef < Path > > ( & self , path : P , opts : & OpenOptions ) -> io:: Result < File > {
1679+ self . inner . open_file ( path. as_ref ( ) , & opts. 0 ) . map ( |f| File { inner : f } )
1680+ }
1681+
1682+ /// Attempts to remove a file relative to this directory.
1683+ ///
1684+ /// # Errors
1685+ ///
1686+ /// This function will return an error if `path` does not point to an existing file.
1687+ /// Other errors may also be returned according to [`OpenOptions::open`].
1688+ ///
1689+ /// # Examples
1690+ ///
1691+ /// ```no_run
1692+ /// #![feature(dirfd)]
1693+ /// use std::fs::Dir;
1694+ ///
1695+ /// fn main() -> std::io::Result<()> {
1696+ /// let dir = Dir::open("foo")?;
1697+ /// dir.remove_file("bar.txt")?;
1698+ /// Ok(())
1699+ /// }
1700+ /// ```
1701+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1702+ pub fn remove_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1703+ self . inner . remove_file ( path. as_ref ( ) )
1704+ }
1705+
1706+ /// Attempts to rename a file or directory relative to this directory to a new name, replacing
1707+ /// the destination file if present.
1708+ ///
1709+ /// # Errors
1710+ ///
1711+ /// This function will return an error if `from` does not point to an existing file or directory.
1712+ /// Other errors may also be returned according to [`OpenOptions::open`].
1713+ ///
1714+ /// # Examples
1715+ ///
1716+ /// ```no_run
1717+ /// #![feature(dirfd)]
1718+ /// use std::fs::Dir;
1719+ ///
1720+ /// fn main() -> std::io::Result<()> {
1721+ /// let dir = Dir::open("foo")?;
1722+ /// dir.rename("bar.txt", &dir, "quux.txt")?;
1723+ /// Ok(())
1724+ /// }
1725+ /// ```
1726+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1727+ pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > (
1728+ & self ,
1729+ from : P ,
1730+ to_dir : & Self ,
1731+ to : Q ,
1732+ ) -> io:: Result < ( ) > {
1733+ self . inner . rename ( from. as_ref ( ) , & to_dir. inner , to. as_ref ( ) )
1734+ }
16521735}
16531736
16541737impl AsInner < fs_imp:: Dir > for Dir {
0 commit comments