@@ -153,6 +153,32 @@ pub enum TryLockError {
153153 WouldBlock ,
154154}
155155
156+ /// An object providing access to a directory on the filesystem.
157+ ///
158+ /// Directories are automatically closed when they go out of scope. Errors detected
159+ /// on closing are ignored by the implementation of `Drop`.
160+ ///
161+ /// # Examples
162+ ///
163+ /// Opens a directory and then a file inside it.
164+ ///
165+ /// ```no_run
166+ /// #![feature(dirfd)]
167+ /// use std::{fs::Dir, io};
168+ ///
169+ /// fn main() -> std::io::Result<()> {
170+ /// let dir = Dir::open("foo")?;
171+ /// let mut file = dir.open_file("bar.txt")?;
172+ /// let contents = io::read_to_string(file)?;
173+ /// assert_eq!(contents, "Hello, world!");
174+ /// Ok(())
175+ /// }
176+ /// ```
177+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
178+ pub struct Dir {
179+ inner : fs_imp:: Dir ,
180+ }
181+
156182/// Metadata information about a file.
157183///
158184/// This structure is returned from the [`metadata`] or
@@ -1474,6 +1500,87 @@ impl Seek for Arc<File> {
14741500 }
14751501}
14761502
1503+ impl Dir {
1504+ /// Attempts to open a directory at `path` in read-only mode.
1505+ ///
1506+ /// # Errors
1507+ ///
1508+ /// This function will return an error if `path` does not point to an existing directory.
1509+ /// Other errors may also be returned according to [`OpenOptions::open`].
1510+ ///
1511+ /// # Examples
1512+ ///
1513+ /// ```no_run
1514+ /// #![feature(dirfd)]
1515+ /// use std::{fs::Dir, io};
1516+ ///
1517+ /// fn main() -> std::io::Result<()> {
1518+ /// let dir = Dir::open("foo")?;
1519+ /// let mut f = dir.open_file("bar.txt")?;
1520+ /// let contents = io::read_to_string(f)?;
1521+ /// assert_eq!(contents, "Hello, world!");
1522+ /// Ok(())
1523+ /// }
1524+ /// ```
1525+ ///
1526+ /// [`new_with`]: Dir::new_with
1527+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1528+ pub fn open < P : AsRef < Path > > ( path : P ) -> io:: Result < Self > {
1529+ fs_imp:: Dir :: open ( path) . map ( |inner| Self { inner } )
1530+ }
1531+
1532+ ///
1533+ /// Attempts to open a file in read-only mode relative to this directory.
1534+ ///
1535+ /// # Errors
1536+ ///
1537+ /// This function will return an error if `path` does not point to an existing file.
1538+ /// Other errors may also be returned according to [`OpenOptions::open`].
1539+ ///
1540+ /// # Examples
1541+ ///
1542+ /// ```no_run
1543+ /// #![feature(dirfd)]
1544+ /// use std::{fs::Dir, io};
1545+ ///
1546+ /// fn main() -> std::io::Result<()> {
1547+ /// let dir = Dir::open("foo")?;
1548+ /// let mut f = dir.open_file("bar.txt")?;
1549+ /// let contents = io::read_to_string(f)?;
1550+ /// assert_eq!(contents, "Hello, world!");
1551+ /// Ok(())
1552+ /// }
1553+ /// ```
1554+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1555+ pub fn open_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < File > {
1556+ self . inner . open_file ( path) . map ( |f| File { inner : f } )
1557+ }
1558+ }
1559+
1560+ impl AsInner < fs_imp:: Dir > for Dir {
1561+ #[ inline]
1562+ fn as_inner ( & self ) -> & fs_imp:: Dir {
1563+ & self . inner
1564+ }
1565+ }
1566+ impl FromInner < fs_imp:: Dir > for Dir {
1567+ fn from_inner ( f : fs_imp:: Dir ) -> Dir {
1568+ Dir { inner : f }
1569+ }
1570+ }
1571+ impl IntoInner < fs_imp:: Dir > for Dir {
1572+ fn into_inner ( self ) -> fs_imp:: Dir {
1573+ self . inner
1574+ }
1575+ }
1576+
1577+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1578+ impl fmt:: Debug for Dir {
1579+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1580+ self . inner . fmt ( f)
1581+ }
1582+ }
1583+
14771584impl OpenOptions {
14781585 /// Creates a blank new set of options ready for configuration.
14791586 ///
0 commit comments