Skip to content

Commit 18201d7

Browse files
committed
More docs for std::io::BufReader
1 parent cdcce3b commit 18201d7

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

src/libstd/io/buffered.rs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use fmt;
2020
use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom};
2121
use ptr;
2222

23-
/// Wraps a `Read` and buffers input from it.
23+
/// The `BufReader` struct adds buffering to any reader.
2424
///
2525
/// It can be excessively inefficient to work directly with a `Read` instance.
2626
/// For example, every call to `read` on `TcpStream` results in a system call.
@@ -29,7 +29,7 @@ use ptr;
2929
///
3030
/// # Examples
3131
///
32-
/// ```no_run
32+
/// ```
3333
/// use std::io::prelude::*;
3434
/// use std::io::BufReader;
3535
/// use std::fs::File;
@@ -54,12 +54,40 @@ pub struct BufReader<R> {
5454

5555
impl<R: Read> BufReader<R> {
5656
/// Creates a new `BufReader` with a default buffer capacity.
57+
///
58+
/// # Examples
59+
///
60+
/// ```
61+
/// use std::io::BufReader;
62+
/// use std::fs::File;
63+
///
64+
/// # fn foo() -> std::io::Result<()> {
65+
/// let mut f = try!(File::open("log.txt"));
66+
/// let mut reader = BufReader::new(f);
67+
/// # Ok(())
68+
/// # }
69+
/// ```
5770
#[stable(feature = "rust1", since = "1.0.0")]
5871
pub fn new(inner: R) -> BufReader<R> {
5972
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
6073
}
6174

6275
/// Creates a new `BufReader` with the specified buffer capacity.
76+
///
77+
/// # Examples
78+
///
79+
/// Creating a buffer with ten bytes of capacity:
80+
///
81+
/// ```
82+
/// use std::io::BufReader;
83+
/// use std::fs::File;
84+
///
85+
/// # fn foo() -> std::io::Result<()> {
86+
/// let mut f = try!(File::open("log.txt"));
87+
/// let mut reader = BufReader::with_capacity(10, f);
88+
/// # Ok(())
89+
/// # }
90+
/// ```
6391
#[stable(feature = "rust1", since = "1.0.0")]
6492
pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> {
6593
BufReader {
@@ -71,20 +99,65 @@ impl<R: Read> BufReader<R> {
7199
}
72100

73101
/// Gets a reference to the underlying reader.
102+
///
103+
/// It is inadvisable to directly read from the underlying reader.
104+
///
105+
/// # Examples
106+
///
107+
/// ```
108+
/// use std::io::BufReader;
109+
/// use std::fs::File;
110+
///
111+
/// # fn foo() -> std::io::Result<()> {
112+
/// let mut f1 = try!(File::open("log.txt"));
113+
/// let mut reader = BufReader::new(f1);
114+
///
115+
/// let f2 = reader.get_ref();
116+
/// # Ok(())
117+
/// # }
118+
/// ```
74119
#[stable(feature = "rust1", since = "1.0.0")]
75120
pub fn get_ref(&self) -> &R { &self.inner }
76121

77122
/// Gets a mutable reference to the underlying reader.
78123
///
79-
/// # Warning
80-
///
81124
/// It is inadvisable to directly read from the underlying reader.
125+
///
126+
/// # Examples
127+
///
128+
/// ```
129+
/// use std::io::BufReader;
130+
/// use std::fs::File;
131+
///
132+
/// # fn foo() -> std::io::Result<()> {
133+
/// let mut f1 = try!(File::open("log.txt"));
134+
/// let mut reader = BufReader::new(f1);
135+
///
136+
/// let f2 = reader.get_mut();
137+
/// # Ok(())
138+
/// # }
139+
/// ```
82140
#[stable(feature = "rust1", since = "1.0.0")]
83141
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
84142

85143
/// Unwraps this `BufReader`, returning the underlying reader.
86144
///
87145
/// Note that any leftover data in the internal buffer is lost.
146+
///
147+
/// # Examples
148+
///
149+
/// ```
150+
/// use std::io::BufReader;
151+
/// use std::fs::File;
152+
///
153+
/// # fn foo() -> std::io::Result<()> {
154+
/// let mut f1 = try!(File::open("log.txt"));
155+
/// let mut reader = BufReader::new(f1);
156+
///
157+
/// let f2 = reader.into_inner();
158+
/// # Ok(())
159+
/// # }
160+
/// ```
88161
#[stable(feature = "rust1", since = "1.0.0")]
89162
pub fn into_inner(self) -> R { self.inner }
90163
}

0 commit comments

Comments
 (0)