Skip to content

Commit 4691b25

Browse files
committed
add documentation, implementations for unsupported platforms
1 parent 80ab2d4 commit 4691b25

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

library/std/src/fs.rs

+26-2
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
}
@@ -1409,13 +1425,21 @@ impl Seek for Arc<File> {
14091425
}
14101426
}
14111427

1412-
#[unstable(feature = "dirfd", issue = "120426")]
14131428
impl Dir {
14141429
/// Opens a file relative to this directory.
1430+
///
1431+
/// # Examples
1432+
/// ```no_run
1433+
/// use std::fs::Dir;
1434+
///
1435+
/// let dir = Dir::new("foo")?;
1436+
/// ```
1437+
#[unstable(feature = "dirfd", issue = "120426")]
14151438
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
14161439
self.inner.open(path).map(|f| File { inner: f })
14171440
}
14181441
/// Opens a file relative to this directory with the specified options.
1442+
#[unstable(feature = "dirfd", issue = "120426")]
14191443
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
14201444
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
14211445
}

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

+34-5
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)