@@ -167,7 +167,7 @@ pub enum TryLockError {
167167/// use std::fs::Dir;
168168///
169169/// fn main() -> std::io::Result<()> {
170- /// let dir = Dir::new("/home/ foo")?;
170+ /// let dir = Dir::new("foo")?;
171171/// let file = dir.open("bar.txt")?;
172172/// Ok(())
173173/// }
@@ -1498,23 +1498,197 @@ impl Seek for Arc<File> {
14981498}
14991499
15001500impl Dir {
1501- /// Opens a file relative to this directory.
1501+ /// Attempts to open a directory at `path` in read-only mode.
1502+ ///
1503+ /// See [`new_with`] for more options.
1504+ ///
1505+ /// # Errors
1506+ ///
1507+ /// This function will return an error in these (and other) situations:
1508+ /// * The path doesn't exist
1509+ /// * The path doesn't specify a directory
1510+ /// * The process doesn't have permission to read the directory
1511+ ///
1512+ /// # Examples
1513+ ///
1514+ /// ```no_run
1515+ /// use std::fs::Dir;
1516+ ///
1517+ /// fn main() -> std::io::Result<()> {
1518+ /// let dir = Dir::new("foo")?;
1519+ /// let mut f = dir.open("bar.txt")?;
1520+ /// let mut data = vec![];
1521+ /// f.read_to_end(&mut data)?;
1522+ /// Ok(())
1523+ /// }
1524+ /// ```
1525+ ///
1526+ /// [`new_with`]: Dir::new_with
1527+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1528+ pub fn new < P : AsRef < Path > > ( path : P ) -> io:: Result < Self > {
1529+ Ok ( Self { inner : fs_imp:: Dir :: new ( path) ? } )
1530+ }
1531+
1532+ /// Attempts to open a directory at `path` with the options specified by `opts`.
1533+ ///
1534+ /// # Errors
1535+ ///
1536+ /// This function will return an error in these (and other) situations:
1537+ /// * The path doesn't exist
1538+ /// * The path doesn't specify a directory
1539+ /// * The process doesn't have permission to read/write (according to `opts`) the directory
15021540 ///
15031541 /// # Examples
1542+ ///
15041543 /// ```no_run
15051544 /// use std::fs::Dir;
15061545 ///
1507- /// let dir = Dir::new("foo")?;
1546+ /// fn main() -> std::io::Result<()> {
1547+ /// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
1548+ /// let mut f = dir.remove_file("bar.txt")?;
1549+ /// Ok(())
1550+ /// }
1551+ /// ```
1552+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1553+ pub fn new_with < P : AsRef < Path > > ( path : P , opts : & OpenOptions ) -> io:: Result < Self > {
1554+ Ok ( Self { inner : fs_imp:: Dir :: new_with ( path, & opts. 0 ) ? } )
1555+ }
1556+
1557+ /// Attempts to open a file relative to this directory.
1558+ ///
1559+ /// # Errors
1560+ ///
1561+ /// This function will return an error in these (and other) situations:
1562+ /// * The path doesn't exist
1563+ /// * The path doesn't specify a regular file
1564+ /// * The process doesn't have permission to read/write (according to `opts`) the directory
1565+ ///
1566+ /// # Examples
1567+ ///
1568+ /// ```no_run
1569+ /// use std::fs::Dir;
1570+ ///
1571+ /// fn main() -> std::io::Result<()> {
1572+ /// let dir = Dir::new("foo")?;
1573+ /// let mut f = dir.open("bar.txt")?;
1574+ /// let mut data = vec![];
1575+ /// f.read_to_end(&mut data)?;
1576+ /// Ok(())
1577+ /// }
15081578 /// ```
15091579 #[ unstable( feature = "dirfd" , issue = "120426" ) ]
15101580 pub fn open < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < File > {
15111581 self . inner . open ( path) . map ( |f| File { inner : f } )
15121582 }
1513- /// Opens a file relative to this directory with the specified options.
1583+
1584+ /// Attempts to open a file relative to this directory with the options specified by `opts`.
1585+ ///
1586+ /// # Errors
1587+ ///
1588+ /// This function will return an error in these (and other) situations:
1589+ /// * The path doesn't exist
1590+ /// * The path doesn't specify a regular file
1591+ /// * The process doesn't have permission to read/write (according to `opts`) the directory
1592+ ///
1593+ /// # Examples
1594+ ///
1595+ /// ```no_run
1596+ /// use std::fs::Dir;
1597+ ///
1598+ /// fn main() -> std::io::Result<()> {
1599+ /// let dir = Dir::new("foo")?;
1600+ /// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
1601+ /// let mut data = vec![];
1602+ /// f.read_to_end(&mut data)?;
1603+ /// Ok(())
1604+ /// }
1605+ /// ```
15141606 #[ unstable( feature = "dirfd" , issue = "120426" ) ]
15151607 pub fn open_with < P : AsRef < Path > > ( & self , path : P , opts : & OpenOptions ) -> io:: Result < File > {
15161608 self . inner . open_with ( path, & opts. 0 ) . map ( |f| File { inner : f } )
15171609 }
1610+
1611+ /// Attempts to remove a file relative to this directory.
1612+ ///
1613+ /// # Errors
1614+ ///
1615+ /// This function will return an error in these (and other) situations:
1616+ /// * The path doesn't exist
1617+ /// * The path doesn't specify a regular file
1618+ /// * The process doesn't have permission to delete the file.
1619+ ///
1620+ /// # Examples
1621+ ///
1622+ /// ```no_run
1623+ /// use std::fs::Dir;
1624+ ///
1625+ /// fn main() -> std::io::Result<()> {
1626+ /// let dir = Dir::new("foo")?;
1627+ /// dir.remove_file("bar.txt")?;
1628+ /// Ok(())
1629+ /// }
1630+ /// ```
1631+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1632+ pub fn remove_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1633+ self . inner . remove_file ( path)
1634+ }
1635+
1636+ /// Attempts to remove a directory relative to this directory.
1637+ ///
1638+ /// # Errors
1639+ ///
1640+ /// This function will return an error in these (and other) situations:
1641+ /// * The path doesn't exist
1642+ /// * The path doesn't specify a directory
1643+ /// * The directory isn't empty
1644+ /// * The process doesn't have permission to delete the directory.
1645+ ///
1646+ /// # Examples
1647+ ///
1648+ /// ```no_run
1649+ /// use std::fs::Dir;
1650+ ///
1651+ /// fn main() -> std::io::Result<()> {
1652+ /// let dir = Dir::new("foo")?;
1653+ /// dir.remove_dir("baz")?;
1654+ /// Ok(())
1655+ /// }
1656+ /// ```
1657+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1658+ pub fn remove_dir < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1659+ self . inner . remove_dir ( path)
1660+ }
1661+
1662+ /// Attempts to rename a file or directory relative to this directory to a new name, replacing
1663+ /// the destination file if present.
1664+ ///
1665+ /// # Errors
1666+ ///
1667+ /// This function will return an error in these (and other) situations:
1668+ /// * The `from` path doesn't exist
1669+ /// * The `from` path doesn't specify a directory
1670+ /// * `self` and `to_dir` are on different mount points
1671+ ///
1672+ /// # Examples
1673+ ///
1674+ /// ```no_run
1675+ /// use std::fs::Dir;
1676+ ///
1677+ /// fn main() -> std::io::Result<()> {
1678+ /// let dir = Dir::new("foo")?;
1679+ /// dir.rename("bar.txt", &dir, "quux.txt")?;
1680+ /// Ok(())
1681+ /// }
1682+ /// ```
1683+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1684+ pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > (
1685+ & self ,
1686+ from : P ,
1687+ to_dir : & Self ,
1688+ to : Q ,
1689+ ) -> io:: Result < ( ) > {
1690+ self . inner . rename ( from, & to_dir. inner , to)
1691+ }
15181692}
15191693
15201694#[ unstable( feature = "dirfd" , issue = "120426" ) ]
0 commit comments