@@ -1547,6 +1547,8 @@ impl crate::io::IoHandle for File {}
15471547impl Dir {
15481548 /// Attempts to open a directory at `path` in read-only mode.
15491549 ///
1550+ /// This function opens a directory. To open a file instead, see [`File::open`].
1551+ ///
15501552 /// # Errors
15511553 ///
15521554 /// This function will return an error if `path` does not point to an existing directory.
@@ -1574,6 +1576,9 @@ impl Dir {
15741576
15751577 /// Attempts to open a file in read-only mode relative to this directory.
15761578 ///
1579+ /// This function interprets `path` relative to the directory provided by `self`. To open a file
1580+ /// relative to the current working directory, or at an absolute path, see [`File::open`].
1581+ ///
15771582 /// # Errors
15781583 ///
15791584 /// This function will return an error if `path` does not point to an existing file.
@@ -1599,6 +1604,98 @@ impl Dir {
15991604 . open_file ( path. as_ref ( ) , & OpenOptions :: new ( ) . read ( true ) . 0 )
16001605 . map ( |f| File { inner : f } )
16011606 }
1607+
1608+ /// Attempts to open a file according to `opts` relative to this directory.
1609+ ///
1610+ /// This function interprets `path` relative to the directory provided by `self`. To open a file
1611+ /// relative to the current working directory, or at an absolute path, see [`File::open`].
1612+ ///
1613+ /// # Errors
1614+ ///
1615+ /// This function will return an error if `path` does not point to an existing file.
1616+ /// Other errors may also be returned according to [`OpenOptions::open`].
1617+ ///
1618+ /// # Examples
1619+ ///
1620+ /// ```no_run
1621+ /// #![feature(dirfd)]
1622+ /// use std::{fs::{Dir, OpenOptions}, io::{self, Write}};
1623+ ///
1624+ /// fn main() -> io::Result<()> {
1625+ /// let dir = Dir::open("foo")?;
1626+ /// let mut opts = OpenOptions::new();
1627+ /// opts.read(true).write(true);
1628+ /// let mut f = dir.open_file_with("bar.txt", &opts)?;
1629+ /// f.write_all(b"Hello, world!")?;
1630+ /// let contents = io::read_to_string(f)?;
1631+ /// assert_eq!(contents, "Hello, world!");
1632+ /// Ok(())
1633+ /// }
1634+ /// ```
1635+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1636+ pub fn open_file_with < P : AsRef < Path > > ( & self , path : P , opts : & OpenOptions ) -> io:: Result < File > {
1637+ self . inner . open_file ( path. as_ref ( ) , & opts. 0 ) . map ( |f| File { inner : f } )
1638+ }
1639+
1640+ /// Attempts to remove a file relative to this directory.
1641+ ///
1642+ /// This function interprets `path` relative to the directory provided by `self`. To remove a file
1643+ /// relative to the current working directory, or at an absolute path, see [`fs::remove_file`][remove_file].
1644+ ///
1645+ /// # Errors
1646+ ///
1647+ /// This function will return an error if `path` does not point to an existing file.
1648+ /// Other errors may also be returned according to [`OpenOptions::open`].
1649+ ///
1650+ /// # Examples
1651+ ///
1652+ /// ```no_run
1653+ /// #![feature(dirfd)]
1654+ /// use std::fs::Dir;
1655+ ///
1656+ /// fn main() -> std::io::Result<()> {
1657+ /// let dir = Dir::open("foo")?;
1658+ /// dir.remove_file("bar.txt")?;
1659+ /// Ok(())
1660+ /// }
1661+ /// ```
1662+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1663+ pub fn remove_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1664+ self . inner . remove_file ( path. as_ref ( ) )
1665+ }
1666+
1667+ /// Attempts to rename a file or directory relative to this directory to a new name, replacing
1668+ /// the destination file if present.
1669+ ///
1670+ /// This function interprets `from` relative to the directory provided by `self` and `to` relative to the directory
1671+ /// provided by `to_dir`. To rename a file relative to the current working directory, or at an absolute path, see [`fs::rename`][rename].
1672+ ///
1673+ /// # Errors
1674+ ///
1675+ /// This function will return an error if `from` does not point to an existing file or directory.
1676+ /// Other errors may also be returned according to [`OpenOptions::open`].
1677+ ///
1678+ /// # Examples
1679+ ///
1680+ /// ```no_run
1681+ /// #![feature(dirfd)]
1682+ /// use std::fs::Dir;
1683+ ///
1684+ /// fn main() -> std::io::Result<()> {
1685+ /// let dir = Dir::open("foo")?;
1686+ /// dir.rename("bar.txt", &dir, "quux.txt")?;
1687+ /// Ok(())
1688+ /// }
1689+ /// ```
1690+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1691+ pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > (
1692+ & self ,
1693+ from : P ,
1694+ to_dir : & Self ,
1695+ to : Q ,
1696+ ) -> io:: Result < ( ) > {
1697+ self . inner . rename ( from. as_ref ( ) , & to_dir. inner , to. as_ref ( ) )
1698+ }
16021699}
16031700
16041701impl AsInner < fs_imp:: Dir > for Dir {
0 commit comments