Skip to content

Commit e31ad95

Browse files
committed
Improve documentation for std::io::LineWriter
Beef up the struct docs, add examples for the methods.
1 parent 4e51763 commit e31ad95

File tree

1 file changed

+120
-3
lines changed

1 file changed

+120
-3
lines changed

src/libstd/io/buffered.rs

+120-3
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,74 @@ impl<W> fmt::Display for IntoInnerError<W> {
417417
}
418418
}
419419

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
421421
/// (`0x0a`, `'\n'`) is detected.
422422
///
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+
/// ```
424468
#[stable(feature = "rust1", since = "1.0.0")]
425469
pub struct LineWriter<W: Write> {
426470
inner: BufWriter<W>,
427471
}
428472

429473
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+
/// ```
431488
#[stable(feature = "rust1", since = "1.0.0")]
432489
pub fn new(inner: W) -> LineWriter<W> {
433490
// Lines typically aren't that long, don't use a giant buffer
@@ -436,25 +493,85 @@ impl<W: Write> LineWriter<W> {
436493

437494
/// Creates a new `LineWriter` with a specified capacity for the internal
438495
/// 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+
/// ```
439509
#[stable(feature = "rust1", since = "1.0.0")]
440510
pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
441511
LineWriter { inner: BufWriter::with_capacity(cap, inner) }
442512
}
443513

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

448533
/// Gets a mutable reference to the underlying writer.
449534
///
450535
/// Caution must be taken when calling methods on the mutable reference
451536
/// 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+
/// ```
452553
#[stable(feature = "rust1", since = "1.0.0")]
453554
pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
454555

455556
/// Unwraps this `LineWriter`, returning the underlying writer.
456557
///
457558
/// 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+
/// ```
458575
#[stable(feature = "rust1", since = "1.0.0")]
459576
pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
460577
self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {

0 commit comments

Comments
 (0)