@@ -9,21 +9,21 @@ use crate::io::{self, Initializer, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom,
9
9
IoSliceMut } ;
10
10
use crate :: memchr;
11
11
12
- /// The `BufReader` struct adds buffering to any reader.
12
+ /// The `BufReader<R> ` struct adds buffering to any reader.
13
13
///
14
14
/// It can be excessively inefficient to work directly with a [`Read`] instance.
15
15
/// 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
17
17
/// the underlying [`Read`] and maintains an in-memory buffer of the results.
18
18
///
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
20
20
/// *repeated* read calls to the same file or network socket. It does not
21
21
/// help when reading very large amounts at once, or reading just one or a few
22
22
/// times. It also provides no advantage when reading from a source that is
23
23
/// already in memory, like a `Vec<u8>`.
24
24
///
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
27
27
/// stream can cause data loss.
28
28
///
29
29
/// [`Read`]: ../../std/io/trait.Read.html
@@ -56,7 +56,7 @@ pub struct BufReader<R> {
56
56
}
57
57
58
58
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,
60
60
/// but may change in the future.
61
61
///
62
62
/// # Examples
@@ -76,7 +76,7 @@ impl<R: Read> BufReader<R> {
76
76
BufReader :: with_capacity ( DEFAULT_BUF_SIZE , inner)
77
77
}
78
78
79
- /// Creates a new `BufReader` with the specified buffer capacity.
79
+ /// Creates a new `BufReader<R> ` with the specified buffer capacity.
80
80
///
81
81
/// # Examples
82
82
///
@@ -177,7 +177,7 @@ impl<R> BufReader<R> {
177
177
& self . buf [ self . pos ..self . cap ]
178
178
}
179
179
180
- /// Unwraps this `BufReader`, returning the underlying reader.
180
+ /// Unwraps this `BufReader<R> `, returning the underlying reader.
181
181
///
182
182
/// Note that any leftover data in the internal buffer is lost.
183
183
///
@@ -304,7 +304,7 @@ impl<R: Seek> Seek for BufReader<R> {
304
304
/// Seek to an offset, in bytes, in the underlying reader.
305
305
///
306
306
/// 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
308
308
/// internal buffer.
309
309
///
310
310
/// Seeking always discards the internal buffer, even if the seek position
@@ -355,19 +355,20 @@ impl<R: Seek> Seek for BufReader<R> {
355
355
/// It can be excessively inefficient to work directly with something that
356
356
/// implements [`Write`]. For example, every call to
357
357
/// [`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
359
359
/// writer in large, infrequent batches.
360
360
///
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
362
362
/// *repeated* write calls to the same file or network socket. It does not
363
363
/// help when writing very large amounts at once, or writing just one or a few
364
364
/// times. It also provides no advantage when writing to a destination that is
365
365
/// in memory, like a `Vec<u8>`.
366
366
///
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.
371
372
///
372
373
/// # Examples
373
374
///
@@ -386,7 +387,7 @@ impl<R: Seek> Seek for BufReader<R> {
386
387
///
387
388
/// Because we're not buffering, we write each one in turn, incurring the
388
389
/// overhead of a system call per byte written. We can fix this with a
389
- /// `BufWriter`:
390
+ /// `BufWriter<W> `:
390
391
///
391
392
/// ```no_run
392
393
/// use std::io::prelude::*;
@@ -398,11 +399,12 @@ impl<R: Seek> Seek for BufReader<R> {
398
399
/// for i in 0..10 {
399
400
/// stream.write(&[i+1]).unwrap();
400
401
/// }
402
+ /// stream.flush().unwrap();
401
403
/// ```
402
404
///
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 .
406
408
///
407
409
/// [`Write`]: ../../std/io/trait.Write.html
408
410
/// [`TcpStream::write`]: ../../std/net/struct.TcpStream.html#method.write
@@ -447,7 +449,7 @@ pub struct BufWriter<W: Write> {
447
449
pub struct IntoInnerError < W > ( W , Error ) ;
448
450
449
451
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,
451
453
/// but may change in the future.
452
454
///
453
455
/// # Examples
@@ -463,7 +465,7 @@ impl<W: Write> BufWriter<W> {
463
465
BufWriter :: with_capacity ( DEFAULT_BUF_SIZE , inner)
464
466
}
465
467
466
- /// Creates a new `BufWriter` with the specified buffer capacity.
468
+ /// Creates a new `BufWriter<W> ` with the specified buffer capacity.
467
469
///
468
470
/// # Examples
469
471
///
@@ -564,7 +566,7 @@ impl<W: Write> BufWriter<W> {
564
566
& self . buf
565
567
}
566
568
567
- /// Unwraps this `BufWriter`, returning the underlying writer.
569
+ /// Unwraps this `BufWriter<W> `, returning the underlying writer.
568
570
///
569
571
/// The buffer is written out before returning the writer.
570
572
///
0 commit comments