@@ -127,14 +127,49 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
127
127
ret
128
128
}
129
129
130
- /// A trait for objects which are byte-oriented sources .
130
+ /// The `Read` trait allows for reading bytes from a source .
131
131
///
132
- /// Readers are defined by one method, `read`. Each call to `read` will attempt
133
- /// to pull bytes from this source into a provided buffer.
132
+ /// Implementors of the `Read` trait are sometimes called 'readers'.
134
133
///
135
- /// Readers are intended to be composable with one another. Many objects
136
- /// throughout the I/O and related libraries take and provide types which
137
- /// implement the `Read` trait.
134
+ /// Readers are defined by one required method, `read()`. Each call to `read`
135
+ /// will attempt to pull bytes from this source into a provided buffer. A
136
+ /// number of other methods are implemented in terms of `read()`, giving
137
+ /// implementors a number of ways to read bytes while only needing to implement
138
+ /// a single method.
139
+ ///
140
+ /// Readers are intended to be composable with one another. Many implementors
141
+ /// throughout `std::io` take and provide types which implement the `Read`
142
+ /// trait.
143
+ ///
144
+ /// # Examples
145
+ ///
146
+ /// [`File`][file]s implement `Read`:
147
+ ///
148
+ /// [file]: ../std/fs/struct.File.html
149
+ ///
150
+ /// ```
151
+ /// use std::io;
152
+ /// use std::fs::File;
153
+ /// use std::io::Read;
154
+ ///
155
+ /// # fn foo() -> io::Result<()> {
156
+ /// let mut f = try!(File::open("foo.txt"));
157
+ /// let mut buffer = Vec::new();
158
+ ///
159
+ /// // read some bytes
160
+ /// f.read(&mut buffer).unwrap();
161
+ ///
162
+ /// // read the whole file
163
+ /// f.read_to_end(&mut buffer).unwrap();
164
+ ///
165
+ /// // read into a String, so that you don't need to do the conversion.
166
+ /// let mut buffer = String::new();
167
+ /// f.read_to_string(&mut buffer).unwrap();
168
+ ///
169
+ /// // and more! See the other methods for more details.
170
+ /// # Ok(())
171
+ /// # }
172
+ /// ```
138
173
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
139
174
pub trait Read {
140
175
/// Pull some bytes from this source into the specified buffer, returning
@@ -164,6 +199,27 @@ pub trait Read {
164
199
/// If this function encounters any form of I/O or other error, an error
165
200
/// variant will be returned. If an error is returned then it must be
166
201
/// guaranteed that no bytes were read.
202
+ ///
203
+ /// # Examples
204
+ ///
205
+ /// [`File`][file]s implement `Read`:
206
+ ///
207
+ /// [file]: ../std/fs/struct.File.html
208
+ ///
209
+ /// ```
210
+ /// use std::io;
211
+ /// use std::io::prelude::*;
212
+ /// use std::fs::File;
213
+ ///
214
+ /// # fn foo() -> io::Result<()> {
215
+ /// let mut f = try!(File::open("foo.txt"));
216
+ /// let mut buffer = [0; 10];
217
+ ///
218
+ /// // read 10 bytes
219
+ /// try!(f.read(&mut buffer[..]));
220
+ /// # Ok(())
221
+ /// # }
222
+ /// ```
167
223
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
168
224
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize > ;
169
225
@@ -185,6 +241,27 @@ pub trait Read {
185
241
/// If any other read error is encountered then this function immediately
186
242
/// returns. Any bytes which have already been read will be appended to
187
243
/// `buf`.
244
+ ///
245
+ /// # Examples
246
+ ///
247
+ /// [`File`][file]s implement `Read`:
248
+ ///
249
+ /// [file]: ../std/fs/struct.File.html
250
+ ///
251
+ /// ```
252
+ /// use std::io;
253
+ /// use std::io::prelude::*;
254
+ /// use std::fs::File;
255
+ ///
256
+ /// # fn foo() -> io::Result<()> {
257
+ /// let mut f = try!(File::open("foo.txt"));
258
+ /// let mut buffer = Vec::new();
259
+ ///
260
+ /// // read the whole file
261
+ /// try!(f.read_to_end(&mut buffer));
262
+ /// # Ok(())
263
+ /// # }
264
+ /// ```
188
265
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
189
266
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> Result < usize > {
190
267
read_to_end ( self , buf)
@@ -200,7 +277,29 @@ pub trait Read {
200
277
/// If the data in this stream is *not* valid UTF-8 then an error is
201
278
/// returned and `buf` is unchanged.
202
279
///
203
- /// See `read_to_end` for other error semantics.
280
+ /// See [`read_to_end()`][readtoend] for other error semantics.
281
+ ///
282
+ /// [readtoend]: #method.read_to_end
283
+ ///
284
+ /// # Examples
285
+ ///
286
+ /// [`File`][file]s implement `Read`:
287
+ ///
288
+ /// [file]: ../std/fs/struct.File.html
289
+ ///
290
+ /// ```
291
+ /// use std::io;
292
+ /// use std::io::prelude::*;
293
+ /// use std::fs::File;
294
+ ///
295
+ /// # fn foo() -> io::Result<()> {
296
+ /// let mut f = try!(File::open("foo.txt"));
297
+ /// let mut buffer = String::new();
298
+ ///
299
+ /// try!(f.read_to_string(&mut buffer));
300
+ /// # Ok(())
301
+ /// # }
302
+ /// ```
204
303
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
205
304
fn read_to_string ( & mut self , buf : & mut String ) -> Result < usize > {
206
305
// Note that we do *not* call `.read_to_end()` here. We are passing
@@ -219,6 +318,36 @@ pub trait Read {
219
318
///
220
319
/// The returned adaptor also implements `Read` and will simply borrow this
221
320
/// current reader.
321
+ ///
322
+ /// # Examples
323
+ ///
324
+ /// [`File`][file]s implement `Read`:
325
+ ///
326
+ /// [file]: ../std/fs/struct.File.html
327
+ ///
328
+ /// ```
329
+ /// use std::io;
330
+ /// use std::io::Read;
331
+ /// use std::fs::File;
332
+ ///
333
+ /// # fn foo() -> io::Result<()> {
334
+ /// let mut f = try!(File::open("foo.txt"));
335
+ /// let mut buffer = Vec::new();
336
+ /// let mut other_buffer = Vec::new();
337
+ ///
338
+ /// {
339
+ /// let reference = f.by_ref();
340
+ ///
341
+ /// // read at most 5 bytes
342
+ /// try!(reference.take(5).read_to_end(&mut buffer));
343
+ ///
344
+ /// } // drop our &mut reference so we can use f again
345
+ ///
346
+ /// // original file still usable, read the rest
347
+ /// try!(f.read_to_end(&mut other_buffer));
348
+ /// # Ok(())
349
+ /// # }
350
+ /// ```
222
351
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
223
352
fn by_ref ( & mut self ) -> & mut Self where Self : Sized { self }
224
353
@@ -228,6 +357,27 @@ pub trait Read {
228
357
/// R::Err>`. The yielded item is `Ok` if a byte was successfully read and
229
358
/// `Err` otherwise for I/O errors. EOF is mapped to returning `None` from
230
359
/// this iterator.
360
+ ///
361
+ /// # Examples
362
+ ///
363
+ /// [`File`][file]s implement `Read`:
364
+ ///
365
+ /// [file]: ../std/fs/struct.File.html
366
+ ///
367
+ /// ```
368
+ /// use std::io;
369
+ /// use std::io::prelude::*;
370
+ /// use std::fs::File;
371
+ ///
372
+ /// # fn foo() -> io::Result<()> {
373
+ /// let mut f = try!(File::open("foo.txt"));
374
+ ///
375
+ /// for byte in f.bytes() {
376
+ /// println!("{}", byte.unwrap());
377
+ /// }
378
+ /// # Ok(())
379
+ /// # }
380
+ /// ```
231
381
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
232
382
fn bytes ( self ) -> Bytes < Self > where Self : Sized {
233
383
Bytes { inner : self }
@@ -243,6 +393,28 @@ pub trait Read {
243
393
///
244
394
/// Currently this adaptor will discard intermediate data read, and should
245
395
/// be avoided if this is not desired.
396
+ ///
397
+ /// # Examples
398
+ ///
399
+ /// [`File`][file]s implement `Read`:
400
+ ///
401
+ /// [file]: ../std/fs/struct.File.html
402
+ ///
403
+ /// ```
404
+ /// #![feature(io)]
405
+ /// use std::io;
406
+ /// use std::io::prelude::*;
407
+ /// use std::fs::File;
408
+ ///
409
+ /// # fn foo() -> io::Result<()> {
410
+ /// let mut f = try!(File::open("foo.txt"));
411
+ ///
412
+ /// for c in f.chars() {
413
+ /// println!("{}", c.unwrap());
414
+ /// }
415
+ /// # Ok(())
416
+ /// # }
417
+ /// ```
246
418
#[ unstable( feature = "io" , reason = "the semantics of a partial read/write \
247
419
of where errors happen is currently \
248
420
unclear and may change") ]
@@ -255,6 +427,31 @@ pub trait Read {
255
427
/// The returned `Read` instance will first read all bytes from this object
256
428
/// until EOF is encountered. Afterwards the output is equivalent to the
257
429
/// output of `next`.
430
+ ///
431
+ /// # Examples
432
+ ///
433
+ /// [`File`][file]s implement `Read`:
434
+ ///
435
+ /// [file]: ../std/fs/struct.File.html
436
+ ///
437
+ /// ```
438
+ /// use std::io;
439
+ /// use std::io::prelude::*;
440
+ /// use std::fs::File;
441
+ ///
442
+ /// # fn foo() -> io::Result<()> {
443
+ /// let mut f1 = try!(File::open("foo.txt"));
444
+ /// let mut f2 = try!(File::open("bar.txt"));
445
+ ///
446
+ /// let mut handle = f1.chain(f2);
447
+ /// let mut buffer = String::new();
448
+ ///
449
+ /// // read the value into a String. We could use any Read method here,
450
+ /// // this is just one example.
451
+ /// try!(handle.read_to_string(&mut buffer));
452
+ /// # Ok(())
453
+ /// # }
454
+ /// ```
258
455
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
259
456
fn chain < R : Read > ( self , next : R ) -> Chain < Self , R > where Self : Sized {
260
457
Chain { first : self , second : next, done_first : false }
@@ -266,6 +463,29 @@ pub trait Read {
266
463
/// `limit` bytes, after which it will always return EOF (`Ok(0)`). Any
267
464
/// read errors will not count towards the number of bytes read and future
268
465
/// calls to `read` may succeed.
466
+ ///
467
+ /// # Examples
468
+ ///
469
+ /// [`File`][file]s implement `Read`:
470
+ ///
471
+ /// [file]: ../std/fs/struct.File.html
472
+ ///
473
+ /// ```
474
+ /// use std::io;
475
+ /// use std::io::prelude::*;
476
+ /// use std::fs::File;
477
+ ///
478
+ /// # fn foo() -> io::Result<()> {
479
+ /// let mut f = try!(File::open("foo.txt"));
480
+ /// let mut buffer = [0; 10];
481
+ ///
482
+ /// // read at most five bytes
483
+ /// let mut handle = f.take(5);
484
+ ///
485
+ /// try!(handle.read(&mut buffer));
486
+ /// # Ok(())
487
+ /// # }
488
+ /// ```
269
489
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
270
490
fn take ( self , limit : u64 ) -> Take < Self > where Self : Sized {
271
491
Take { inner : self , limit : limit }
@@ -277,6 +497,31 @@ pub trait Read {
277
497
/// Whenever the returned `Read` instance is read it will write the read
278
498
/// data to `out`. The current semantics of this implementation imply that
279
499
/// a `write` error will not report how much data was initially read.
500
+ ///
501
+ /// # Examples
502
+ ///
503
+ /// [`File`][file]s implement `Read`:
504
+ ///
505
+ /// [file]: ../std/fs/struct.File.html
506
+ ///
507
+ /// ```
508
+ /// #![feature(io)]
509
+ /// use std::io;
510
+ /// use std::io::prelude::*;
511
+ /// use std::fs::File;
512
+ ///
513
+ /// # fn foo() -> io::Result<()> {
514
+ /// let mut f = try!(File::open("foo.txt"));
515
+ /// let mut buffer1 = Vec::with_capacity(10);
516
+ /// let mut buffer2 = Vec::with_capacity(10);
517
+ ///
518
+ /// // write the output to buffer1 as we read
519
+ /// let mut handle = f.tee(&mut buffer1);
520
+ ///
521
+ /// try!(handle.read(&mut buffer2));
522
+ /// # Ok(())
523
+ /// # }
524
+ /// ```
280
525
#[ unstable( feature = "io" , reason = "the semantics of a partial read/write \
281
526
of where errors happen is currently \
282
527
unclear and may change") ]
@@ -293,9 +538,30 @@ pub trait Read {
293
538
/// The `flush` method is useful for adaptors and explicit buffers themselves
294
539
/// for ensuring that all buffered data has been pushed out to the "true sink".
295
540
///
296
- /// Writers are intended to be composable with one another. Many objects
297
- /// throughout the I/O and related libraries take and provide types which
298
- /// implement the `Write` trait.
541
+ /// * The `write()` method will attempt to write some data into the object,
542
+ /// returning how many bytes were successfully written.
543
+ ///
544
+ /// * The `flush()` method is useful for adaptors and explicit buffers
545
+ /// themselves for ensuring that all buffered data has been pushed out to the
546
+ /// 'true sink'.
547
+ ///
548
+ /// Writers are intended to be composable with one another. Many implementors
549
+ /// throughout `std::io` take and provide types which implement the `Write`
550
+ /// trait.
551
+ ///
552
+ /// # Examples
553
+ ///
554
+ /// ```
555
+ /// use std::io::prelude::*;
556
+ /// use std::fs::File;
557
+ ///
558
+ /// # fn foo() -> std::io::Result<()> {
559
+ /// let mut buffer = try!(File::create("foo.txt"));
560
+ ///
561
+ /// try!(buffer.write(b"some bytes"));
562
+ /// # Ok(())
563
+ /// # }
564
+ /// ```
299
565
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
300
566
pub trait Write {
301
567
/// Write a buffer into this object, returning how many bytes were written.
@@ -508,8 +774,8 @@ fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
508
774
/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
509
775
/// to perform extra ways of reading.
510
776
///
511
- /// For example, reading line-by-line requires using a buffer, so if you want
512
- /// to read by line, you'll need `BufRead`, which includes a
777
+ /// For example, reading line-by-line is inefficient without using a buffer, so
778
+ /// if you want to read by line, you'll need `BufRead`, which includes a
513
779
/// [`read_line()`][readline] method as well as a [`lines()`][lines] iterator.
514
780
///
515
781
/// [readline]: #method.read_line
0 commit comments