@@ -20,7 +20,7 @@ use fmt;
20
20
use io:: { self , DEFAULT_BUF_SIZE , Error , ErrorKind , SeekFrom } ;
21
21
use ptr;
22
22
23
- /// Wraps a `Read` and buffers input from it .
23
+ /// The `BufReader` struct adds buffering to any reader .
24
24
///
25
25
/// It can be excessively inefficient to work directly with a `Read` instance.
26
26
/// For example, every call to `read` on `TcpStream` results in a system call.
@@ -29,7 +29,7 @@ use ptr;
29
29
///
30
30
/// # Examples
31
31
///
32
- /// ```no_run
32
+ /// ```
33
33
/// use std::io::prelude::*;
34
34
/// use std::io::BufReader;
35
35
/// use std::fs::File;
@@ -54,12 +54,40 @@ pub struct BufReader<R> {
54
54
55
55
impl < R : Read > BufReader < R > {
56
56
/// 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
+ /// ```
57
70
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
58
71
pub fn new ( inner : R ) -> BufReader < R > {
59
72
BufReader :: with_capacity ( DEFAULT_BUF_SIZE , inner)
60
73
}
61
74
62
75
/// 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
+ /// ```
63
91
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
64
92
pub fn with_capacity ( cap : usize , inner : R ) -> BufReader < R > {
65
93
BufReader {
@@ -71,20 +99,65 @@ impl<R: Read> BufReader<R> {
71
99
}
72
100
73
101
/// 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
+ /// ```
74
119
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
75
120
pub fn get_ref ( & self ) -> & R { & self . inner }
76
121
77
122
/// Gets a mutable reference to the underlying reader.
78
123
///
79
- /// # Warning
80
- ///
81
124
/// 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
+ /// ```
82
140
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
83
141
pub fn get_mut ( & mut self ) -> & mut R { & mut self . inner }
84
142
85
143
/// Unwraps this `BufReader`, returning the underlying reader.
86
144
///
87
145
/// 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
+ /// ```
88
161
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
89
162
pub fn into_inner ( self ) -> R { self . inner }
90
163
}
0 commit comments