@@ -16,8 +16,7 @@ use prelude::v1::*;
16
16
use io:: prelude:: * ;
17
17
18
18
use cmp;
19
- use error:: Error as StdError ;
20
- use error:: FromError ;
19
+ use error:: { self , FromError } ;
21
20
use fmt;
22
21
use io:: { self , Cursor , DEFAULT_BUF_SIZE , Error , ErrorKind } ;
23
22
use ptr;
@@ -28,18 +27,21 @@ use ptr;
28
27
/// For example, every call to `read` on `TcpStream` results in a system call.
29
28
/// A `BufReader` performs large, infrequent reads on the underlying `Read`
30
29
/// and maintains an in-memory buffer of the results.
30
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
31
31
pub struct BufReader < R > {
32
32
inner : R ,
33
33
buf : Cursor < Vec < u8 > > ,
34
34
}
35
35
36
36
impl < R : Read > BufReader < R > {
37
37
/// Creates a new `BufReader` with a default buffer capacity
38
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
38
39
pub fn new ( inner : R ) -> BufReader < R > {
39
40
BufReader :: with_capacity ( DEFAULT_BUF_SIZE , inner)
40
41
}
41
42
42
43
/// Creates a new `BufReader` with the specified buffer capacity
44
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
43
45
pub fn with_capacity ( cap : usize , inner : R ) -> BufReader < R > {
44
46
BufReader {
45
47
inner : inner,
@@ -48,21 +50,25 @@ impl<R: Read> BufReader<R> {
48
50
}
49
51
50
52
/// Gets a reference to the underlying reader.
53
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
51
54
pub fn get_ref ( & self ) -> & R { & self . inner }
52
55
53
56
/// Gets a mutable reference to the underlying reader.
54
57
///
55
58
/// # Warning
56
59
///
57
60
/// It is inadvisable to directly read from the underlying reader.
61
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
58
62
pub fn get_mut ( & mut self ) -> & mut R { & mut self . inner }
59
63
60
64
/// Unwraps this `BufReader`, returning the underlying reader.
61
65
///
62
66
/// Note that any leftover data in the internal buffer is lost.
67
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
63
68
pub fn into_inner ( self ) -> R { self . inner }
64
69
}
65
70
71
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
66
72
impl < R : Read > Read for BufReader < R > {
67
73
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
68
74
// If we don't have any buffered data and we're doing a massive read
@@ -77,6 +83,7 @@ impl<R: Read> Read for BufReader<R> {
77
83
}
78
84
}
79
85
86
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
80
87
impl < R : Read > BufRead for BufReader < R > {
81
88
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
82
89
// If we've reached the end of our internal buffer then we need to fetch
@@ -112,6 +119,7 @@ impl<R> fmt::Debug for BufReader<R> where R: fmt::Debug {
112
119
/// underlying `Write` in large, infrequent batches.
113
120
///
114
121
/// This writer will be flushed when it is dropped.
122
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
115
123
pub struct BufWriter < W > {
116
124
inner : Option < W > ,
117
125
buf : Vec < u8 > ,
@@ -120,15 +128,18 @@ pub struct BufWriter<W> {
120
128
/// An error returned by `into_inner` which indicates whether a flush error
121
129
/// happened or not.
122
130
#[ derive( Debug ) ]
131
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
123
132
pub struct IntoInnerError < W > ( W , Error ) ;
124
133
125
134
impl < W : Write > BufWriter < W > {
126
135
/// Creates a new `BufWriter` with a default buffer capacity
136
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
127
137
pub fn new ( inner : W ) -> BufWriter < W > {
128
138
BufWriter :: with_capacity ( DEFAULT_BUF_SIZE , inner)
129
139
}
130
140
131
141
/// Creates a new `BufWriter` with the specified buffer capacity
142
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
132
143
pub fn with_capacity ( cap : usize , inner : W ) -> BufWriter < W > {
133
144
BufWriter {
134
145
inner : Some ( inner) ,
@@ -165,18 +176,21 @@ impl<W: Write> BufWriter<W> {
165
176
}
166
177
167
178
/// Gets a reference to the underlying writer.
179
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
168
180
pub fn get_ref ( & self ) -> & W { self . inner . as_ref ( ) . unwrap ( ) }
169
181
170
182
/// Gets a mutable reference to the underlying write.
171
183
///
172
184
/// # Warning
173
185
///
174
186
/// It is inadvisable to directly read from the underlying writer.
187
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
175
188
pub fn get_mut ( & mut self ) -> & mut W { self . inner . as_mut ( ) . unwrap ( ) }
176
189
177
190
/// Unwraps this `BufWriter`, returning the underlying writer.
178
191
///
179
192
/// The buffer is flushed before returning the writer.
193
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
180
194
pub fn into_inner ( mut self ) -> Result < W , IntoInnerError < BufWriter < W > > > {
181
195
match self . flush_buf ( ) {
182
196
Err ( e) => Err ( IntoInnerError ( self , e) ) ,
@@ -185,6 +199,7 @@ impl<W: Write> BufWriter<W> {
185
199
}
186
200
}
187
201
202
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
188
203
impl < W : Write > Write for BufWriter < W > {
189
204
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
190
205
if self . buf . len ( ) + buf. len ( ) > self . buf . capacity ( ) {
@@ -224,23 +239,30 @@ impl<W> IntoInnerError<W> {
224
239
/// Returns the error which caused the call to `into_inner` to fail.
225
240
///
226
241
/// This error was returned when attempting to flush the internal buffer.
242
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
227
243
pub fn error ( & self ) -> & Error { & self . 1 }
228
244
229
245
/// Returns the underlying `BufWriter` instance which generated the error.
230
246
///
231
247
/// The returned object can be used to retry a flush or re-inspect the
232
248
/// buffer.
249
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
233
250
pub fn into_inner ( self ) -> W { self . 0 }
234
251
}
235
252
253
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
236
254
impl < W > FromError < IntoInnerError < W > > for Error {
237
255
fn from_error ( iie : IntoInnerError < W > ) -> Error { iie. 1 }
238
256
}
239
257
240
- impl < W > StdError for IntoInnerError < W > {
241
- fn description ( & self ) -> & str { self . error ( ) . description ( ) }
258
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
259
+ impl < W > error:: Error for IntoInnerError < W > {
260
+ fn description ( & self ) -> & str {
261
+ error:: Error :: description ( self . error ( ) )
262
+ }
242
263
}
243
264
265
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
244
266
impl < W > fmt:: Display for IntoInnerError < W > {
245
267
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
246
268
self . error ( ) . fmt ( f)
@@ -251,33 +273,49 @@ impl<W> fmt::Display for IntoInnerError<W> {
251
273
/// (`0x0a`, `'\n'`) is detected.
252
274
///
253
275
/// This writer will be flushed when it is dropped.
276
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
254
277
pub struct LineWriter < W > {
255
278
inner : BufWriter < W > ,
256
279
}
257
280
258
281
impl < W : Write > LineWriter < W > {
259
282
/// Creates a new `LineWriter`
283
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
260
284
pub fn new ( inner : W ) -> LineWriter < W > {
261
285
// Lines typically aren't that long, don't use a giant buffer
262
- LineWriter { inner : BufWriter :: with_capacity ( 1024 , inner) }
286
+ LineWriter :: with_capacity ( 1024 , inner)
287
+ }
288
+
289
+ /// Creates a new `LineWriter` with a specified capacity for the internal
290
+ /// buffer.
291
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
292
+ pub fn with_capacity ( cap : usize , inner : W ) -> LineWriter < W > {
293
+ LineWriter { inner : BufWriter :: with_capacity ( cap, inner) }
263
294
}
264
295
265
296
/// Gets a reference to the underlying writer.
297
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
298
+ pub fn get_ref ( & self ) -> & W { self . inner . get_ref ( ) }
299
+
300
+ /// Gets a mutable reference to the underlying writer.
266
301
///
267
- /// This type does not expose the ability to get a mutable reference to the
268
- /// underlying reader because that could possibly corrupt the buffer.
269
- pub fn get_ref < ' a > ( & ' a self ) -> & ' a W { self . inner . get_ref ( ) }
302
+ /// Caution must be taken when calling methods on the mutable reference
303
+ /// returned as extra writes could corrupt the output stream.
304
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
305
+ pub fn get_mut ( & mut self ) -> & mut W { self . inner . get_mut ( ) }
270
306
271
307
/// Unwraps this `LineWriter`, returning the underlying writer.
272
308
///
273
309
/// The internal buffer is flushed before returning the writer.
310
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
274
311
pub fn into_inner ( self ) -> Result < W , IntoInnerError < LineWriter < W > > > {
275
312
self . inner . into_inner ( ) . map_err ( |IntoInnerError ( buf, e) | {
276
313
IntoInnerError ( LineWriter { inner : buf } , e)
277
314
} )
278
315
}
279
316
}
280
317
318
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
281
319
impl < W : Write > Write for LineWriter < W > {
282
320
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
283
321
match buf. rposition_elem ( & b'\n' ) {
@@ -320,19 +358,21 @@ impl<W: Read> Read for InternalBufWriter<W> {
320
358
321
359
/// Wraps a Stream and buffers input and output to and from it.
322
360
///
323
- /// It can be excessively inefficient to work directly with a `Stream `. For
361
+ /// It can be excessively inefficient to work directly with a `Read+Write `. For
324
362
/// example, every call to `read` or `write` on `TcpStream` results in a system
325
363
/// call. A `BufStream` keeps in memory buffers of data, making large,
326
- /// infrequent calls to `read` and `write` on the underlying `Stream `.
364
+ /// infrequent calls to `read` and `write` on the underlying `Read+Write `.
327
365
///
328
366
/// The output half will be flushed when this stream is dropped.
367
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
329
368
pub struct BufStream < S > {
330
369
inner : BufReader < InternalBufWriter < S > >
331
370
}
332
371
333
372
impl < S : Read + Write > BufStream < S > {
334
373
/// Creates a new buffered stream with explicitly listed capacities for the
335
374
/// reader/writer buffer.
375
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
336
376
pub fn with_capacities ( reader_cap : usize , writer_cap : usize , inner : S )
337
377
-> BufStream < S > {
338
378
let writer = BufWriter :: with_capacity ( writer_cap, inner) ;
@@ -343,11 +383,13 @@ impl<S: Read + Write> BufStream<S> {
343
383
344
384
/// Creates a new buffered stream with the default reader/writer buffer
345
385
/// capacities.
386
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
346
387
pub fn new ( inner : S ) -> BufStream < S > {
347
388
BufStream :: with_capacities ( DEFAULT_BUF_SIZE , DEFAULT_BUF_SIZE , inner)
348
389
}
349
390
350
391
/// Gets a reference to the underlying stream.
392
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
351
393
pub fn get_ref ( & self ) -> & S {
352
394
let InternalBufWriter ( ref w) = self . inner . inner ;
353
395
w. get_ref ( )
@@ -359,6 +401,7 @@ impl<S: Read + Write> BufStream<S> {
359
401
///
360
402
/// It is inadvisable to read directly from or write directly to the
361
403
/// underlying stream.
404
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
362
405
pub fn get_mut ( & mut self ) -> & mut S {
363
406
let InternalBufWriter ( ref mut w) = self . inner . inner ;
364
407
w. get_mut ( )
@@ -368,6 +411,7 @@ impl<S: Read + Write> BufStream<S> {
368
411
///
369
412
/// The internal buffer is flushed before returning the stream. Any leftover
370
413
/// data in the read buffer is lost.
414
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
371
415
pub fn into_inner ( self ) -> Result < S , IntoInnerError < BufStream < S > > > {
372
416
let BufReader { inner : InternalBufWriter ( w) , buf } = self . inner ;
373
417
w. into_inner ( ) . map_err ( |IntoInnerError ( w, e) | {
@@ -378,17 +422,20 @@ impl<S: Read + Write> BufStream<S> {
378
422
}
379
423
}
380
424
425
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
381
426
impl < S : Read + Write > BufRead for BufStream < S > {
382
427
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > { self . inner . fill_buf ( ) }
383
428
fn consume ( & mut self , amt : uint ) { self . inner . consume ( amt) }
384
429
}
385
430
431
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
386
432
impl < S : Read + Write > Read for BufStream < S > {
387
433
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
388
434
self . inner . read ( buf)
389
435
}
390
436
}
391
437
438
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
392
439
impl < S : Read + Write > Write for BufStream < S > {
393
440
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
394
441
self . inner . inner . get_mut ( ) . write ( buf)
0 commit comments