Skip to content

Commit b1fe074

Browse files
committed
improve documentation, tests
1 parent 2504c15 commit b1fe074

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

library/std/src/fs.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,8 @@ impl crate::io::IoHandle for File {}
15461546
impl Dir {
15471547
/// Attempts to open a directory at `path` in read-only mode.
15481548
///
1549+
/// This function opens a directory. To open a file instead, see [`File::open`].
1550+
///
15491551
/// # Errors
15501552
///
15511553
/// This function will return an error if `path` does not point to an existing directory.
@@ -1573,6 +1575,9 @@ impl Dir {
15731575

15741576
/// Attempts to open a file in read-only mode relative to this directory.
15751577
///
1578+
/// This function interprets `path` relative to the directory provided by `self`. To open a file
1579+
/// relative to the current working directory, or at an absolute path, see [`File::open`].
1580+
///
15761581
/// # Errors
15771582
///
15781583
/// This function will return an error if `path` does not point to an existing file.
@@ -1601,6 +1606,9 @@ impl Dir {
16011606

16021607
/// Attempts to open a file according to `opts` relative to this directory.
16031608
///
1609+
/// This function interprets `path` relative to the directory provided by `self`. To open a file
1610+
/// relative to the current working directory, or at an absolute path, see [`File::open`].
1611+
///
16041612
/// # Errors
16051613
///
16061614
/// This function will return an error if `path` does not point to an existing file.
@@ -1617,7 +1625,7 @@ impl Dir {
16171625
/// let mut opts = OpenOptions::new();
16181626
/// opts.read(true).write(true);
16191627
/// let mut f = dir.open_file_with("bar.txt", &opts)?;
1620-
/// f.write(b"Hello, world!")?;
1628+
/// f.write_all(b"Hello, world!")?;
16211629
/// let contents = io::read_to_string(f)?;
16221630
/// assert_eq!(contents, "Hello, world!");
16231631
/// Ok(())
@@ -1630,6 +1638,9 @@ impl Dir {
16301638

16311639
/// Attempts to remove a file relative to this directory.
16321640
///
1641+
/// This function interprets `path` relative to the directory provided by `self`. To remove a file
1642+
/// relative to the current working directory, or at an absolute path, see [`fs::remove_file`][remove_file].
1643+
///
16331644
/// # Errors
16341645
///
16351646
/// This function will return an error if `path` does not point to an existing file.
@@ -1655,6 +1666,9 @@ impl Dir {
16551666
/// Attempts to rename a file or directory relative to this directory to a new name, replacing
16561667
/// the destination file if present.
16571668
///
1669+
/// This function interprets `from` relative to the directory provided by `self` and `to` relative to the directory
1670+
/// provided by `to_dir`. To rename a file relative to the current working directory, or at an absolute path, see [`fs::rename`][rename].
1671+
///
16581672
/// # Errors
16591673
///
16601674
/// This function will return an error if `from` does not point to an existing file or directory.

library/std/src/fs/tests.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,22 +2532,19 @@ fn test_fs_set_times_nofollow() {
25322532
}
25332533

25342534
#[test]
2535-
// FIXME: libc calls fail on miri
2536-
#[cfg(not(miri))]
2535+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25372536
fn test_dir_smoke_test() {
25382537
let tmpdir = tmpdir();
25392538
let dir = Dir::open(tmpdir.path());
25402539
check!(dir);
25412540
}
25422541

25432542
#[test]
2544-
// FIXME: libc calls fail on miri
2545-
#[cfg(not(miri))]
2543+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25462544
fn test_dir_read_file() {
25472545
let tmpdir = tmpdir();
25482546
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2549-
check!(f.write(b"bar"));
2550-
check!(f.flush());
2547+
check!(f.write_all(b"bar"));
25512548
drop(f);
25522549
let dir = check!(Dir::open(tmpdir.path()));
25532550
let f = check!(dir.open_file("foo.txt"));
@@ -2559,8 +2556,7 @@ fn test_dir_read_file() {
25592556
}
25602557

25612558
#[test]
2562-
// FIXME: libc calls fail on miri
2563-
#[cfg(not(miri))]
2559+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25642560
fn test_dir_write_file() {
25652561
let tmpdir = tmpdir();
25662562
let dir = check!(Dir::open(tmpdir.path()));
@@ -2575,8 +2571,7 @@ fn test_dir_write_file() {
25752571
}
25762572

25772573
#[test]
2578-
// FIXME: libc calls fail on miri
2579-
#[cfg(not(miri))]
2574+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25802575
fn test_dir_remove_file() {
25812576
let tmpdir = tmpdir();
25822577
let mut f = check!(File::create(tmpdir.join("foo.txt")));
@@ -2589,13 +2584,11 @@ fn test_dir_remove_file() {
25892584
}
25902585

25912586
#[test]
2592-
// FIXME: libc calls fail on miri
2593-
#[cfg(not(miri))]
2587+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25942588
fn test_dir_rename_file() {
25952589
let tmpdir = tmpdir();
25962590
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2597-
check!(f.write(b"bar"));
2598-
check!(f.flush());
2591+
check!(f.write_all(b"bar"));
25992592
drop(f);
26002593
let dir = check!(Dir::open(tmpdir.path()));
26012594
check!(dir.rename("foo.txt", &dir, "baz.txt"));

0 commit comments

Comments
 (0)