Skip to content

Commit 064b704

Browse files
authored
Rollup merge of #63410 - johnterickson:master, r=joshtriplett
Update BufWriter example to include call to flush() I was playing with a writing a Huffman encoder/decoder and was getting weird corruptions and truncations. I finally realized it was was because `BufWriter` was swallowing write errors 😬. I've found Rust to generally be explicit and err on the safe side, so I definitely found this unintuitive and not "rustic". https://twitter.com/johnterickson/status/1159514988123312128
2 parents 59cc53e + 1b94610 commit 064b704

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/libstd/io/buffered.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ use crate::io::{self, Initializer, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom,
99
IoSliceMut};
1010
use crate::memchr;
1111

12-
/// The `BufReader` struct adds buffering to any reader.
12+
/// The `BufReader<R>` struct adds buffering to any reader.
1313
///
1414
/// It can be excessively inefficient to work directly with a [`Read`] instance.
1515
/// For example, every call to [`read`][`TcpStream::read`] on [`TcpStream`]
16-
/// results in a system call. A `BufReader` performs large, infrequent reads on
16+
/// results in a system call. A `BufReader<R>` performs large, infrequent reads on
1717
/// the underlying [`Read`] and maintains an in-memory buffer of the results.
1818
///
19-
/// `BufReader` can improve the speed of programs that make *small* and
19+
/// `BufReader<R>` can improve the speed of programs that make *small* and
2020
/// *repeated* read calls to the same file or network socket. It does not
2121
/// help when reading very large amounts at once, or reading just one or a few
2222
/// times. It also provides no advantage when reading from a source that is
2323
/// already in memory, like a `Vec<u8>`.
2424
///
25-
/// When the `BufReader` is dropped, the contents of its buffer will be
26-
/// discarded. Creating multiple instances of a `BufReader` on the same
25+
/// When the `BufReader<R>` is dropped, the contents of its buffer will be
26+
/// discarded. Creating multiple instances of a `BufReader<R>` on the same
2727
/// stream can cause data loss.
2828
///
2929
/// [`Read`]: ../../std/io/trait.Read.html
@@ -56,7 +56,7 @@ pub struct BufReader<R> {
5656
}
5757

5858
impl<R: Read> BufReader<R> {
59-
/// Creates a new `BufReader` with a default buffer capacity. The default is currently 8 KB,
59+
/// Creates a new `BufReader<R>` with a default buffer capacity. The default is currently 8 KB,
6060
/// but may change in the future.
6161
///
6262
/// # Examples
@@ -76,7 +76,7 @@ impl<R: Read> BufReader<R> {
7676
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
7777
}
7878

79-
/// Creates a new `BufReader` with the specified buffer capacity.
79+
/// Creates a new `BufReader<R>` with the specified buffer capacity.
8080
///
8181
/// # Examples
8282
///
@@ -177,7 +177,7 @@ impl<R> BufReader<R> {
177177
&self.buf[self.pos..self.cap]
178178
}
179179

180-
/// Unwraps this `BufReader`, returning the underlying reader.
180+
/// Unwraps this `BufReader<R>`, returning the underlying reader.
181181
///
182182
/// Note that any leftover data in the internal buffer is lost.
183183
///
@@ -304,7 +304,7 @@ impl<R: Seek> Seek for BufReader<R> {
304304
/// Seek to an offset, in bytes, in the underlying reader.
305305
///
306306
/// The position used for seeking with `SeekFrom::Current(_)` is the
307-
/// position the underlying reader would be at if the `BufReader` had no
307+
/// position the underlying reader would be at if the `BufReader<R>` had no
308308
/// internal buffer.
309309
///
310310
/// Seeking always discards the internal buffer, even if the seek position
@@ -355,19 +355,20 @@ impl<R: Seek> Seek for BufReader<R> {
355355
/// It can be excessively inefficient to work directly with something that
356356
/// implements [`Write`]. For example, every call to
357357
/// [`write`][`TcpStream::write`] on [`TcpStream`] results in a system call. A
358-
/// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying
358+
/// `BufWriter<W>` keeps an in-memory buffer of data and writes it to an underlying
359359
/// writer in large, infrequent batches.
360360
///
361-
/// `BufWriter` can improve the speed of programs that make *small* and
361+
/// `BufWriter<W>` can improve the speed of programs that make *small* and
362362
/// *repeated* write calls to the same file or network socket. It does not
363363
/// help when writing very large amounts at once, or writing just one or a few
364364
/// times. It also provides no advantage when writing to a destination that is
365365
/// in memory, like a `Vec<u8>`.
366366
///
367-
/// When the `BufWriter` is dropped, the contents of its buffer will be written
368-
/// out. However, any errors that happen in the process of flushing the buffer
369-
/// when the writer is dropped will be ignored. Code that wishes to handle such
370-
/// errors must manually call [`flush`] before the writer is dropped.
367+
/// It is critical to call [`flush`] before `BufWriter<W>` is dropped. Though
368+
/// dropping will attempt to flush the the contents of the buffer, any errors
369+
/// that happen in the process of dropping will be ignored. Calling ['flush']
370+
/// ensures that the buffer is empty and thus dropping will not even attempt
371+
/// file operations.
371372
///
372373
/// # Examples
373374
///
@@ -386,7 +387,7 @@ impl<R: Seek> Seek for BufReader<R> {
386387
///
387388
/// Because we're not buffering, we write each one in turn, incurring the
388389
/// overhead of a system call per byte written. We can fix this with a
389-
/// `BufWriter`:
390+
/// `BufWriter<W>`:
390391
///
391392
/// ```no_run
392393
/// use std::io::prelude::*;
@@ -398,11 +399,12 @@ impl<R: Seek> Seek for BufReader<R> {
398399
/// for i in 0..10 {
399400
/// stream.write(&[i+1]).unwrap();
400401
/// }
402+
/// stream.flush().unwrap();
401403
/// ```
402404
///
403-
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
404-
/// together by the buffer, and will all be written out in one system call when
405-
/// the `stream` is dropped.
405+
/// By wrapping the stream with a `BufWriter<W>`, these ten writes are all grouped
406+
/// together by the buffer and will all be written out in one system call when
407+
/// the `stream` is flushed.
406408
///
407409
/// [`Write`]: ../../std/io/trait.Write.html
408410
/// [`TcpStream::write`]: ../../std/net/struct.TcpStream.html#method.write
@@ -447,7 +449,7 @@ pub struct BufWriter<W: Write> {
447449
pub struct IntoInnerError<W>(W, Error);
448450

449451
impl<W: Write> BufWriter<W> {
450-
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
452+
/// Creates a new `BufWriter<W>` with a default buffer capacity. The default is currently 8 KB,
451453
/// but may change in the future.
452454
///
453455
/// # Examples
@@ -463,7 +465,7 @@ impl<W: Write> BufWriter<W> {
463465
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
464466
}
465467

466-
/// Creates a new `BufWriter` with the specified buffer capacity.
468+
/// Creates a new `BufWriter<W>` with the specified buffer capacity.
467469
///
468470
/// # Examples
469471
///
@@ -564,7 +566,7 @@ impl<W: Write> BufWriter<W> {
564566
&self.buf
565567
}
566568

567-
/// Unwraps this `BufWriter`, returning the underlying writer.
569+
/// Unwraps this `BufWriter<W>`, returning the underlying writer.
568570
///
569571
/// The buffer is written out before returning the writer.
570572
///

0 commit comments

Comments
 (0)