Skip to content

Commit 014790d

Browse files
committed
add documentation, implementations for unsupported platforms
1 parent 4810739 commit 014790d

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
}
@@ -1476,13 +1492,21 @@ impl Seek for Arc<File> {
14761492
}
14771493
}
14781494

1479-
#[unstable(feature = "dirfd", issue = "120426")]
14801495
impl Dir {
14811496
/// Opens a file relative to this directory.
1497+
///
1498+
/// # Examples
1499+
/// ```no_run
1500+
/// use std::fs::Dir;
1501+
///
1502+
/// let dir = Dir::new("foo")?;
1503+
/// ```
1504+
#[unstable(feature = "dirfd", issue = "120426")]
14821505
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
14831506
self.inner.open(path).map(|f| File { inner: f })
14841507
}
14851508
/// Opens a file relative to this directory with the specified options.
1509+
#[unstable(feature = "dirfd", issue = "120426")]
14861510
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
14871511
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
14881512
}

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

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

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

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

0 commit comments

Comments
 (0)