Skip to content

Commit 0f30ab8

Browse files
boatsStjepan Glavina
boats
authored and
Stjepan Glavina
committed
Fix the docs and Debug output of BufWriter. (#588)
The BufWriter docs inaccurately stated that it flushes on drop, which it does not do. This PR changes the docs, as well as the example, to highlight that the user must explicitly flush a bufwriter. There were also two places where the BufWriter code referred to it as a BufReader: in the link to the std docs, and in the Debug output. Those have also been fixed.
1 parent 850b8ae commit 0f30ab8

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Diff for: src/io/buf_writer.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ pin_project! {
2222
/// times. It also provides no advantage when writing to a destination that is
2323
/// in memory, like a `Vec<u8>`.
2424
///
25-
/// When the `BufWriter` is dropped, the contents of its buffer will be written
26-
/// out. However, any errors that happen in the process of flushing the buffer
27-
/// when the writer is dropped will be ignored. Code that wishes to handle such
28-
/// errors must manually call [`flush`] before the writer is dropped.
25+
/// Unlike the `BufWriter` type in `std`, this type does not write out the
26+
/// contents of its buffer when it is dropped. Therefore, it is absolutely
27+
/// critical that users explicitly flush the buffer before dropping a
28+
/// `BufWriter`.
2929
///
30-
/// This type is an async version of [`std::io::BufReader`].
30+
/// This type is an async version of [`std::io::BufWriter`].
3131
///
32-
/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
32+
/// [`std::io::BufWriter`]: https://doc.rust-lang.org/std/io/struct.BufWriter.html
3333
///
3434
/// # Examples
3535
///
@@ -61,10 +61,13 @@ pin_project! {
6161
/// use async_std::prelude::*;
6262
///
6363
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
64+
///
6465
/// for i in 0..10 {
6566
/// let arr = [i+1];
6667
/// stream.write(&arr).await?;
6768
/// };
69+
///
70+
/// stream.flush().await?;
6871
/// #
6972
/// # Ok(()) }) }
7073
/// ```
@@ -325,7 +328,7 @@ impl<W: Write> Write for BufWriter<W> {
325328

326329
impl<W: Write + fmt::Debug> fmt::Debug for BufWriter<W> {
327330
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328-
f.debug_struct("BufReader")
331+
f.debug_struct("BufWriter")
329332
.field("writer", &self.inner)
330333
.field("buf", &self.buf)
331334
.finish()

0 commit comments

Comments
 (0)