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