Skip to content

Commit fe4a352

Browse files
committed
improve documentation, tests
1 parent 1fd54f6 commit fe4a352

2 files changed

Lines changed: 22 additions & 11 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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,18 +2526,19 @@ fn test_fs_set_times_nofollow() {
25262526
}
25272527

25282528
#[test]
2529+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25292530
fn test_dir_smoke_test() {
25302531
let tmpdir = tmpdir();
25312532
let dir = Dir::open(tmpdir.path());
25322533
check!(dir);
25332534
}
25342535

25352536
#[test]
2537+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25362538
fn test_dir_read_file() {
25372539
let tmpdir = tmpdir();
25382540
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2539-
check!(f.write(b"bar"));
2540-
check!(f.flush());
2541+
check!(f.write_all(b"bar"));
25412542
drop(f);
25422543
let dir = check!(Dir::open(tmpdir.path()));
25432544
let f = check!(dir.open_file("foo.txt"));
@@ -2549,8 +2550,7 @@ fn test_dir_read_file() {
25492550
}
25502551

25512552
#[test]
2552-
// FIXME: libc calls fail on miri
2553-
#[cfg(not(miri))]
2553+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25542554
fn test_dir_write_file() {
25552555
let tmpdir = tmpdir();
25562556
let dir = check!(Dir::open(tmpdir.path()));
@@ -2565,8 +2565,7 @@ fn test_dir_write_file() {
25652565
}
25662566

25672567
#[test]
2568-
// FIXME: libc calls fail on miri
2569-
#[cfg(not(miri))]
2568+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25702569
fn test_dir_remove_file() {
25712570
let tmpdir = tmpdir();
25722571
let mut f = check!(File::create(tmpdir.join("foo.txt")));
@@ -2579,13 +2578,11 @@ fn test_dir_remove_file() {
25792578
}
25802579

25812580
#[test]
2582-
// FIXME: libc calls fail on miri
2583-
#[cfg(not(miri))]
2581+
#[cfg_attr(miri, ignore)] // FIXME: Miri does not support directory handles
25842582
fn test_dir_rename_file() {
25852583
let tmpdir = tmpdir();
25862584
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2587-
check!(f.write(b"bar"));
2588-
check!(f.flush());
2585+
check!(f.write_all(b"bar"));
25892586
drop(f);
25902587
let dir = check!(Dir::open(tmpdir.path()));
25912588
check!(dir.rename("foo.txt", &dir, "baz.txt"));

0 commit comments

Comments
 (0)