Skip to content

Commit 05f87c5

Browse files
committed
add documentation, implementations for unsupported platforms
1 parent bcd769d commit 05f87c5

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
@@ -133,8 +133,24 @@ pub enum TryLockError {
133133
}
134134

135135
#[unstable(feature = "dirfd", issue = "120426")]
136-
#[cfg(target_family = "unix")]
137136
/// An object providing access to a directory on the filesystem.
137+
///
138+
/// Files are automatically closed when they go out of scope. Errors detected
139+
/// on closing are ignored by the implementation of `Drop`.
140+
///
141+
/// # Examples
142+
///
143+
/// Opens a directory and then a file inside it.
144+
///
145+
/// ```no_run
146+
/// use std::fs::Dir;
147+
///
148+
/// fn main() -> std::io::Result<()> {
149+
/// let dir = Dir::new("/home/foo")?;
150+
/// let file = dir.open("bar.txt")?;
151+
/// Ok(())
152+
/// }
153+
/// ```
138154
pub struct Dir {
139155
inner: fs_imp::Dir,
140156
}
@@ -1460,13 +1476,21 @@ impl Seek for Arc<File> {
14601476
}
14611477
}
14621478

1463-
#[unstable(feature = "dirfd", issue = "120426")]
14641479
impl Dir {
14651480
/// Opens a file relative to this directory.
1481+
///
1482+
/// # Examples
1483+
/// ```no_run
1484+
/// use std::fs::Dir;
1485+
///
1486+
/// let dir = Dir::new("foo")?;
1487+
/// ```
1488+
#[unstable(feature = "dirfd", issue = "120426")]
14661489
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
14671490
self.inner.open(path).map(|f| File { inner: f })
14681491
}
14691492
/// Opens a file relative to this directory with the specified options.
1493+
#[unstable(feature = "dirfd", issue = "120426")]
14701494
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
14711495
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
14721496
}

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)