@@ -252,14 +252,49 @@ impl<R: Seek> Seek for BufReader<R> {
252
252
}
253
253
}
254
254
255
- /// Wraps a Writer and buffers output to it .
255
+ /// Wraps a writer and buffers its output .
256
256
///
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.
261
261
///
262
262
/// 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.
263
298
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
264
299
pub struct BufWriter < W : Write > {
265
300
inner : Option < W > ,
@@ -275,12 +310,33 @@ pub struct IntoInnerError<W>(W, Error);
275
310
276
311
impl < W : Write > BufWriter < W > {
277
312
/// 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
+ /// ```
278
322
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
279
323
pub fn new ( inner : W ) -> BufWriter < W > {
280
324
BufWriter :: with_capacity ( DEFAULT_BUF_SIZE , inner)
281
325
}
282
326
283
327
/// 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
+ /// ```
284
340
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
285
341
pub fn with_capacity ( cap : usize , inner : W ) -> BufWriter < W > {
286
342
BufWriter {
@@ -313,6 +369,18 @@ impl<W: Write> BufWriter<W> {
313
369
}
314
370
315
371
/// 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
+ /// ```
316
384
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
317
385
pub fn get_ref ( & self ) -> & W { self . inner . as_ref ( ) . unwrap ( ) }
318
386
@@ -321,12 +389,37 @@ impl<W: Write> BufWriter<W> {
321
389
/// # Warning
322
390
///
323
391
/// 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
+ /// ```
324
405
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
325
406
pub fn get_mut ( & mut self ) -> & mut W { self . inner . as_mut ( ) . unwrap ( ) }
326
407
327
408
/// Unwraps this `BufWriter`, returning the underlying writer.
328
409
///
329
410
/// 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
+ /// ```
330
423
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
331
424
pub fn into_inner ( mut self ) -> Result < W , IntoInnerError < BufWriter < W > > > {
332
425
match self . flush_buf ( ) {
0 commit comments