@@ -417,17 +417,74 @@ impl<W> fmt::Display for IntoInnerError<W> {
417
417
}
418
418
}
419
419
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
421
421
/// (`0x0a`, `'\n'`) is detected.
422
422
///
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
+ /// ```
424
468
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
425
469
pub struct LineWriter < W : Write > {
426
470
inner : BufWriter < W > ,
427
471
}
428
472
429
473
impl < 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
+ /// ```
431
488
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
432
489
pub fn new ( inner : W ) -> LineWriter < W > {
433
490
// Lines typically aren't that long, don't use a giant buffer
@@ -436,25 +493,85 @@ impl<W: Write> LineWriter<W> {
436
493
437
494
/// Creates a new `LineWriter` with a specified capacity for the internal
438
495
/// 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
+ /// ```
439
509
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
440
510
pub fn with_capacity ( cap : usize , inner : W ) -> LineWriter < W > {
441
511
LineWriter { inner : BufWriter :: with_capacity ( cap, inner) }
442
512
}
443
513
444
514
/// 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
+ /// ```
445
530
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
446
531
pub fn get_ref ( & self ) -> & W { self . inner . get_ref ( ) }
447
532
448
533
/// Gets a mutable reference to the underlying writer.
449
534
///
450
535
/// Caution must be taken when calling methods on the mutable reference
451
536
/// returned as extra writes could corrupt the output stream.
537
+ ///
538
+ /// # Examples
539
+ ///
540
+ /// ```
541
+ /// use std::fs::File;
542
+ /// use std::io::LineWriter;
543
+ ///
544
+ /// # fn foo() -> std::io::Result<()> {
545
+ /// let file = try!(File::create("poem.txt"));
546
+ /// let mut file = LineWriter::new(file);
547
+ ///
548
+ /// // we can use reference just like file
549
+ /// let reference = file.get_mut();
550
+ /// # Ok(())
551
+ /// # }
552
+ /// ```
452
553
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
453
554
pub fn get_mut ( & mut self ) -> & mut W { self . inner . get_mut ( ) }
454
555
455
556
/// Unwraps this `LineWriter`, returning the underlying writer.
456
557
///
457
558
/// The internal buffer is written out before returning the writer.
559
+ ///
560
+ /// # Examples
561
+ ///
562
+ /// ```
563
+ /// use std::fs::File;
564
+ /// use std::io::LineWriter;
565
+ ///
566
+ /// # fn foo() -> std::io::Result<()> {
567
+ /// let file = try!(File::create("poem.txt"));
568
+ ///
569
+ /// let writer: LineWriter<File> = LineWriter::new(file);
570
+ ///
571
+ /// let file: File = try!(writer.into_inner());
572
+ /// # Ok(())
573
+ /// # }
574
+ /// ```
458
575
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
459
576
pub fn into_inner ( self ) -> Result < W , IntoInnerError < LineWriter < W > > > {
460
577
self . inner . into_inner ( ) . map_err ( |IntoInnerError ( buf, e) | {
0 commit comments