Skip to content

Commit cc20ab1

Browse files
Rollup merge of #43136 - jgallag88:bufWriterDocs, r=steveklabnik
Add warning to BufWriter documentation When using `BufWriter`, it is very easy to unintentionally ignore errors, because errors which occur when flushing buffered data when the `BufWriter` is dropped are ignored. This has been noted in a couple places: #32677, #37045. There has been some discussion about how to fix this problem in #32677, but no solution seems likely to land in the near future. For now, anyone who wishes to have robust error handling must remember to manually call `flush()` on a `BufWriter` before it is dropped. Until a permanent fix is in place, it seems worthwhile to add a warning to that effect to the documentation.
2 parents 6aeb0f0 + c6b280e commit cc20ab1

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/libstd/io/buffered.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ impl<R: Seek> Seek for BufReader<R> {
276276
/// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying
277277
/// writer in large, infrequent batches.
278278
///
279-
/// The buffer will be written out when the writer is dropped.
279+
/// When the `BufWriter` is dropped, the contents of its buffer will be written
280+
/// out. However, any errors that happen in the process of flushing the buffer
281+
/// when the writer is dropped will be ignored. Code that wishes to handle such
282+
/// errors must manually call [`flush`] before the writer is dropped.
280283
///
281284
/// # Examples
282285
///
@@ -316,6 +319,7 @@ impl<R: Seek> Seek for BufReader<R> {
316319
/// [`Write`]: ../../std/io/trait.Write.html
317320
/// [`Tcpstream::write`]: ../../std/net/struct.TcpStream.html#method.write
318321
/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
322+
/// [`flush`]: #method.flush
319323
#[stable(feature = "rust1", since = "1.0.0")]
320324
pub struct BufWriter<W: Write> {
321325
inner: Option<W>,

0 commit comments

Comments
 (0)