Skip to content

Commit 7d96376

Browse files
committed
add documentation, implementations for unsupported platforms
1 parent df275b6 commit 7d96376

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

library/std/src/fs.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,24 @@ pub enum TryLockError {
154154
}
155155

156156
#[unstable(feature = "dirfd", issue = "120426")]
157-
#[cfg(target_family = "unix")]
158157
/// An object providing access to a directory on the filesystem.
158+
///
159+
/// Files are automatically closed when they go out of scope. Errors detected
160+
/// on closing are ignored by the implementation of `Drop`.
161+
///
162+
/// # Examples
163+
///
164+
/// Opens a directory and then a file inside it.
165+
///
166+
/// ```no_run
167+
/// use std::fs::Dir;
168+
///
169+
/// fn main() -> std::io::Result<()> {
170+
/// let dir = Dir::new("/home/foo")?;
171+
/// let file = dir.open("bar.txt")?;
172+
/// Ok(())
173+
/// }
174+
/// ```
159175
pub struct Dir {
160176
inner: fs_imp::Dir,
161177
}
@@ -1481,13 +1497,21 @@ impl Seek for Arc<File> {
14811497
}
14821498
}
14831499

1484-
#[unstable(feature = "dirfd", issue = "120426")]
14851500
impl Dir {
14861501
/// Opens a file relative to this directory.
1502+
///
1503+
/// # Examples
1504+
/// ```no_run
1505+
/// use std::fs::Dir;
1506+
///
1507+
/// let dir = Dir::new("foo")?;
1508+
/// ```
1509+
#[unstable(feature = "dirfd", issue = "120426")]
14871510
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
14881511
self.inner.open(path).map(|f| File { inner: f })
14891512
}
14901513
/// Opens a file relative to this directory with the specified options.
1514+
#[unstable(feature = "dirfd", issue = "120426")]
14911515
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
14921516
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
14931517
}

library/std/src/sys/fs/unix.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,41 @@ impl Dir {
301301
})?;
302302
Ok(File(unsafe { FileDesc::from_raw_fd(fd) }))
303303
}
304+
}
304305

305-
// pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
306-
// pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(&self, from: P, to_dir: &Self, to: Q) -> Result<()>
307-
// pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
308-
// pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
309-
// pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(&self, original: P, link: Q)
306+
#[cfg(any(
307+
miri,
308+
target_os = "redox",
309+
target_os = "nto",
310+
target_os = "vita",
311+
target_os = "hurd",
312+
target_os = "espidf",
313+
target_os = "horizon",
314+
target_os = "vxworks",
315+
target_os = "rtems",
316+
target_os = "nuttx",
317+
))]
318+
impl Dir {
319+
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
320+
Err(io::const_error!(
321+
io::ErrorKind::Unsupported,
322+
"directory handles are not available for this platform",
323+
))
324+
}
325+
326+
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
327+
Err(io::const_error!(
328+
io::ErrorKind::Unsupported,
329+
"directory handles are not available for this platform",
330+
))
331+
}
332+
333+
pub fn open_c(&self, path: &CStr, opts: &OpenOptions) -> io::Result<File> {
334+
Err(io::const_error!(
335+
io::ErrorKind::Unsupported,
336+
"directory handles are not available for this platform",
337+
))
338+
}
310339
}
311340

312341
fn get_path_from_fd(fd: c_int) -> Option<PathBuf> {

0 commit comments

Comments
 (0)