Skip to content

Commit ce5406e

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

File tree

1 file changed

+121
-3
lines changed

1 file changed

+121
-3
lines changed

src/libstd/io/buffered.rs

+121-3
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,74 @@ impl<W> fmt::Display for IntoInnerError<W> {
510510
}
511511
}
512512

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
514514
/// (`0x0a`, `'\n'`) is detected.
515515
///
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+
/// ```
517561
#[stable(feature = "rust1", since = "1.0.0")]
518562
pub struct LineWriter<W: Write> {
519563
inner: BufWriter<W>,
520564
}
521565

522566
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+
/// ```
524581
#[stable(feature = "rust1", since = "1.0.0")]
525582
pub fn new(inner: W) -> LineWriter<W> {
526583
// Lines typically aren't that long, don't use a giant buffer
@@ -529,25 +586,86 @@ impl<W: Write> LineWriter<W> {
529586

530587
/// Creates a new `LineWriter` with a specified capacity for the internal
531588
/// 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+
/// ```
532602
#[stable(feature = "rust1", since = "1.0.0")]
533603
pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
534604
LineWriter { inner: BufWriter::with_capacity(cap, inner) }
535605
}
536606

537607
/// 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+
/// ```
538623
#[stable(feature = "rust1", since = "1.0.0")]
539624
pub fn get_ref(&self) -> &W { self.inner.get_ref() }
540625

541626
/// Gets a mutable reference to the underlying writer.
542627
///
543628
/// Caution must be taken when calling methods on the mutable reference
544629
/// 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+
/// ```
545647
#[stable(feature = "rust1", since = "1.0.0")]
546648
pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
547649

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

0 commit comments

Comments
 (0)