@@ -509,17 +509,74 @@ impl<W> fmt::Display for IntoInnerError<W> {
509509 }
510510}
511511
512- /// Wraps a Writer and buffers output to it, flushing whenever a newline
512+ /// Wraps a writer and buffers output to it, flushing whenever a newline
513513/// (`0x0a`, `'\n'`) is detected.
514514///
515- /// The buffer will be written out when the writer is dropped.
515+ /// The [`BufWriter`][bufwriter] struct wraps a writer and buffers its output.
516+ /// But it only does this batched write when it goes out of scope, or when the
517+ /// internal buffer is full. Sometimes, you'd prefer to write each line as it's
518+ /// completed, rather than the entire buffer at once. Enter `LineWriter`. It
519+ /// does exactly that.
520+ ///
521+ /// [bufwriter]: struct.BufWriter.html
522+ ///
523+ /// If there's still a partial line in the buffer when the `LineWriter` is
524+ /// dropped, it will flush those contents.
525+ ///
526+ /// # Examples
527+ ///
528+ /// We can use `LineWriter` to write one line at a time, significantly
529+ /// reducing the number of actual writes to the file.
530+ ///
531+ /// ```
532+ /// use std::fs::File;
533+ /// use std::io::prelude::*;
534+ /// use std::io::LineWriter;
535+ ///
536+ /// # fn foo() -> std::io::Result<()> {
537+ /// let road_not_taken = b"I shall be telling this with a sigh
538+ /// Somewhere ages and ages hence:
539+ /// Two roads diverged in a wood, and I -
540+ /// I took the one less traveled by,
541+ /// And that has made all the difference.";
542+ ///
543+ /// let file = try!(File::create("poem.txt"));
544+ /// let mut file = LineWriter::new(file);
545+ ///
546+ /// for &byte in road_not_taken.iter() {
547+ /// file.write(&[byte]).unwrap();
548+ /// }
549+ ///
550+ /// // let's check we did the right thing.
551+ /// let mut file = try!(File::open("poem.txt"));
552+ /// let mut contents = String::new();
553+ ///
554+ /// try!(file.read_to_string(&mut contents));
555+ ///
556+ /// assert_eq!(contents.as_bytes(), &road_not_taken[..]);
557+ /// # Ok(())
558+ /// # }
559+ /// ```
516560#[ stable( feature = "rust1" , since = "1.0.0" ) ]
517561pub struct LineWriter < W : Write > {
518562 inner : BufWriter < W > ,
519563}
520564
521565impl < W : Write > LineWriter < W > {
522- /// Creates a new `LineWriter`
566+ /// Creates a new `LineWriter`.
567+ ///
568+ /// # Examples
569+ ///
570+ /// ```
571+ /// use std::fs::File;
572+ /// use std::io::LineWriter;
573+ ///
574+ /// # fn foo() -> std::io::Result<()> {
575+ /// let file = try!(File::create("poem.txt"));
576+ /// let file = LineWriter::new(file);
577+ /// # Ok(())
578+ /// # }
579+ /// ```
523580 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
524581 pub fn new ( inner : W ) -> LineWriter < W > {
525582 // Lines typically aren't that long, don't use a giant buffer
@@ -528,25 +585,85 @@ impl<W: Write> LineWriter<W> {
528585
529586 /// Creates a new `LineWriter` with a specified capacity for the internal
530587 /// buffer.
588+ ///
589+ /// # Examples
590+ ///
591+ /// ```
592+ /// use std::fs::File;
593+ /// use std::io::LineWriter;
594+ ///
595+ /// # fn foo() -> std::io::Result<()> {
596+ /// let file = try!(File::create("poem.txt"));
597+ /// let file = LineWriter::with_capacity(100, file);
598+ /// # Ok(())
599+ /// # }
600+ /// ```
531601 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
532602 pub fn with_capacity ( cap : usize , inner : W ) -> LineWriter < W > {
533603 LineWriter { inner : BufWriter :: with_capacity ( cap, inner) }
534604 }
535605
536606 /// Gets a reference to the underlying writer.
607+ ///
608+ /// # Examples
609+ ///
610+ /// ```
611+ /// use std::fs::File;
612+ /// use std::io::LineWriter;
613+ ///
614+ /// # fn foo() -> std::io::Result<()> {
615+ /// let file = try!(File::create("poem.txt"));
616+ /// let file = LineWriter::new(file);
617+ ///
618+ /// let reference = file.get_ref();
619+ /// # Ok(())
620+ /// # }
621+ /// ```
537622 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
538623 pub fn get_ref ( & self ) -> & W { self . inner . get_ref ( ) }
539624
540625 /// Gets a mutable reference to the underlying writer.
541626 ///
542627 /// Caution must be taken when calling methods on the mutable reference
543628 /// returned as extra writes could corrupt the output stream.
629+ ///
630+ /// # Examples
631+ ///
632+ /// ```
633+ /// use std::fs::File;
634+ /// use std::io::LineWriter;
635+ ///
636+ /// # fn foo() -> std::io::Result<()> {
637+ /// let file = try!(File::create("poem.txt"));
638+ /// let mut file = LineWriter::new(file);
639+ ///
640+ /// // we can use reference just like file
641+ /// let reference = file.get_mut();
642+ /// # Ok(())
643+ /// # }
644+ /// ```
544645 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
545646 pub fn get_mut ( & mut self ) -> & mut W { self . inner . get_mut ( ) }
546647
547648 /// Unwraps this `LineWriter`, returning the underlying writer.
548649 ///
549650 /// The internal buffer is written out before returning the writer.
651+ ///
652+ /// # Examples
653+ ///
654+ /// ```
655+ /// use std::fs::File;
656+ /// use std::io::LineWriter;
657+ ///
658+ /// # fn foo() -> std::io::Result<()> {
659+ /// let file = try!(File::create("poem.txt"));
660+ ///
661+ /// let writer: LineWriter<File> = LineWriter::new(file);
662+ ///
663+ /// let file: File = try!(writer.into_inner());
664+ /// # Ok(())
665+ /// # }
666+ /// ```
550667 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
551668 pub fn into_inner ( self ) -> Result < W , IntoInnerError < LineWriter < W > > > {
552669 self . inner . into_inner ( ) . map_err ( |IntoInnerError ( buf, e) | {
0 commit comments