@@ -509,17 +509,74 @@ impl<W> fmt::Display for IntoInnerError<W> {
509
509
}
510
510
}
511
511
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
513
513
/// (`0x0a`, `'\n'`) is detected.
514
514
///
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
+ /// ```
516
560
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
517
561
pub struct LineWriter < W : Write > {
518
562
inner : BufWriter < W > ,
519
563
}
520
564
521
565
impl < 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
+ /// ```
523
580
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
524
581
pub fn new ( inner : W ) -> LineWriter < W > {
525
582
// Lines typically aren't that long, don't use a giant buffer
@@ -528,25 +585,86 @@ impl<W: Write> LineWriter<W> {
528
585
529
586
/// Creates a new `LineWriter` with a specified capacity for the internal
530
587
/// 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
+ /// ```
531
601
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
532
602
pub fn with_capacity ( cap : usize , inner : W ) -> LineWriter < W > {
533
603
LineWriter { inner : BufWriter :: with_capacity ( cap, inner) }
534
604
}
535
605
536
606
/// 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
+ /// ```
537
622
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
538
623
pub fn get_ref ( & self ) -> & W { self . inner . get_ref ( ) }
539
624
540
625
/// Gets a mutable reference to the underlying writer.
541
626
///
542
627
/// Caution must be taken when calling methods on the mutable reference
543
628
/// returned as extra writes could corrupt the output stream.
629
+ ///
630
+ /// # Examples
631
+ ///
632
+ /// ```
633
+ /// use std::fs::File;
634
+ /// use std::io::prelude::*;
635
+ /// use std::io::LineWriter;
636
+ ///
637
+ /// # fn foo() -> std::io::Result<()> {
638
+ /// let file = try!(File::create("poem.txt"));
639
+ /// let mut file = LineWriter::new(file);
640
+ ///
641
+ /// // we can use reference just like file
642
+ /// let reference = file.get_mut();
643
+ /// # Ok(())
644
+ /// # }
645
+ /// ```
544
646
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
545
647
pub fn get_mut ( & mut self ) -> & mut W { self . inner . get_mut ( ) }
546
648
547
649
/// Unwraps this `LineWriter`, returning the underlying writer.
548
650
///
549
651
/// The internal buffer is written out before returning the writer.
652
+ ///
653
+ /// # Examples
654
+ ///
655
+ /// ```
656
+ /// use std::fs::File;
657
+ /// use std::io::LineWriter;
658
+ ///
659
+ /// # fn foo() -> std::io::Result<()> {
660
+ /// let file = try!(File::create("poem.txt"));
661
+ ///
662
+ /// let writer: LineWriter<File> = LineWriter::new(file);
663
+ ///
664
+ /// let file: File = try!(writer.into_inner());
665
+ /// # Ok(())
666
+ /// # }
667
+ /// ```
550
668
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
551
669
pub fn into_inner ( self ) -> Result < W , IntoInnerError < LineWriter < W > > > {
552
670
self . inner . into_inner ( ) . map_err ( |IntoInnerError ( buf, e) | {
0 commit comments