Skip to content

Commit 4fbd256

Browse files
authored
Unrolled build for rust-lang#119319
Rollup merge of rust-lang#119319 - chfogelman:buffered-file-doc, r=the8472 Document that File does not buffer reads/writes ...and refer to `BufReader`/`BufWriter`. This is a common source of efficiency issues in Rust programs written naively. Including this information with the `File` docs, and adding a link to the wrapper types, will help discoverability.
2 parents 1a47f5b + 5cbe41a commit 4fbd256

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

library/std/src/fs.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ use crate::time::SystemTime;
3131
/// on closing are ignored by the implementation of `Drop`. Use the method
3232
/// [`sync_all`] if these errors must be manually handled.
3333
///
34+
/// `File` does not buffer reads and writes. For efficiency, consider wrapping the
35+
/// file in a [`BufReader`] or [`BufWriter`] when performing many small [`read`]
36+
/// or [`write`] calls, unless unbuffered reads and writes are required.
37+
///
3438
/// # Examples
3539
///
3640
/// Creates a new file and write bytes to it (you can also use [`write()`]):
@@ -61,8 +65,7 @@ use crate::time::SystemTime;
6165
/// }
6266
/// ```
6367
///
64-
/// It can be more efficient to read the contents of a file with a buffered
65-
/// [`Read`]er. This can be accomplished with [`BufReader<R>`]:
68+
/// Using a buffered [`Read`]er:
6669
///
6770
/// ```no_run
6871
/// use std::fs::File;
@@ -93,8 +96,11 @@ use crate::time::SystemTime;
9396
/// perform synchronous I/O operations. Therefore the underlying file must not
9497
/// have been opened for asynchronous I/O (e.g. by using `FILE_FLAG_OVERLAPPED`).
9598
///
96-
/// [`BufReader<R>`]: io::BufReader
99+
/// [`BufReader`]: io::BufReader
100+
/// [`BufWriter`]: io::BufReader
97101
/// [`sync_all`]: File::sync_all
102+
/// [`write`]: File::write
103+
/// [`read`]: File::read
98104
#[stable(feature = "rust1", since = "1.0.0")]
99105
#[cfg_attr(not(test), rustc_diagnostic_item = "File")]
100106
pub struct File {

0 commit comments

Comments
 (0)