@@ -417,17 +417,74 @@ impl<W> fmt::Display for IntoInnerError<W> {
417417 }
418418}
419419
420- /// Wraps a Writer and buffers output to it, flushing whenever a newline
420+ /// Wraps a writer and buffers output to it, flushing whenever a newline
421421/// (`0x0a`, `'\n'`) is detected.
422422///
423- /// The buffer will be written out when the writer is dropped.
423+ /// The [`BufWriter`][bufwriter] struct wraps a writer and buffers its output.
424+ /// But it only does this batched write when it goes out of scope, or when the
425+ /// internal buffer is full. Sometimes, you'd prefer to write each line as it's
426+ /// completed, rather than the entire buffer at once. Enter `LineWriter`. It
427+ /// does exactly that.
428+ ///
429+ /// [bufwriter]: struct.BufWriter.html
430+ ///
431+ /// If there's still a partial line in the buffer when the `LineWriter` is
432+ /// dropped, it will flush those contents.
433+ ///
434+ /// # Examples
435+ ///
436+ /// We can use `LineWriter` to write one line at a time, significantly
437+ /// reducing the number of actual writes to the file.
438+ ///
439+ /// ```
440+ /// use std::fs::File;
441+ /// use std::io::prelude::*;
442+ /// use std::io::LineWriter;
443+ ///
444+ /// # fn foo() -> std::io::Result<()> {
445+ /// let road_not_taken = b"I shall be telling this with a sigh
446+ /// Somewhere ages and ages hence:
447+ /// Two roads diverged in a wood, and I -
448+ /// I took the one less traveled by,
449+ /// And that has made all the difference.";
450+ ///
451+ /// let file = try!(File::create("poem.txt"));
452+ /// let mut file = LineWriter::new(file);
453+ ///
454+ /// for &byte in road_not_taken.iter() {
455+ /// file.write(&[byte]).unwrap();
456+ /// }
457+ ///
458+ /// // let's check we did the right thing.
459+ /// let mut file = try!(File::open("poem.txt"));
460+ /// let mut contents = String::new();
461+ ///
462+ /// try!(file.read_to_string(&mut contents));
463+ ///
464+ /// assert_eq!(contents.as_bytes(), &road_not_taken[..]);
465+ /// # Ok(())
466+ /// # }
467+ /// ```
424468#[ stable( feature = "rust1" , since = "1.0.0" ) ]
425469pub struct LineWriter < W : Write > {
426470 inner : BufWriter < W > ,
427471}
428472
429473impl < W : Write > LineWriter < W > {
430- /// Creates a new `LineWriter`
474+ /// Creates a new `LineWriter`.
475+ ///
476+ /// # Examples
477+ ///
478+ /// ```
479+ /// use std::fs::File;
480+ /// use std::io::LineWriter;
481+ ///
482+ /// # fn foo() -> std::io::Result<()> {
483+ /// let file = try!(File::create("poem.txt"));
484+ /// let file = LineWriter::new(file);
485+ /// # Ok(())
486+ /// # }
487+ /// ```
431488 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
432489 pub fn new ( inner : W ) -> LineWriter < W > {
433490 // Lines typically aren't that long, don't use a giant buffer
@@ -436,25 +493,86 @@ impl<W: Write> LineWriter<W> {
436493
437494 /// Creates a new `LineWriter` with a specified capacity for the internal
438495 /// buffer.
496+ ///
497+ /// # Examples
498+ ///
499+ /// ```
500+ /// use std::fs::File;
501+ /// use std::io::LineWriter;
502+ ///
503+ /// # fn foo() -> std::io::Result<()> {
504+ /// let file = try!(File::create("poem.txt"));
505+ /// let file = LineWriter::with_capacity(100, file);
506+ /// # Ok(())
507+ /// # }
508+ /// ```
439509 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
440510 pub fn with_capacity ( cap : usize , inner : W ) -> LineWriter < W > {
441511 LineWriter { inner : BufWriter :: with_capacity ( cap, inner) }
442512 }
443513
444514 /// Gets a reference to the underlying writer.
515+ ///
516+ /// # Examples
517+ ///
518+ /// ```
519+ /// use std::fs::File;
520+ /// use std::io::LineWriter;
521+ ///
522+ /// # fn foo() -> std::io::Result<()> {
523+ /// let file = try!(File::create("poem.txt"));
524+ /// let file = LineWriter::new(file);
525+ ///
526+ /// let reference = file.get_ref();
527+ /// # Ok(())
528+ /// # }
529+ /// ```
445530 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
446531 pub fn get_ref ( & self ) -> & W { self . inner . get_ref ( ) }
447532
448533 /// Gets a mutable reference to the underlying writer.
449534 ///
450535 /// Caution must be taken when calling methods on the mutable reference
451536 /// returned as extra writes could corrupt the output stream.
537+ ///
538+ /// # Examples
539+ ///
540+ /// ```
541+ /// use std::fs::File;
542+ /// use std::io::prelude::*;
543+ /// use std::io::LineWriter;
544+ ///
545+ /// # fn foo() -> std::io::Result<()> {
546+ /// let file = try!(File::create("poem.txt"));
547+ /// let mut file = LineWriter::new(file);
548+ ///
549+ /// // we can use reference just like file
550+ /// let reference = file.get_mut();
551+ /// # Ok(())
552+ /// # }
553+ /// ```
452554 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
453555 pub fn get_mut ( & mut self ) -> & mut W { self . inner . get_mut ( ) }
454556
455557 /// Unwraps this `LineWriter`, returning the underlying writer.
456558 ///
457559 /// The internal buffer is written out before returning the writer.
560+ ///
561+ /// # Examples
562+ ///
563+ /// ```
564+ /// use std::fs::File;
565+ /// use std::io::LineWriter;
566+ ///
567+ /// # fn foo() -> std::io::Result<()> {
568+ /// let file = try!(File::create("poem.txt"));
569+ ///
570+ /// let writer: LineWriter<File> = LineWriter::new(file);
571+ ///
572+ /// let file: File = try!(writer.into_inner());
573+ /// # Ok(())
574+ /// # }
575+ /// ```
458576 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
459577 pub fn into_inner ( self ) -> Result < W , IntoInnerError < LineWriter < W > > > {
460578 self . inner . into_inner ( ) . map_err ( |IntoInnerError ( buf, e) | {
0 commit comments