Skip to content

Commit 9f9e2dd

Browse files
committed
Rollup merge of rust-lang#27155 - steveklabnik:doc_std_io_buf_writer, r=alexcrichton
Mostly through adding examples. r? @alexcrichton I'm going to be doing a bunch of these today, but I figured I'd keep it one PR per struct, since the last 'all the things in one PR' ended up taking a week to actually land.
2 parents fcfce41 + eb39df0 commit 9f9e2dd

File tree

1 file changed

+98
-5
lines changed

1 file changed

+98
-5
lines changed

src/libstd/io/buffered.rs

+98-5
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,49 @@ impl<R: Seek> Seek for BufReader<R> {
252252
}
253253
}
254254

255-
/// Wraps a Writer and buffers output to it.
255+
/// Wraps a writer and buffers its output.
256256
///
257-
/// It can be excessively inefficient to work directly with a `Write`. For
258-
/// example, every call to `write` on `TcpStream` results in a system call. A
259-
/// `BufWriter` keeps an in memory buffer of data and writes it to the
260-
/// underlying `Write` in large, infrequent batches.
257+
/// It can be excessively inefficient to work directly with something that
258+
/// implements `Write`. For example, every call to `write` on `TcpStream`
259+
/// results in a system call. A `BufWriter` keeps an in-memory buffer of data
260+
/// and writes it to an underlying writer in large, infrequent batches.
261261
///
262262
/// The buffer will be written out when the writer is dropped.
263+
///
264+
/// # Examples
265+
///
266+
/// Let's write the numbers one through ten to a `TcpStream`:
267+
///
268+
/// ```no_run
269+
/// use std::io::prelude::*;
270+
/// use std::net::TcpStream;
271+
///
272+
/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
273+
///
274+
/// for i in 1..10 {
275+
/// stream.write(&[i]).unwrap();
276+
/// }
277+
/// ```
278+
///
279+
/// Because we're not buffering, we write each one in turn, incurring the
280+
/// overhead of a system call per byte written. We can fix this with a
281+
/// `BufWriter`:
282+
///
283+
/// ```no_run
284+
/// use std::io::prelude::*;
285+
/// use std::io::BufWriter;
286+
/// use std::net::TcpStream;
287+
///
288+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
289+
///
290+
/// for i in 1..10 {
291+
/// stream.write(&[i]).unwrap();
292+
/// }
293+
/// ```
294+
///
295+
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
296+
/// together by the buffer, and will all be written out in one system call when
297+
/// the `stream` is dropped.
263298
#[stable(feature = "rust1", since = "1.0.0")]
264299
pub struct BufWriter<W: Write> {
265300
inner: Option<W>,
@@ -275,12 +310,33 @@ pub struct IntoInnerError<W>(W, Error);
275310

276311
impl<W: Write> BufWriter<W> {
277312
/// Creates a new `BufWriter` with a default buffer capacity.
313+
///
314+
/// # Examples
315+
///
316+
/// ```no_run
317+
/// use std::io::BufWriter;
318+
/// use std::net::TcpStream;
319+
///
320+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
321+
/// ```
278322
#[stable(feature = "rust1", since = "1.0.0")]
279323
pub fn new(inner: W) -> BufWriter<W> {
280324
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
281325
}
282326

283327
/// Creates a new `BufWriter` with the specified buffer capacity.
328+
///
329+
/// # Examples
330+
///
331+
/// Creating a buffer with a buffer of a hundred bytes.
332+
///
333+
/// ```no_run
334+
/// use std::io::BufWriter;
335+
/// use std::net::TcpStream;
336+
///
337+
/// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
338+
/// let mut buffer = BufWriter::with_capacity(100, stream);
339+
/// ```
284340
#[stable(feature = "rust1", since = "1.0.0")]
285341
pub fn with_capacity(cap: usize, inner: W) -> BufWriter<W> {
286342
BufWriter {
@@ -313,6 +369,18 @@ impl<W: Write> BufWriter<W> {
313369
}
314370

315371
/// Gets a reference to the underlying writer.
372+
///
373+
/// # Examples
374+
///
375+
/// ```no_run
376+
/// use std::io::BufWriter;
377+
/// use std::net::TcpStream;
378+
///
379+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
380+
///
381+
/// // we can use reference just like buffer
382+
/// let reference = buffer.get_ref();
383+
/// ```
316384
#[stable(feature = "rust1", since = "1.0.0")]
317385
pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
318386

@@ -321,12 +389,37 @@ impl<W: Write> BufWriter<W> {
321389
/// # Warning
322390
///
323391
/// It is inadvisable to directly write to the underlying writer.
392+
///
393+
/// # Examples
394+
///
395+
/// ```no_run
396+
/// use std::io::prelude::*;
397+
/// use std::io::BufWriter;
398+
/// use std::net::TcpStream;
399+
///
400+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
401+
///
402+
/// // we can use reference just like buffer
403+
/// let reference = buffer.get_mut();
404+
/// ```
324405
#[stable(feature = "rust1", since = "1.0.0")]
325406
pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
326407

327408
/// Unwraps this `BufWriter`, returning the underlying writer.
328409
///
329410
/// The buffer is written out before returning the writer.
411+
///
412+
/// # Examples
413+
///
414+
/// ```no_run
415+
/// use std::io::BufWriter;
416+
/// use std::net::TcpStream;
417+
///
418+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
419+
///
420+
/// // unwrap the TcpStream and flush the buffer
421+
/// let stream = buffer.into_inner().unwrap();
422+
/// ```
330423
#[stable(feature = "rust1", since = "1.0.0")]
331424
pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
332425
match self.flush_buf() {

0 commit comments

Comments
 (0)