Skip to content

Commit 9132606

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

2 files changed

Lines changed: 17 additions & 5 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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,8 +2546,7 @@ fn test_dir_smoke_test() {
25462546
fn test_dir_read_file() {
25472547
let tmpdir = tmpdir();
25482548
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2549-
check!(f.write(b"bar"));
2550-
check!(f.flush());
2549+
check!(f.write_all(b"bar"));
25512550
drop(f);
25522551
let dir = check!(Dir::open(tmpdir.path()));
25532552
let f = check!(dir.open_file("foo.txt"));
@@ -2594,8 +2593,7 @@ fn test_dir_remove_file() {
25942593
fn test_dir_rename_file() {
25952594
let tmpdir = tmpdir();
25962595
let mut f = check!(File::create(tmpdir.join("foo.txt")));
2597-
check!(f.write(b"bar"));
2598-
check!(f.flush());
2596+
check!(f.write_all(b"bar"));
25992597
drop(f);
26002598
let dir = check!(Dir::open(tmpdir.path()));
26012599
check!(dir.rename("foo.txt", &dir, "baz.txt"));

0 commit comments

Comments
 (0)