Skip to content

Commit 3fc7be4

Browse files
committed
Rollup merge of #27166 - steveklabnik:doc_std_io_linewriter, r=alexcrichton
Beef up the struct docs, add examples for the methods. r? @alexcrichton
2 parents 2af8ea2 + 94b8f28 commit 3fc7be4

File tree

1 file changed

+121
-3
lines changed

1 file changed

+121
-3
lines changed

src/libstd/io/buffered.rs

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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")]
517561
pub struct LineWriter<W: Write> {
518562
inner: BufWriter<W>,
519563
}
520564

521565
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+
/// ```
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,86 @@ 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::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+
/// ```
544646
#[stable(feature = "rust1", since = "1.0.0")]
545647
pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
546648

547649
/// Unwraps this `LineWriter`, returning the underlying writer.
548650
///
549651
/// 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+
/// ```
550668
#[stable(feature = "rust1", since = "1.0.0")]
551669
pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
552670
self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {

0 commit comments

Comments
 (0)