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