]> git.proxmox.com Git - rustc.git/blame - src/libstd/io/mod.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / libstd / io / mod.rs
CommitLineData
85aaf69f 1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
1a4d82fc
JJ
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
1a4d82fc 10
85aaf69f 11//! Traits, helpers, and type definitions for core I/O functionality.
c1a9b12d
SL
12//!
13//! The `std::io` module contains a number of common things you'll need
14//! when doing input and output. The most core part of this module is
15//! the [`Read`][read] and [`Write`][write] traits, which provide the
16//! most general interface for reading and writing input and output.
17//!
18//! [read]: trait.Read.html
19//! [write]: trait.Write.html
20//!
21//! # Read and Write
22//!
b039eaaf
SL
23//! Because they are traits, `Read` and `Write` are implemented by a number
24//! of other types, and you can implement them for your types too. As such,
25//! you'll see a few different types of I/O throughout the documentation in
26//! this module: `File`s, `TcpStream`s, and sometimes even `Vec<T>`s. For
27//! example, `Read` adds a `read()` method, which we can use on `File`s:
c1a9b12d
SL
28//!
29//! ```
30//! use std::io;
31//! use std::io::prelude::*;
32//! use std::fs::File;
33//!
34//! # fn foo() -> io::Result<()> {
35//! let mut f = try!(File::open("foo.txt"));
36//! let mut buffer = [0; 10];
37//!
38//! // read up to 10 bytes
39//! try!(f.read(&mut buffer));
40//!
41//! println!("The bytes: {:?}", buffer);
42//! # Ok(())
43//! # }
44//! ```
45//!
46//! `Read` and `Write` are so important, implementors of the two traits have a
47//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
48//! of 'a type that implements the `Read` trait'. Much easier!
49//!
50//! ## Seek and BufRead
51//!
52//! Beyond that, there are two important traits that are provided: [`Seek`][seek]
53//! and [`BufRead`][bufread]. Both of these build on top of a reader to control
54//! how the reading happens. `Seek` lets you control where the next byte is
55//! coming from:
56//!
57//! ```
58//! use std::io;
59//! use std::io::prelude::*;
60//! use std::io::SeekFrom;
61//! use std::fs::File;
62//!
63//! # fn foo() -> io::Result<()> {
64//! let mut f = try!(File::open("foo.txt"));
65//! let mut buffer = [0; 10];
66//!
67//! // skip to the last 10 bytes of the file
68//! try!(f.seek(SeekFrom::End(-10)));
69//!
70//! // read up to 10 bytes
71//! try!(f.read(&mut buffer));
72//!
73//! println!("The bytes: {:?}", buffer);
74//! # Ok(())
75//! # }
76//! ```
77//!
78//! [seek]: trait.Seek.html
79//! [bufread]: trait.BufRead.html
80//!
81//! `BufRead` uses an internal buffer to provide a number of other ways to read, but
82//! to show it off, we'll need to talk about buffers in general. Keep reading!
83//!
84//! ## BufReader and BufWriter
85//!
86//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
87//! making near-constant calls to the operating system. To help with this,
88//! `std::io` comes with two structs, `BufReader` and `BufWriter`, which wrap
89//! readers and writers. The wrapper uses a buffer, reducing the number of
90//! calls and providing nicer methods for accessing exactly what you want.
91//!
92//! For example, `BufReader` works with the `BufRead` trait to add extra
93//! methods to any reader:
94//!
95//! ```
96//! use std::io;
97//! use std::io::prelude::*;
98//! use std::io::BufReader;
99//! use std::fs::File;
100//!
101//! # fn foo() -> io::Result<()> {
102//! let f = try!(File::open("foo.txt"));
103//! let mut reader = BufReader::new(f);
104//! let mut buffer = String::new();
105//!
106//! // read a line into buffer
107//! try!(reader.read_line(&mut buffer));
108//!
109//! println!("{}", buffer);
110//! # Ok(())
111//! # }
112//! ```
113//!
b039eaaf 114//! `BufWriter` doesn't add any new ways of writing; it just buffers every call
9cc50fc6 115//! to [`write()`][write()]:
c1a9b12d
SL
116//!
117//! ```
118//! use std::io;
119//! use std::io::prelude::*;
120//! use std::io::BufWriter;
121//! use std::fs::File;
122//!
123//! # fn foo() -> io::Result<()> {
124//! let f = try!(File::create("foo.txt"));
125//! {
126//! let mut writer = BufWriter::new(f);
127//!
128//! // write a byte to the buffer
129//! try!(writer.write(&[42]));
130//!
131//! } // the buffer is flushed once writer goes out of scope
132//!
133//! # Ok(())
134//! # }
135//! ```
136//!
9cc50fc6 137//! [write()]: trait.Write.html#tymethod.write
c1a9b12d
SL
138//!
139//! ## Standard input and output
140//!
141//! A very common source of input is standard input:
142//!
143//! ```
144//! use std::io;
145//!
146//! # fn foo() -> io::Result<()> {
147//! let mut input = String::new();
148//!
149//! try!(io::stdin().read_line(&mut input));
150//!
151//! println!("You typed: {}", input.trim());
152//! # Ok(())
153//! # }
154//! ```
155//!
156//! And a very common source of output is standard output:
157//!
158//! ```
159//! use std::io;
160//! use std::io::prelude::*;
161//!
162//! # fn foo() -> io::Result<()> {
163//! try!(io::stdout().write(&[42]));
164//! # Ok(())
165//! # }
166//! ```
167//!
b039eaaf 168//! Of course, using `io::stdout()` directly is less common than something like
c1a9b12d
SL
169//! `println!`.
170//!
171//! ## Iterator types
172//!
173//! A large number of the structures provided by `std::io` are for various
174//! ways of iterating over I/O. For example, `Lines` is used to split over
175//! lines:
176//!
177//! ```
178//! use std::io;
179//! use std::io::prelude::*;
180//! use std::io::BufReader;
181//! use std::fs::File;
182//!
183//! # fn foo() -> io::Result<()> {
184//! let f = try!(File::open("foo.txt"));
a7813a04 185//! let reader = BufReader::new(f);
c1a9b12d
SL
186//!
187//! for line in reader.lines() {
a7813a04 188//! println!("{}", try!(line));
c1a9b12d
SL
189//! }
190//!
191//! # Ok(())
192//! # }
193//! ```
194//!
195//! ## Functions
196//!
a7813a04 197//! There are a number of [functions][functions-list] that offer access to various
c1a9b12d
SL
198//! features. For example, we can use three of these functions to copy everything
199//! from standard input to standard output:
200//!
201//! ```
202//! use std::io;
203//!
204//! # fn foo() -> io::Result<()> {
205//! try!(io::copy(&mut io::stdin(), &mut io::stdout()));
206//! # Ok(())
207//! # }
208//! ```
209//!
a7813a04 210//! [functions-list]: #functions-1
c1a9b12d
SL
211//!
212//! ## io::Result
213//!
214//! Last, but certainly not least, is [`io::Result`][result]. This type is used
215//! as the return type of many `std::io` functions that can cause an error, and
216//! can be returned from your own functions as well. Many of the examples in this
217//! module use the [`try!`][try] macro:
218//!
219//! ```
220//! use std::io;
221//!
222//! fn read_input() -> io::Result<()> {
223//! let mut input = String::new();
224//!
225//! try!(io::stdin().read_line(&mut input));
226//!
227//! println!("You typed: {}", input.trim());
228//!
229//! Ok(())
230//! }
231//! ```
232//!
233//! The return type of `read_input()`, `io::Result<()>`, is a very common type
234//! for functions which don't have a 'real' return value, but do want to return
235//! errors if they happen. In this case, the only purpose of this function is
b039eaaf 236//! to read the line and print it, so we use `()`.
c1a9b12d
SL
237//!
238//! [result]: type.Result.html
9e0c209e 239//! [try]: ../macro.try.html
9cc50fc6
SL
240//!
241//! ## Platform-specific behavior
242//!
243//! Many I/O functions throughout the standard library are documented to indicate
244//! what various library or syscalls they are delegated to. This is done to help
245//! applications both understand what's happening under the hood as well as investigate
246//! any possibly unclear semantics. Note, however, that this is informative, not a binding
247//! contract. The implementation of many of these functions are subject to change over
248//! time and may call fewer or more syscalls/library functions.
1a4d82fc 249
c34b1796 250#![stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 251
85aaf69f 252use cmp;
d9579d0f 253use rustc_unicode::str as core_str;
c34b1796 254use error as std_error;
1a4d82fc 255use fmt;
85aaf69f 256use result;
c34b1796 257use str;
9cc50fc6 258use memchr;
1a4d82fc 259
92a42be0 260#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 261pub use self::buffered::{BufReader, BufWriter, LineWriter};
92a42be0 262#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 263pub use self::buffered::IntoInnerError;
92a42be0 264#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 265pub use self::cursor::Cursor;
92a42be0 266#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 267pub use self::error::{Result, Error, ErrorKind};
92a42be0 268#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 269pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat};
92a42be0 270#[stable(feature = "rust1", since = "1.0.0")]
c34b1796 271pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr};
92a42be0 272#[stable(feature = "rust1", since = "1.0.0")]
c34b1796 273pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
92a42be0 274#[unstable(feature = "libstd_io_internals", issue = "0")]
c34b1796
AL
275#[doc(no_inline, hidden)]
276pub use self::stdio::{set_panic, set_print};
277
85aaf69f 278pub mod prelude;
1a4d82fc 279mod buffered;
85aaf69f
SL
280mod cursor;
281mod error;
282mod impls;
62682a34 283mod lazy;
85aaf69f 284mod util;
c34b1796 285mod stdio;
1a4d82fc 286
54a0048b 287const DEFAULT_BUF_SIZE: usize = 8 * 1024;
1a4d82fc 288
85aaf69f
SL
289// A few methods below (read_to_string, read_line) will append data into a
290// `String` buffer, but we need to be pretty careful when doing this. The
291// implementation will just call `.as_mut_vec()` and then delegate to a
292// byte-oriented reading method, but we must ensure that when returning we never
293// leave `buf` in a state such that it contains invalid UTF-8 in its bounds.
294//
295// To this end, we use an RAII guard (to protect against panics) which updates
296// the length of the string when it is dropped. This guard initially truncates
297// the string to the prior length and only after we've validated that the
298// new contents are valid UTF-8 do we allow it to set a longer length.
299//
300// The unsafety in this function is twofold:
301//
302// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
303// checks.
304// 2. We're passing a raw buffer to the function `f`, and it is expected that
305// the function only *appends* bytes to the buffer. We'll get undefined
306// behavior if existing bytes are overwritten to have non-UTF-8 data.
c34b1796
AL
307fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
308 where F: FnOnce(&mut Vec<u8>) -> Result<usize>
85aaf69f
SL
309{
310 struct Guard<'a> { s: &'a mut Vec<u8>, len: usize }
d9579d0f 311 impl<'a> Drop for Guard<'a> {
85aaf69f
SL
312 fn drop(&mut self) {
313 unsafe { self.s.set_len(self.len); }
1a4d82fc
JJ
314 }
315 }
1a4d82fc 316
85aaf69f
SL
317 unsafe {
318 let mut g = Guard { len: buf.len(), s: buf.as_mut_vec() };
319 let ret = f(g.s);
320 if str::from_utf8(&g.s[g.len..]).is_err() {
c34b1796 321 ret.and_then(|_| {
62682a34 322 Err(Error::new(ErrorKind::InvalidData,
c34b1796 323 "stream did not contain valid UTF-8"))
85aaf69f
SL
324 })
325 } else {
326 g.len = g.s.len();
327 ret
328 }
1a4d82fc
JJ
329 }
330}
331
c34b1796
AL
332// This uses an adaptive system to extend the vector when it fills. We want to
333// avoid paying to allocate and zero a huge chunk of memory if the reader only
334// has 4 bytes while still making large reads if the reader does have a ton
335// of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
336// time is 4,500 times (!) slower than this if the reader has a very small
337// amount of data to return.
338fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
339 let start_len = buf.len();
340 let mut len = start_len;
341 let mut new_write_size = 16;
342 let ret;
85aaf69f 343 loop {
c34b1796
AL
344 if len == buf.len() {
345 if new_write_size < DEFAULT_BUF_SIZE {
346 new_write_size *= 2;
347 }
c1a9b12d 348 buf.resize(len + new_write_size, 0);
85aaf69f 349 }
c34b1796
AL
350
351 match r.read(&mut buf[len..]) {
352 Ok(0) => {
353 ret = Ok(len - start_len);
354 break;
355 }
356 Ok(n) => len += n,
85aaf69f 357 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
c34b1796
AL
358 Err(e) => {
359 ret = Err(e);
360 break;
361 }
85aaf69f 362 }
1a4d82fc 363 }
c34b1796
AL
364
365 buf.truncate(len);
366 ret
1a4d82fc
JJ
367}
368
c1a9b12d 369/// The `Read` trait allows for reading bytes from a source.
85aaf69f 370///
c1a9b12d 371/// Implementors of the `Read` trait are sometimes called 'readers'.
1a4d82fc 372///
c1a9b12d
SL
373/// Readers are defined by one required method, `read()`. Each call to `read`
374/// will attempt to pull bytes from this source into a provided buffer. A
375/// number of other methods are implemented in terms of `read()`, giving
376/// implementors a number of ways to read bytes while only needing to implement
377/// a single method.
378///
379/// Readers are intended to be composable with one another. Many implementors
380/// throughout `std::io` take and provide types which implement the `Read`
381/// trait.
382///
b039eaaf
SL
383/// Please note that each call to `read` may involve a system call, and
384/// therefore, using something that implements [`BufRead`][bufread], such as
385/// [`BufReader`][bufreader], will be more efficient.
386///
387/// [bufread]: trait.BufRead.html
388/// [bufreader]: struct.BufReader.html
389///
c1a9b12d
SL
390/// # Examples
391///
392/// [`File`][file]s implement `Read`:
393///
9cc50fc6 394/// [file]: ../fs/struct.File.html
c1a9b12d
SL
395///
396/// ```
397/// use std::io;
398/// use std::io::prelude::*;
399/// use std::fs::File;
400///
401/// # fn foo() -> io::Result<()> {
402/// let mut f = try!(File::open("foo.txt"));
403/// let mut buffer = [0; 10];
404///
405/// // read up to 10 bytes
406/// try!(f.read(&mut buffer));
407///
408/// let mut buffer = vec![0; 10];
409/// // read the whole file
410/// try!(f.read_to_end(&mut buffer));
411///
412/// // read into a String, so that you don't need to do the conversion.
413/// let mut buffer = String::new();
414/// try!(f.read_to_string(&mut buffer));
415///
416/// // and more! See the other methods for more details.
417/// # Ok(())
418/// # }
419/// ```
c34b1796 420#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
421pub trait Read {
422 /// Pull some bytes from this source into the specified buffer, returning
423 /// how many bytes were read.
424 ///
425 /// This function does not provide any guarantees about whether it blocks
426 /// waiting for data, but if an object needs to block for a read but cannot
427 /// it will typically signal this via an `Err` return value.
428 ///
429 /// If the return value of this method is `Ok(n)`, then it must be
430 /// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
9346a6ac 431 /// that the buffer `buf` has been filled in with `n` bytes of data from this
85aaf69f
SL
432 /// source. If `n` is `0`, then it can indicate one of two scenarios:
433 ///
434 /// 1. This reader has reached its "end of file" and will likely no longer
435 /// be able to produce bytes. Note that this does not mean that the
436 /// reader will *always* no longer be able to produce bytes.
437 /// 2. The buffer specified was 0 bytes in length.
438 ///
439 /// No guarantees are provided about the contents of `buf` when this
440 /// function is called, implementations cannot rely on any property of the
441 /// contents of `buf` being true. It is recommended that implementations
442 /// only write data to `buf` instead of reading its contents.
1a4d82fc 443 ///
85aaf69f 444 /// # Errors
1a4d82fc 445 ///
85aaf69f
SL
446 /// If this function encounters any form of I/O or other error, an error
447 /// variant will be returned. If an error is returned then it must be
448 /// guaranteed that no bytes were read.
c1a9b12d
SL
449 ///
450 /// # Examples
451 ///
452 /// [`File`][file]s implement `Read`:
453 ///
9cc50fc6 454 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
455 ///
456 /// ```
457 /// use std::io;
458 /// use std::io::prelude::*;
459 /// use std::fs::File;
460 ///
461 /// # fn foo() -> io::Result<()> {
462 /// let mut f = try!(File::open("foo.txt"));
463 /// let mut buffer = [0; 10];
464 ///
465 /// // read 10 bytes
466 /// try!(f.read(&mut buffer[..]));
467 /// # Ok(())
468 /// # }
469 /// ```
c34b1796 470 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 471 fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
1a4d82fc 472
85aaf69f 473 /// Read all bytes until EOF in this source, placing them into `buf`.
1a4d82fc 474 ///
85aaf69f 475 /// All bytes read from this source will be appended to the specified buffer
9346a6ac
AL
476 /// `buf`. This function will continuously call `read` to append more data to
477 /// `buf` until `read` returns either `Ok(0)` or an error of
478 /// non-`ErrorKind::Interrupted` kind.
1a4d82fc 479 ///
9346a6ac 480 /// If successful, this function will return the total number of bytes read.
1a4d82fc 481 ///
85aaf69f 482 /// # Errors
1a4d82fc 483 ///
85aaf69f
SL
484 /// If this function encounters an error of the kind
485 /// `ErrorKind::Interrupted` then the error is ignored and the operation
486 /// will continue.
1a4d82fc 487 ///
85aaf69f
SL
488 /// If any other read error is encountered then this function immediately
489 /// returns. Any bytes which have already been read will be appended to
490 /// `buf`.
c1a9b12d
SL
491 ///
492 /// # Examples
493 ///
494 /// [`File`][file]s implement `Read`:
495 ///
9cc50fc6 496 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
497 ///
498 /// ```
499 /// use std::io;
500 /// use std::io::prelude::*;
501 /// use std::fs::File;
502 ///
503 /// # fn foo() -> io::Result<()> {
504 /// let mut f = try!(File::open("foo.txt"));
505 /// let mut buffer = Vec::new();
506 ///
507 /// // read the whole file
508 /// try!(f.read_to_end(&mut buffer));
509 /// # Ok(())
510 /// # }
511 /// ```
c34b1796
AL
512 #[stable(feature = "rust1", since = "1.0.0")]
513 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
85aaf69f 514 read_to_end(self, buf)
1a4d82fc
JJ
515 }
516
85aaf69f 517 /// Read all bytes until EOF in this source, placing them into `buf`.
1a4d82fc 518 ///
c34b1796
AL
519 /// If successful, this function returns the number of bytes which were read
520 /// and appended to `buf`.
521 ///
85aaf69f 522 /// # Errors
1a4d82fc 523 ///
85aaf69f
SL
524 /// If the data in this stream is *not* valid UTF-8 then an error is
525 /// returned and `buf` is unchanged.
1a4d82fc 526 ///
c1a9b12d
SL
527 /// See [`read_to_end()`][readtoend] for other error semantics.
528 ///
529 /// [readtoend]: #method.read_to_end
530 ///
531 /// # Examples
532 ///
533 /// [`File`][file]s implement `Read`:
534 ///
9cc50fc6 535 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
536 ///
537 /// ```
538 /// use std::io;
539 /// use std::io::prelude::*;
540 /// use std::fs::File;
541 ///
542 /// # fn foo() -> io::Result<()> {
543 /// let mut f = try!(File::open("foo.txt"));
544 /// let mut buffer = String::new();
545 ///
546 /// try!(f.read_to_string(&mut buffer));
547 /// # Ok(())
548 /// # }
549 /// ```
c34b1796
AL
550 #[stable(feature = "rust1", since = "1.0.0")]
551 fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
85aaf69f
SL
552 // Note that we do *not* call `.read_to_end()` here. We are passing
553 // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
554 // method to fill it up. An arbitrary implementation could overwrite the
555 // entire contents of the vector, not just append to it (which is what
556 // we are expecting).
557 //
558 // To prevent extraneously checking the UTF-8-ness of the entire buffer
559 // we pass it to our hardcoded `read_to_end` implementation which we
560 // know is guaranteed to only read data into the end of the buffer.
561 append_to_string(buf, |b| read_to_end(self, b))
1a4d82fc
JJ
562 }
563
e9174d1e
SL
564 /// Read the exact number of bytes required to fill `buf`.
565 ///
566 /// This function reads as many bytes as necessary to completely fill the
567 /// specified buffer `buf`.
568 ///
569 /// No guarantees are provided about the contents of `buf` when this
570 /// function is called, implementations cannot rely on any property of the
571 /// contents of `buf` being true. It is recommended that implementations
572 /// only write data to `buf` instead of reading its contents.
573 ///
574 /// # Errors
575 ///
576 /// If this function encounters an error of the kind
577 /// `ErrorKind::Interrupted` then the error is ignored and the operation
578 /// will continue.
579 ///
580 /// If this function encounters an "end of file" before completely filling
92a42be0 581 /// the buffer, it returns an error of the kind `ErrorKind::UnexpectedEof`.
e9174d1e
SL
582 /// The contents of `buf` are unspecified in this case.
583 ///
584 /// If any other read error is encountered then this function immediately
585 /// returns. The contents of `buf` are unspecified in this case.
586 ///
587 /// If this function returns an error, it is unspecified how many bytes it
588 /// has read, but it will never read more than would be necessary to
589 /// completely fill the buffer.
590 ///
591 /// # Examples
592 ///
593 /// [`File`][file]s implement `Read`:
594 ///
9cc50fc6 595 /// [file]: ../fs/struct.File.html
e9174d1e
SL
596 ///
597 /// ```
e9174d1e
SL
598 /// use std::io;
599 /// use std::io::prelude::*;
600 /// use std::fs::File;
601 ///
602 /// # fn foo() -> io::Result<()> {
603 /// let mut f = try!(File::open("foo.txt"));
604 /// let mut buffer = [0; 10];
605 ///
606 /// // read exactly 10 bytes
607 /// try!(f.read_exact(&mut buffer));
608 /// # Ok(())
609 /// # }
610 /// ```
92a42be0 611 #[stable(feature = "read_exact", since = "1.6.0")]
e9174d1e
SL
612 fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
613 while !buf.is_empty() {
614 match self.read(buf) {
615 Ok(0) => break,
616 Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
617 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
618 Err(e) => return Err(e),
619 }
620 }
621 if !buf.is_empty() {
92a42be0 622 Err(Error::new(ErrorKind::UnexpectedEof,
e9174d1e
SL
623 "failed to fill whole buffer"))
624 } else {
625 Ok(())
626 }
627 }
628
9346a6ac 629 /// Creates a "by reference" adaptor for this instance of `Read`.
1a4d82fc 630 ///
85aaf69f
SL
631 /// The returned adaptor also implements `Read` and will simply borrow this
632 /// current reader.
c1a9b12d
SL
633 ///
634 /// # Examples
635 ///
636 /// [`File`][file]s implement `Read`:
637 ///
9cc50fc6 638 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
639 ///
640 /// ```
641 /// use std::io;
642 /// use std::io::Read;
643 /// use std::fs::File;
644 ///
645 /// # fn foo() -> io::Result<()> {
646 /// let mut f = try!(File::open("foo.txt"));
647 /// let mut buffer = Vec::new();
648 /// let mut other_buffer = Vec::new();
649 ///
650 /// {
651 /// let reference = f.by_ref();
652 ///
653 /// // read at most 5 bytes
654 /// try!(reference.take(5).read_to_end(&mut buffer));
655 ///
656 /// } // drop our &mut reference so we can use f again
657 ///
658 /// // original file still usable, read the rest
659 /// try!(f.read_to_end(&mut other_buffer));
660 /// # Ok(())
661 /// # }
662 /// ```
c34b1796
AL
663 #[stable(feature = "rust1", since = "1.0.0")]
664 fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
1a4d82fc 665
9346a6ac 666 /// Transforms this `Read` instance to an `Iterator` over its bytes.
1a4d82fc 667 ///
85aaf69f
SL
668 /// The returned type implements `Iterator` where the `Item` is `Result<u8,
669 /// R::Err>`. The yielded item is `Ok` if a byte was successfully read and
670 /// `Err` otherwise for I/O errors. EOF is mapped to returning `None` from
671 /// this iterator.
c1a9b12d
SL
672 ///
673 /// # Examples
674 ///
675 /// [`File`][file]s implement `Read`:
676 ///
9cc50fc6 677 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
678 ///
679 /// ```
680 /// use std::io;
681 /// use std::io::prelude::*;
682 /// use std::fs::File;
683 ///
684 /// # fn foo() -> io::Result<()> {
685 /// let mut f = try!(File::open("foo.txt"));
686 ///
687 /// for byte in f.bytes() {
688 /// println!("{}", byte.unwrap());
689 /// }
690 /// # Ok(())
691 /// # }
692 /// ```
c34b1796
AL
693 #[stable(feature = "rust1", since = "1.0.0")]
694 fn bytes(self) -> Bytes<Self> where Self: Sized {
85aaf69f 695 Bytes { inner: self }
1a4d82fc
JJ
696 }
697
9346a6ac 698 /// Transforms this `Read` instance to an `Iterator` over `char`s.
1a4d82fc 699 ///
d9579d0f 700 /// This adaptor will attempt to interpret this reader as a UTF-8 encoded
85aaf69f
SL
701 /// sequence of characters. The returned iterator will return `None` once
702 /// EOF is reached for this reader. Otherwise each element yielded will be a
703 /// `Result<char, E>` where `E` may contain information about what I/O error
704 /// occurred or where decoding failed.
1a4d82fc 705 ///
85aaf69f
SL
706 /// Currently this adaptor will discard intermediate data read, and should
707 /// be avoided if this is not desired.
c1a9b12d
SL
708 ///
709 /// # Examples
710 ///
711 /// [`File`][file]s implement `Read`:
712 ///
9cc50fc6 713 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
714 ///
715 /// ```
716 /// #![feature(io)]
717 /// use std::io;
718 /// use std::io::prelude::*;
719 /// use std::fs::File;
720 ///
721 /// # fn foo() -> io::Result<()> {
722 /// let mut f = try!(File::open("foo.txt"));
723 ///
724 /// for c in f.chars() {
725 /// println!("{}", c.unwrap());
726 /// }
727 /// # Ok(())
728 /// # }
729 /// ```
c34b1796
AL
730 #[unstable(feature = "io", reason = "the semantics of a partial read/write \
731 of where errors happen is currently \
e9174d1e
SL
732 unclear and may change",
733 issue = "27802")]
c34b1796 734 fn chars(self) -> Chars<Self> where Self: Sized {
85aaf69f 735 Chars { inner: self }
1a4d82fc
JJ
736 }
737
9346a6ac 738 /// Creates an adaptor which will chain this stream with another.
1a4d82fc 739 ///
85aaf69f
SL
740 /// The returned `Read` instance will first read all bytes from this object
741 /// until EOF is encountered. Afterwards the output is equivalent to the
742 /// output of `next`.
c1a9b12d
SL
743 ///
744 /// # Examples
745 ///
746 /// [`File`][file]s implement `Read`:
747 ///
9cc50fc6 748 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
749 ///
750 /// ```
751 /// use std::io;
752 /// use std::io::prelude::*;
753 /// use std::fs::File;
754 ///
755 /// # fn foo() -> io::Result<()> {
756 /// let mut f1 = try!(File::open("foo.txt"));
757 /// let mut f2 = try!(File::open("bar.txt"));
758 ///
759 /// let mut handle = f1.chain(f2);
760 /// let mut buffer = String::new();
761 ///
762 /// // read the value into a String. We could use any Read method here,
763 /// // this is just one example.
764 /// try!(handle.read_to_string(&mut buffer));
765 /// # Ok(())
766 /// # }
767 /// ```
c34b1796
AL
768 #[stable(feature = "rust1", since = "1.0.0")]
769 fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
85aaf69f 770 Chain { first: self, second: next, done_first: false }
1a4d82fc
JJ
771 }
772
9346a6ac 773 /// Creates an adaptor which will read at most `limit` bytes from it.
1a4d82fc 774 ///
85aaf69f
SL
775 /// This function returns a new instance of `Read` which will read at most
776 /// `limit` bytes, after which it will always return EOF (`Ok(0)`). Any
777 /// read errors will not count towards the number of bytes read and future
778 /// calls to `read` may succeed.
c1a9b12d
SL
779 ///
780 /// # Examples
781 ///
782 /// [`File`][file]s implement `Read`:
783 ///
9cc50fc6 784 /// [file]: ../fs/struct.File.html
c1a9b12d
SL
785 ///
786 /// ```
787 /// use std::io;
788 /// use std::io::prelude::*;
789 /// use std::fs::File;
790 ///
791 /// # fn foo() -> io::Result<()> {
792 /// let mut f = try!(File::open("foo.txt"));
793 /// let mut buffer = [0; 5];
794 ///
795 /// // read at most five bytes
796 /// let mut handle = f.take(5);
797 ///
798 /// try!(handle.read(&mut buffer));
799 /// # Ok(())
800 /// # }
801 /// ```
c34b1796
AL
802 #[stable(feature = "rust1", since = "1.0.0")]
803 fn take(self, limit: u64) -> Take<Self> where Self: Sized {
85aaf69f 804 Take { inner: self, limit: limit }
1a4d82fc 805 }
85aaf69f 806}
1a4d82fc 807
85aaf69f
SL
808/// A trait for objects which are byte-oriented sinks.
809///
c1a9b12d
SL
810/// Implementors of the `Write` trait are sometimes called 'writers'.
811///
812/// Writers are defined by two required methods, `write()` and `flush()`:
813///
814/// * The `write()` method will attempt to write some data into the object,
815/// returning how many bytes were successfully written.
816///
817/// * The `flush()` method is useful for adaptors and explicit buffers
818/// themselves for ensuring that all buffered data has been pushed out to the
819/// 'true sink'.
820///
821/// Writers are intended to be composable with one another. Many implementors
822/// throughout `std::io` take and provide types which implement the `Write`
823/// trait.
824///
825/// # Examples
826///
827/// ```
828/// use std::io::prelude::*;
829/// use std::fs::File;
85aaf69f 830///
c1a9b12d
SL
831/// # fn foo() -> std::io::Result<()> {
832/// let mut buffer = try!(File::create("foo.txt"));
85aaf69f 833///
c1a9b12d
SL
834/// try!(buffer.write(b"some bytes"));
835/// # Ok(())
836/// # }
837/// ```
c34b1796 838#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
839pub trait Write {
840 /// Write a buffer into this object, returning how many bytes were written.
1a4d82fc 841 ///
85aaf69f
SL
842 /// This function will attempt to write the entire contents of `buf`, but
843 /// the entire write may not succeed, or the write may also generate an
844 /// error. A call to `write` represents *at most one* attempt to write to
845 /// any wrapped object.
1a4d82fc 846 ///
85aaf69f 847 /// Calls to `write` are not guaranteed to block waiting for data to be
62682a34 848 /// written, and a write which would otherwise block can be indicated through
85aaf69f 849 /// an `Err` variant.
1a4d82fc 850 ///
85aaf69f
SL
851 /// If the return value is `Ok(n)` then it must be guaranteed that
852 /// `0 <= n <= buf.len()`. A return value of `0` typically means that the
853 /// underlying object is no longer able to accept bytes and will likely not
854 /// be able to in the future as well, or that the buffer provided is empty.
1a4d82fc 855 ///
85aaf69f 856 /// # Errors
1a4d82fc 857 ///
85aaf69f
SL
858 /// Each call to `write` may generate an I/O error indicating that the
859 /// operation could not be completed. If an error is returned then no bytes
860 /// in the buffer were written to this writer.
1a4d82fc 861 ///
85aaf69f
SL
862 /// It is **not** considered an error if the entire buffer could not be
863 /// written to this writer.
c1a9b12d
SL
864 ///
865 /// # Examples
866 ///
867 /// ```
868 /// use std::io::prelude::*;
869 /// use std::fs::File;
870 ///
871 /// # fn foo() -> std::io::Result<()> {
872 /// let mut buffer = try!(File::create("foo.txt"));
873 ///
874 /// try!(buffer.write(b"some bytes"));
875 /// # Ok(())
876 /// # }
877 /// ```
c34b1796 878 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 879 fn write(&mut self, buf: &[u8]) -> Result<usize>;
1a4d82fc 880
85aaf69f
SL
881 /// Flush this output stream, ensuring that all intermediately buffered
882 /// contents reach their destination.
1a4d82fc 883 ///
85aaf69f 884 /// # Errors
1a4d82fc 885 ///
85aaf69f
SL
886 /// It is considered an error if not all bytes could be written due to
887 /// I/O errors or EOF being reached.
c1a9b12d
SL
888 ///
889 /// # Examples
890 ///
891 /// ```
892 /// use std::io::prelude::*;
893 /// use std::io::BufWriter;
894 /// use std::fs::File;
895 ///
896 /// # fn foo() -> std::io::Result<()> {
897 /// let mut buffer = BufWriter::new(try!(File::create("foo.txt")));
898 ///
899 /// try!(buffer.write(b"some bytes"));
900 /// try!(buffer.flush());
901 /// # Ok(())
902 /// # }
903 /// ```
c34b1796 904 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 905 fn flush(&mut self) -> Result<()>;
1a4d82fc 906
85aaf69f 907 /// Attempts to write an entire buffer into this write.
1a4d82fc 908 ///
85aaf69f
SL
909 /// This method will continuously call `write` while there is more data to
910 /// write. This method will not return until the entire buffer has been
911 /// successfully written or an error occurs. The first error generated from
912 /// this method will be returned.
1a4d82fc
JJ
913 ///
914 /// # Errors
915 ///
85aaf69f 916 /// This function will return the first error that `write` returns.
c1a9b12d
SL
917 ///
918 /// # Examples
919 ///
920 /// ```
921 /// use std::io::prelude::*;
922 /// use std::fs::File;
923 ///
924 /// # fn foo() -> std::io::Result<()> {
925 /// let mut buffer = try!(File::create("foo.txt"));
926 ///
927 /// try!(buffer.write_all(b"some bytes"));
928 /// # Ok(())
929 /// # }
930 /// ```
c34b1796 931 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 932 fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
9346a6ac 933 while !buf.is_empty() {
85aaf69f
SL
934 match self.write(buf) {
935 Ok(0) => return Err(Error::new(ErrorKind::WriteZero,
c34b1796 936 "failed to write whole buffer")),
85aaf69f
SL
937 Ok(n) => buf = &buf[n..],
938 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
939 Err(e) => return Err(e),
940 }
941 }
942 Ok(())
943 }
1a4d82fc
JJ
944
945 /// Writes a formatted string into this writer, returning any error
946 /// encountered.
947 ///
c1a9b12d
SL
948 /// This method is primarily used to interface with the
949 /// [`format_args!`][formatargs] macro, but it is rare that this should
950 /// explicitly be called. The [`write!`][write] macro should be favored to
951 /// invoke this method instead.
952 ///
9e0c209e
SL
953 /// [formatargs]: ../macro.format_args.html
954 /// [write]: ../macro.write.html
1a4d82fc 955 ///
c1a9b12d
SL
956 /// This function internally uses the [`write_all`][writeall] method on
957 /// this trait and hence will continuously write data so long as no errors
958 /// are received. This also means that partial writes are not indicated in
959 /// this signature.
960 ///
961 /// [writeall]: #method.write_all
85aaf69f 962 ///
1a4d82fc
JJ
963 /// # Errors
964 ///
965 /// This function will return any I/O error reported while formatting.
c1a9b12d
SL
966 ///
967 /// # Examples
968 ///
969 /// ```
970 /// use std::io::prelude::*;
971 /// use std::fs::File;
972 ///
973 /// # fn foo() -> std::io::Result<()> {
974 /// let mut buffer = try!(File::create("foo.txt"));
975 ///
976 /// // this call
977 /// try!(write!(buffer, "{:.*}", 2, 1.234567));
978 /// // turns into this:
979 /// try!(buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)));
980 /// # Ok(())
981 /// # }
982 /// ```
c34b1796 983 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
984 fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()> {
985 // Create a shim which translates a Write to a fmt::Write and saves
1a4d82fc 986 // off I/O errors. instead of discarding them
85aaf69f 987 struct Adaptor<'a, T: ?Sized + 'a> {
1a4d82fc 988 inner: &'a mut T,
85aaf69f 989 error: Result<()>,
1a4d82fc
JJ
990 }
991
85aaf69f 992 impl<'a, T: Write + ?Sized> fmt::Write for Adaptor<'a, T> {
1a4d82fc 993 fn write_str(&mut self, s: &str) -> fmt::Result {
85aaf69f 994 match self.inner.write_all(s.as_bytes()) {
1a4d82fc
JJ
995 Ok(()) => Ok(()),
996 Err(e) => {
997 self.error = Err(e);
998 Err(fmt::Error)
999 }
1000 }
1001 }
1002 }
1003
1004 let mut output = Adaptor { inner: self, error: Ok(()) };
1005 match fmt::write(&mut output, fmt) {
1006 Ok(()) => Ok(()),
7453a54e
SL
1007 Err(..) => {
1008 // check if the error came from the underlying `Write` or not
1009 if output.error.is_err() {
1010 output.error
1011 } else {
1012 Err(Error::new(ErrorKind::Other, "formatter error"))
1013 }
1014 }
1a4d82fc
JJ
1015 }
1016 }
1a4d82fc 1017
9346a6ac 1018 /// Creates a "by reference" adaptor for this instance of `Write`.
1a4d82fc 1019 ///
85aaf69f
SL
1020 /// The returned adaptor also implements `Write` and will simply borrow this
1021 /// current writer.
c1a9b12d
SL
1022 ///
1023 /// # Examples
1024 ///
1025 /// ```
1026 /// use std::io::Write;
1027 /// use std::fs::File;
1028 ///
1029 /// # fn foo() -> std::io::Result<()> {
1030 /// let mut buffer = try!(File::create("foo.txt"));
1031 ///
1032 /// let reference = buffer.by_ref();
1033 ///
1034 /// // we can use reference just like our original buffer
1035 /// try!(reference.write_all(b"some bytes"));
1036 /// # Ok(())
1037 /// # }
1038 /// ```
c34b1796
AL
1039 #[stable(feature = "rust1", since = "1.0.0")]
1040 fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
1a4d82fc
JJ
1041}
1042
c1a9b12d
SL
1043/// The `Seek` trait provides a cursor which can be moved within a stream of
1044/// bytes.
1a4d82fc 1045///
85aaf69f
SL
1046/// The stream typically has a fixed size, allowing seeking relative to either
1047/// end or the current offset.
c1a9b12d
SL
1048///
1049/// # Examples
1050///
1051/// [`File`][file]s implement `Seek`:
1052///
9cc50fc6 1053/// [file]: ../fs/struct.File.html
c1a9b12d
SL
1054///
1055/// ```
1056/// use std::io;
1057/// use std::io::prelude::*;
1058/// use std::fs::File;
1059/// use std::io::SeekFrom;
1060///
1061/// # fn foo() -> io::Result<()> {
1062/// let mut f = try!(File::open("foo.txt"));
1063///
1064/// // move the cursor 42 bytes from the start of the file
1065/// try!(f.seek(SeekFrom::Start(42)));
1066/// # Ok(())
1067/// # }
1068/// ```
c34b1796 1069#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 1070pub trait Seek {
c1a9b12d 1071 /// Seek to an offset, in bytes, in a stream.
85aaf69f 1072 ///
c1a9b12d
SL
1073 /// A seek beyond the end of a stream is allowed, but implementation
1074 /// defined.
85aaf69f 1075 ///
c1a9b12d
SL
1076 /// If the seek operation completed successfully,
1077 /// this method returns the new position from the start of the stream.
5bcae85e 1078 /// That position can be used later with [`SeekFrom::Start`].
85aaf69f
SL
1079 ///
1080 /// # Errors
1081 ///
c1a9b12d 1082 /// Seeking to a negative offset is considered an error.
5bcae85e
SL
1083 ///
1084 /// [`SeekFrom::Start`]: enum.SeekFrom.html#variant.Start
c34b1796 1085 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 1086 fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
1a4d82fc
JJ
1087}
1088
85aaf69f 1089/// Enumeration of possible methods to seek within an I/O object.
5bcae85e
SL
1090///
1091/// It is used by the [`Seek`] trait.
1092///
1093/// [`Seek`]: trait.Seek.html
85aaf69f 1094#[derive(Copy, PartialEq, Eq, Clone, Debug)]
c34b1796 1095#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1096pub enum SeekFrom {
1097 /// Set the offset to the provided number of bytes.
c34b1796 1098 #[stable(feature = "rust1", since = "1.0.0")]
7453a54e 1099 Start(#[stable(feature = "rust1", since = "1.0.0")] u64),
1a4d82fc 1100
85aaf69f
SL
1101 /// Set the offset to the size of this object plus the specified number of
1102 /// bytes.
1103 ///
9cc50fc6 1104 /// It is possible to seek beyond the end of an object, but it's an error to
85aaf69f 1105 /// seek before byte 0.
c34b1796 1106 #[stable(feature = "rust1", since = "1.0.0")]
7453a54e 1107 End(#[stable(feature = "rust1", since = "1.0.0")] i64),
1a4d82fc 1108
85aaf69f
SL
1109 /// Set the offset to the current position plus the specified number of
1110 /// bytes.
1111 ///
9cc50fc6 1112 /// It is possible to seek beyond the end of an object, but it's an error to
85aaf69f 1113 /// seek before byte 0.
c34b1796 1114 #[stable(feature = "rust1", since = "1.0.0")]
7453a54e 1115 Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
1a4d82fc
JJ
1116}
1117
85aaf69f 1118fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
c34b1796
AL
1119 -> Result<usize> {
1120 let mut read = 0;
85aaf69f
SL
1121 loop {
1122 let (done, used) = {
1123 let available = match r.fill_buf() {
1124 Ok(n) => n,
1125 Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
1126 Err(e) => return Err(e)
1127 };
9cc50fc6 1128 match memchr::memchr(delim, available) {
85aaf69f 1129 Some(i) => {
92a42be0 1130 buf.extend_from_slice(&available[..i + 1]);
85aaf69f
SL
1131 (true, i + 1)
1132 }
1133 None => {
92a42be0 1134 buf.extend_from_slice(available);
85aaf69f
SL
1135 (false, available.len())
1136 }
1137 }
1138 };
1139 r.consume(used);
c34b1796 1140 read += used;
85aaf69f 1141 if done || used == 0 {
c34b1796 1142 return Ok(read);
1a4d82fc
JJ
1143 }
1144 }
1145}
1146
c1a9b12d
SL
1147/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
1148/// to perform extra ways of reading.
1149///
1150/// For example, reading line-by-line is inefficient without using a buffer, so
1151/// if you want to read by line, you'll need `BufRead`, which includes a
1152/// [`read_line()`][readline] method as well as a [`lines()`][lines] iterator.
1153///
1154/// [readline]: #method.read_line
1155/// [lines]: #method.lines
1156///
1157/// # Examples
1158///
1159/// A locked standard input implements `BufRead`:
1160///
1161/// ```
1162/// use std::io;
1163/// use std::io::prelude::*;
1164///
1165/// let stdin = io::stdin();
1166/// for line in stdin.lock().lines() {
1167/// println!("{}", line.unwrap());
1168/// }
1169/// ```
1170///
1171/// If you have something that implements `Read`, you can use the [`BufReader`
1172/// type][bufreader] to turn it into a `BufRead`.
1173///
1174/// For example, [`File`][file] implements `Read`, but not `BufRead`.
1175/// `BufReader` to the rescue!
85aaf69f 1176///
c1a9b12d
SL
1177/// [bufreader]: struct.BufReader.html
1178/// [file]: ../fs/struct.File.html
1179///
1180/// ```
1181/// use std::io::{self, BufReader};
1182/// use std::io::prelude::*;
1183/// use std::fs::File;
1184///
1185/// # fn foo() -> io::Result<()> {
1186/// let f = try!(File::open("foo.txt"));
1187/// let f = BufReader::new(f);
1188///
1189/// for line in f.lines() {
1190/// println!("{}", line.unwrap());
1191/// }
1192///
1193/// # Ok(())
1194/// # }
1195/// ```
62682a34 1196///
c34b1796 1197#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 1198pub trait BufRead: Read {
1a4d82fc 1199 /// Fills the internal buffer of this object, returning the buffer contents.
85aaf69f 1200 ///
c1a9b12d
SL
1201 /// This function is a lower-level call. It needs to be paired with the
1202 /// [`consume`][consume] method to function properly. When calling this
1203 /// method, none of the contents will be "read" in the sense that later
1204 /// calling `read` may return the same contents. As such, `consume` must be
1205 /// called with the number of bytes that are consumed from this buffer to
1206 /// ensure that the bytes are never returned twice.
1a4d82fc 1207 ///
c1a9b12d 1208 /// [consume]: #tymethod.consume
1a4d82fc 1209 ///
85aaf69f
SL
1210 /// An empty buffer returned indicates that the stream has reached EOF.
1211 ///
1212 /// # Errors
1a4d82fc
JJ
1213 ///
1214 /// This function will return an I/O error if the underlying reader was
85aaf69f 1215 /// read, but returned an error.
c1a9b12d
SL
1216 ///
1217 /// # Examples
1218 ///
1219 /// A locked standard input implements `BufRead`:
1220 ///
1221 /// ```
1222 /// use std::io;
1223 /// use std::io::prelude::*;
1224 ///
1225 /// let stdin = io::stdin();
1226 /// let mut stdin = stdin.lock();
1227 ///
1228 /// // we can't have two `&mut` references to `stdin`, so use a block
1229 /// // to end the borrow early.
1230 /// let length = {
1231 /// let buffer = stdin.fill_buf().unwrap();
1232 ///
1233 /// // work with buffer
1234 /// println!("{:?}", buffer);
1235 ///
1236 /// buffer.len()
1237 /// };
1238 ///
1239 /// // ensure the bytes we worked with aren't returned again later
1240 /// stdin.consume(length);
1241 /// ```
c34b1796 1242 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 1243 fn fill_buf(&mut self) -> Result<&[u8]>;
1a4d82fc
JJ
1244
1245 /// Tells this buffer that `amt` bytes have been consumed from the buffer,
1246 /// so they should no longer be returned in calls to `read`.
c34b1796 1247 ///
c1a9b12d
SL
1248 /// This function is a lower-level call. It needs to be paired with the
1249 /// [`fill_buf`][fillbuf] method to function properly. This function does
1250 /// not perform any I/O, it simply informs this object that some amount of
1251 /// its buffer, returned from `fill_buf`, has been consumed and should no
1252 /// longer be returned. As such, this function may do odd things if
1253 /// `fill_buf` isn't called before calling it.
1254 ///
54a0048b 1255 /// [fillbuf]: #tymethod.fill_buf
c1a9b12d
SL
1256 ///
1257 /// The `amt` must be `<=` the number of bytes in the buffer returned by
1258 /// `fill_buf`.
c34b1796 1259 ///
c1a9b12d 1260 /// # Examples
c34b1796 1261 ///
c1a9b12d
SL
1262 /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf],
1263 /// that method's example includes an example of `consume()`.
c34b1796 1264 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 1265 fn consume(&mut self, amt: usize);
1a4d82fc 1266
c1a9b12d 1267 /// Read all bytes into `buf` until the delimiter `byte` is reached.
1a4d82fc 1268 ///
c1a9b12d
SL
1269 /// This function will read bytes from the underlying stream until the
1270 /// delimiter or EOF is found. Once found, all bytes up to, and including,
1271 /// the delimiter (if found) will be appended to `buf`.
1a4d82fc 1272 ///
c1a9b12d
SL
1273 /// If this reader is currently at EOF then this function will not modify
1274 /// `buf` and will return `Ok(n)` where `n` is the number of bytes which
1275 /// were read.
1a4d82fc 1276 ///
85aaf69f 1277 /// # Errors
1a4d82fc 1278 ///
85aaf69f
SL
1279 /// This function will ignore all instances of `ErrorKind::Interrupted` and
1280 /// will otherwise return any errors returned by `fill_buf`.
1a4d82fc 1281 ///
85aaf69f
SL
1282 /// If an I/O error is encountered then all bytes read so far will be
1283 /// present in `buf` and its length will have been adjusted appropriately.
c1a9b12d
SL
1284 ///
1285 /// # Examples
1286 ///
1287 /// A locked standard input implements `BufRead`. In this example, we'll
1288 /// read from standard input until we see an `a` byte.
1289 ///
1290 /// ```
1291 /// use std::io;
1292 /// use std::io::prelude::*;
1293 ///
1294 /// fn foo() -> io::Result<()> {
1295 /// let stdin = io::stdin();
1296 /// let mut stdin = stdin.lock();
1297 /// let mut buffer = Vec::new();
1298 ///
1299 /// try!(stdin.read_until(b'a', &mut buffer));
1300 ///
1301 /// println!("{:?}", buffer);
1302 /// # Ok(())
1303 /// # }
1304 /// ```
c34b1796
AL
1305 #[stable(feature = "rust1", since = "1.0.0")]
1306 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
85aaf69f 1307 read_until(self, byte, buf)
1a4d82fc
JJ
1308 }
1309
c1a9b12d
SL
1310 /// Read all bytes until a newline (the 0xA byte) is reached, and append
1311 /// them to the provided buffer.
1a4d82fc 1312 ///
c1a9b12d
SL
1313 /// This function will read bytes from the underlying stream until the
1314 /// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
1315 /// up to, and including, the delimiter (if found) will be appended to
1316 /// `buf`.
1a4d82fc 1317 ///
85aaf69f 1318 /// If this reader is currently at EOF then this function will not modify
c34b1796
AL
1319 /// `buf` and will return `Ok(n)` where `n` is the number of bytes which
1320 /// were read.
85aaf69f
SL
1321 ///
1322 /// # Errors
1323 ///
1324 /// This function has the same error semantics as `read_until` and will also
1325 /// return an error if the read bytes are not valid UTF-8. If an I/O error
1326 /// is encountered then `buf` may contain some bytes already read in the
1327 /// event that all data read so far was valid UTF-8.
c1a9b12d
SL
1328 ///
1329 /// # Examples
1330 ///
1331 /// A locked standard input implements `BufRead`. In this example, we'll
1332 /// read all of the lines from standard input. If we were to do this in
1333 /// an actual project, the [`lines()`][lines] method would be easier, of
1334 /// course.
1335 ///
1336 /// [lines]: #method.lines
1337 ///
1338 /// ```
1339 /// use std::io;
1340 /// use std::io::prelude::*;
1341 ///
1342 /// let stdin = io::stdin();
1343 /// let mut stdin = stdin.lock();
1344 /// let mut buffer = String::new();
1345 ///
1346 /// while stdin.read_line(&mut buffer).unwrap() > 0 {
1347 /// // work with buffer
1348 /// println!("{:?}", buffer);
1349 ///
1350 /// buffer.clear();
1351 /// }
1352 /// ```
c34b1796
AL
1353 #[stable(feature = "rust1", since = "1.0.0")]
1354 fn read_line(&mut self, buf: &mut String) -> Result<usize> {
85aaf69f
SL
1355 // Note that we are not calling the `.read_until` method here, but
1356 // rather our hardcoded implementation. For more details as to why, see
1357 // the comments in `read_to_end`.
1358 append_to_string(buf, |b| read_until(self, b'\n', b))
1a4d82fc 1359 }
1a4d82fc 1360
85aaf69f
SL
1361 /// Returns an iterator over the contents of this reader split on the byte
1362 /// `byte`.
1a4d82fc 1363 ///
85aaf69f
SL
1364 /// The iterator returned from this function will return instances of
1365 /// `io::Result<Vec<u8>>`. Each vector returned will *not* have the
1366 /// delimiter byte at the end.
1a4d82fc 1367 ///
85aaf69f
SL
1368 /// This function will yield errors whenever `read_until` would have also
1369 /// yielded an error.
c1a9b12d
SL
1370 ///
1371 /// # Examples
1372 ///
1373 /// A locked standard input implements `BufRead`. In this example, we'll
1374 /// read some input from standard input, splitting on commas.
1375 ///
1376 /// ```
1377 /// use std::io;
1378 /// use std::io::prelude::*;
1379 ///
1380 /// let stdin = io::stdin();
1381 ///
1382 /// for content in stdin.lock().split(b',') {
1383 /// println!("{:?}", content.unwrap());
1384 /// }
1385 /// ```
c34b1796
AL
1386 #[stable(feature = "rust1", since = "1.0.0")]
1387 fn split(self, byte: u8) -> Split<Self> where Self: Sized {
85aaf69f
SL
1388 Split { buf: self, delim: byte }
1389 }
1a4d82fc 1390
85aaf69f 1391 /// Returns an iterator over the lines of this reader.
1a4d82fc 1392 ///
85aaf69f
SL
1393 /// The iterator returned from this function will yield instances of
1394 /// `io::Result<String>`. Each string returned will *not* have a newline
e9174d1e 1395 /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
c1a9b12d
SL
1396 ///
1397 /// # Examples
1398 ///
1399 /// A locked standard input implements `BufRead`:
1400 ///
1401 /// ```
1402 /// use std::io;
1403 /// use std::io::prelude::*;
1404 ///
1405 /// let stdin = io::stdin();
1406 ///
1407 /// for line in stdin.lock().lines() {
1408 /// println!("{}", line.unwrap());
1409 /// }
1410 /// ```
c34b1796
AL
1411 #[stable(feature = "rust1", since = "1.0.0")]
1412 fn lines(self) -> Lines<Self> where Self: Sized {
85aaf69f
SL
1413 Lines { buf: self }
1414 }
1415}
1416
c1a9b12d
SL
1417/// Adaptor to chain together two readers.
1418///
1419/// This struct is generally created by calling [`chain()`][chain] on a reader.
1420/// Please see the documentation of `chain()` for more details.
85aaf69f 1421///
c1a9b12d 1422/// [chain]: trait.Read.html#method.chain
c34b1796 1423#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1424pub struct Chain<T, U> {
1425 first: T,
1426 second: U,
1427 done_first: bool,
1a4d82fc
JJ
1428}
1429
c34b1796 1430#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1431impl<T: Read, U: Read> Read for Chain<T, U> {
1432 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
1433 if !self.done_first {
54a0048b 1434 match self.first.read(buf)? {
85aaf69f
SL
1435 0 => { self.done_first = true; }
1436 n => return Ok(n),
1437 }
1438 }
1439 self.second.read(buf)
1440 }
1a4d82fc
JJ
1441}
1442
54a0048b
SL
1443#[stable(feature = "chain_bufread", since = "1.9.0")]
1444impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
1445 fn fill_buf(&mut self) -> Result<&[u8]> {
1446 if !self.done_first {
1447 match self.first.fill_buf()? {
1448 buf if buf.len() == 0 => { self.done_first = true; }
1449 buf => return Ok(buf),
1450 }
1451 }
1452 self.second.fill_buf()
1453 }
1454
1455 fn consume(&mut self, amt: usize) {
1456 if !self.done_first {
1457 self.first.consume(amt)
1458 } else {
1459 self.second.consume(amt)
1460 }
1461 }
1462}
1463
85aaf69f 1464/// Reader adaptor which limits the bytes read from an underlying reader.
1a4d82fc 1465///
c1a9b12d
SL
1466/// This struct is generally created by calling [`take()`][take] on a reader.
1467/// Please see the documentation of `take()` for more details.
1468///
1469/// [take]: trait.Read.html#method.take
c34b1796 1470#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1471pub struct Take<T> {
1472 inner: T,
1473 limit: u64,
1a4d82fc
JJ
1474}
1475
85aaf69f
SL
1476impl<T> Take<T> {
1477 /// Returns the number of bytes that can be read before this instance will
1478 /// return EOF.
1a4d82fc 1479 ///
85aaf69f 1480 /// # Note
1a4d82fc 1481 ///
85aaf69f
SL
1482 /// This instance may reach EOF after reading fewer bytes than indicated by
1483 /// this method if the underlying `Read` instance reaches EOF.
5bcae85e
SL
1484 ///
1485 /// # Examples
1486 ///
1487 /// ```
1488 /// use std::io;
1489 /// use std::io::prelude::*;
1490 /// use std::fs::File;
1491 ///
1492 /// # fn foo() -> io::Result<()> {
1493 /// let f = try!(File::open("foo.txt"));
1494 ///
1495 /// // read at most five bytes
1496 /// let handle = f.take(5);
1497 ///
1498 /// println!("limit: {}", handle.limit());
1499 /// # Ok(())
1500 /// # }
1501 /// ```
c34b1796 1502 #[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 1503 pub fn limit(&self) -> u64 { self.limit }
9e0c209e
SL
1504
1505 /// Consumes the `Take`, returning the wrapped reader.
1506 ///
1507 /// # Examples
1508 ///
1509 /// ```
1510 /// #![feature(io_take_into_inner)]
1511 ///
1512 /// use std::io;
1513 /// use std::io::prelude::*;
1514 /// use std::fs::File;
1515 ///
1516 /// # fn foo() -> io::Result<()> {
1517 /// let mut file = try!(File::open("foo.txt"));
1518 ///
1519 /// let mut buffer = [0; 5];
1520 /// let mut handle = file.take(5);
1521 /// try!(handle.read(&mut buffer));
1522 ///
1523 /// let file = handle.into_inner();
1524 /// # Ok(())
1525 /// # }
1526 /// ```
1527 #[unstable(feature = "io_take_into_inner", issue = "0")]
1528 pub fn into_inner(self) -> T {
1529 self.inner
1530 }
85aaf69f 1531}
1a4d82fc 1532
c34b1796 1533#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1534impl<T: Read> Read for Take<T> {
1535 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
c34b1796
AL
1536 // Don't call into inner reader at all at EOF because it may still block
1537 if self.limit == 0 {
1538 return Ok(0);
1539 }
1540
85aaf69f 1541 let max = cmp::min(buf.len() as u64, self.limit) as usize;
54a0048b 1542 let n = self.inner.read(&mut buf[..max])?;
85aaf69f
SL
1543 self.limit -= n as u64;
1544 Ok(n)
1a4d82fc
JJ
1545 }
1546}
1547
c34b1796
AL
1548#[stable(feature = "rust1", since = "1.0.0")]
1549impl<T: BufRead> BufRead for Take<T> {
1550 fn fill_buf(&mut self) -> Result<&[u8]> {
a7813a04
XL
1551 // Don't call into inner reader at all at EOF because it may still block
1552 if self.limit == 0 {
1553 return Ok(&[]);
1554 }
1555
54a0048b 1556 let buf = self.inner.fill_buf()?;
c34b1796
AL
1557 let cap = cmp::min(buf.len() as u64, self.limit) as usize;
1558 Ok(&buf[..cap])
1559 }
1560
1561 fn consume(&mut self, amt: usize) {
1562 // Don't let callers reset the limit by passing an overlarge value
1563 let amt = cmp::min(amt as u64, self.limit) as usize;
1564 self.limit -= amt as u64;
1565 self.inner.consume(amt);
1566 }
1567}
1568
5bcae85e
SL
1569fn read_one_byte(reader: &mut Read) -> Option<Result<u8>> {
1570 let mut buf = [0];
1571 loop {
1572 return match reader.read(&mut buf) {
1573 Ok(0) => None,
1574 Ok(..) => Some(Ok(buf[0])),
1575 Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
1576 Err(e) => Some(Err(e)),
1577 };
1578 }
1579}
1580
c1a9b12d
SL
1581/// An iterator over `u8` values of a reader.
1582///
1583/// This struct is generally created by calling [`bytes()`][bytes] on a reader.
1584/// Please see the documentation of `bytes()` for more details.
1a4d82fc 1585///
c1a9b12d 1586/// [bytes]: trait.Read.html#method.bytes
c34b1796 1587#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1588pub struct Bytes<R> {
1589 inner: R,
1a4d82fc
JJ
1590}
1591
c34b1796 1592#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1593impl<R: Read> Iterator for Bytes<R> {
1594 type Item = Result<u8>;
1a4d82fc 1595
85aaf69f 1596 fn next(&mut self) -> Option<Result<u8>> {
5bcae85e 1597 read_one_byte(&mut self.inner)
85aaf69f 1598 }
1a4d82fc
JJ
1599}
1600
c1a9b12d 1601/// An iterator over the `char`s of a reader.
1a4d82fc 1602///
c1a9b12d
SL
1603/// This struct is generally created by calling [`chars()`][chars] on a reader.
1604/// Please see the documentation of `chars()` for more details.
1605///
1606/// [chars]: trait.Read.html#method.chars
e9174d1e
SL
1607#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1608 issue = "27802")]
85aaf69f
SL
1609pub struct Chars<R> {
1610 inner: R,
1a4d82fc
JJ
1611}
1612
85aaf69f
SL
1613/// An enumeration of possible errors that can be generated from the `Chars`
1614/// adapter.
c34b1796 1615#[derive(Debug)]
e9174d1e
SL
1616#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1617 issue = "27802")]
85aaf69f
SL
1618pub enum CharsError {
1619 /// Variant representing that the underlying stream was read successfully
1620 /// but it did not contain valid utf8 data.
1621 NotUtf8,
1a4d82fc 1622
85aaf69f
SL
1623 /// Variant representing that an I/O error occurred.
1624 Other(Error),
1a4d82fc
JJ
1625}
1626
e9174d1e
SL
1627#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1628 issue = "27802")]
85aaf69f
SL
1629impl<R: Read> Iterator for Chars<R> {
1630 type Item = result::Result<char, CharsError>;
1a4d82fc 1631
85aaf69f 1632 fn next(&mut self) -> Option<result::Result<char, CharsError>> {
5bcae85e
SL
1633 let first_byte = match read_one_byte(&mut self.inner) {
1634 None => return None,
1635 Some(Ok(b)) => b,
1636 Some(Err(e)) => return Some(Err(CharsError::Other(e))),
85aaf69f
SL
1637 };
1638 let width = core_str::utf8_char_width(first_byte);
1639 if width == 1 { return Some(Ok(first_byte as char)) }
1640 if width == 0 { return Some(Err(CharsError::NotUtf8)) }
1641 let mut buf = [first_byte, 0, 0, 0];
1642 {
1643 let mut start = 1;
1644 while start < width {
1645 match self.inner.read(&mut buf[start..width]) {
1646 Ok(0) => return Some(Err(CharsError::NotUtf8)),
1647 Ok(n) => start += n,
5bcae85e 1648 Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
85aaf69f
SL
1649 Err(e) => return Some(Err(CharsError::Other(e))),
1650 }
1651 }
1652 }
1653 Some(match str::from_utf8(&buf[..width]).ok() {
54a0048b 1654 Some(s) => Ok(s.chars().next().unwrap()),
85aaf69f
SL
1655 None => Err(CharsError::NotUtf8),
1656 })
1657 }
1a4d82fc
JJ
1658}
1659
e9174d1e
SL
1660#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1661 issue = "27802")]
c34b1796 1662impl std_error::Error for CharsError {
85aaf69f
SL
1663 fn description(&self) -> &str {
1664 match *self {
1665 CharsError::NotUtf8 => "invalid utf8 encoding",
c34b1796 1666 CharsError::Other(ref e) => std_error::Error::description(e),
85aaf69f
SL
1667 }
1668 }
c34b1796 1669 fn cause(&self) -> Option<&std_error::Error> {
85aaf69f
SL
1670 match *self {
1671 CharsError::NotUtf8 => None,
1672 CharsError::Other(ref e) => e.cause(),
1673 }
1a4d82fc
JJ
1674 }
1675}
1676
e9174d1e
SL
1677#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1678 issue = "27802")]
85aaf69f 1679impl fmt::Display for CharsError {
1a4d82fc 1680 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85aaf69f
SL
1681 match *self {
1682 CharsError::NotUtf8 => {
1683 "byte stream did not contain valid utf8".fmt(f)
1684 }
1685 CharsError::Other(ref e) => e.fmt(f),
1686 }
1a4d82fc
JJ
1687 }
1688}
1689
85aaf69f
SL
1690/// An iterator over the contents of an instance of `BufRead` split on a
1691/// particular byte.
1692///
c1a9b12d
SL
1693/// This struct is generally created by calling [`split()`][split] on a
1694/// `BufRead`. Please see the documentation of `split()` for more details.
1695///
1696/// [split]: trait.BufRead.html#method.split
c34b1796 1697#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1698pub struct Split<B> {
1699 buf: B,
1700 delim: u8,
1701}
1a4d82fc 1702
c34b1796 1703#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1704impl<B: BufRead> Iterator for Split<B> {
1705 type Item = Result<Vec<u8>>;
1a4d82fc 1706
85aaf69f
SL
1707 fn next(&mut self) -> Option<Result<Vec<u8>>> {
1708 let mut buf = Vec::new();
1709 match self.buf.read_until(self.delim, &mut buf) {
c34b1796
AL
1710 Ok(0) => None,
1711 Ok(_n) => {
85aaf69f
SL
1712 if buf[buf.len() - 1] == self.delim {
1713 buf.pop();
1714 }
1715 Some(Ok(buf))
1716 }
1717 Err(e) => Some(Err(e))
1a4d82fc
JJ
1718 }
1719 }
85aaf69f
SL
1720}
1721
c1a9b12d 1722/// An iterator over the lines of an instance of `BufRead`.
85aaf69f 1723///
c1a9b12d
SL
1724/// This struct is generally created by calling [`lines()`][lines] on a
1725/// `BufRead`. Please see the documentation of `lines()` for more details.
1726///
1727/// [lines]: trait.BufRead.html#method.lines
c34b1796 1728#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1729pub struct Lines<B> {
1730 buf: B,
1731}
1732
c34b1796 1733#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f
SL
1734impl<B: BufRead> Iterator for Lines<B> {
1735 type Item = Result<String>;
1a4d82fc 1736
85aaf69f
SL
1737 fn next(&mut self) -> Option<Result<String>> {
1738 let mut buf = String::new();
1739 match self.buf.read_line(&mut buf) {
c34b1796
AL
1740 Ok(0) => None,
1741 Ok(_n) => {
85aaf69f
SL
1742 if buf.ends_with("\n") {
1743 buf.pop();
e9174d1e
SL
1744 if buf.ends_with("\r") {
1745 buf.pop();
1746 }
1a4d82fc 1747 }
85aaf69f 1748 Some(Ok(buf))
1a4d82fc 1749 }
85aaf69f 1750 Err(e) => Some(Err(e))
1a4d82fc
JJ
1751 }
1752 }
85aaf69f
SL
1753}
1754
1755#[cfg(test)]
1756mod tests {
85aaf69f 1757 use io::prelude::*;
c34b1796 1758 use io;
85aaf69f 1759 use super::Cursor;
c1a9b12d
SL
1760 use test;
1761 use super::repeat;
1a4d82fc
JJ
1762
1763 #[test]
85aaf69f 1764 fn read_until() {
c34b1796 1765 let mut buf = Cursor::new(&b"12"[..]);
85aaf69f 1766 let mut v = Vec::new();
c34b1796 1767 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
85aaf69f
SL
1768 assert_eq!(v, b"12");
1769
c34b1796 1770 let mut buf = Cursor::new(&b"1233"[..]);
85aaf69f 1771 let mut v = Vec::new();
c34b1796 1772 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
85aaf69f
SL
1773 assert_eq!(v, b"123");
1774 v.truncate(0);
c34b1796 1775 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
85aaf69f
SL
1776 assert_eq!(v, b"3");
1777 v.truncate(0);
c34b1796 1778 assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
85aaf69f 1779 assert_eq!(v, []);
1a4d82fc
JJ
1780 }
1781
1782 #[test]
85aaf69f 1783 fn split() {
c34b1796 1784 let buf = Cursor::new(&b"12"[..]);
85aaf69f 1785 let mut s = buf.split(b'3');
c34b1796
AL
1786 assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
1787 assert!(s.next().is_none());
1a4d82fc 1788
c34b1796 1789 let buf = Cursor::new(&b"1233"[..]);
85aaf69f 1790 let mut s = buf.split(b'3');
c34b1796
AL
1791 assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
1792 assert_eq!(s.next().unwrap().unwrap(), vec![]);
1793 assert!(s.next().is_none());
85aaf69f 1794 }
1a4d82fc 1795
85aaf69f
SL
1796 #[test]
1797 fn read_line() {
c34b1796 1798 let mut buf = Cursor::new(&b"12"[..]);
85aaf69f 1799 let mut v = String::new();
c34b1796 1800 assert_eq!(buf.read_line(&mut v).unwrap(), 2);
85aaf69f
SL
1801 assert_eq!(v, "12");
1802
c34b1796 1803 let mut buf = Cursor::new(&b"12\n\n"[..]);
85aaf69f 1804 let mut v = String::new();
c34b1796 1805 assert_eq!(buf.read_line(&mut v).unwrap(), 3);
85aaf69f
SL
1806 assert_eq!(v, "12\n");
1807 v.truncate(0);
c34b1796 1808 assert_eq!(buf.read_line(&mut v).unwrap(), 1);
85aaf69f
SL
1809 assert_eq!(v, "\n");
1810 v.truncate(0);
c34b1796 1811 assert_eq!(buf.read_line(&mut v).unwrap(), 0);
85aaf69f
SL
1812 assert_eq!(v, "");
1813 }
1a4d82fc 1814
85aaf69f
SL
1815 #[test]
1816 fn lines() {
e9174d1e 1817 let buf = Cursor::new(&b"12\r"[..]);
85aaf69f 1818 let mut s = buf.lines();
e9174d1e 1819 assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
c34b1796 1820 assert!(s.next().is_none());
1a4d82fc 1821
e9174d1e 1822 let buf = Cursor::new(&b"12\r\n\n"[..]);
85aaf69f 1823 let mut s = buf.lines();
c34b1796
AL
1824 assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
1825 assert_eq!(s.next().unwrap().unwrap(), "".to_string());
1826 assert!(s.next().is_none());
1a4d82fc
JJ
1827 }
1828
1829 #[test]
85aaf69f 1830 fn read_to_end() {
c34b1796 1831 let mut c = Cursor::new(&b""[..]);
85aaf69f 1832 let mut v = Vec::new();
c34b1796 1833 assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
85aaf69f
SL
1834 assert_eq!(v, []);
1835
c34b1796 1836 let mut c = Cursor::new(&b"1"[..]);
85aaf69f 1837 let mut v = Vec::new();
c34b1796 1838 assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
85aaf69f 1839 assert_eq!(v, b"1");
c1a9b12d
SL
1840
1841 let cap = 1024 * 1024;
1842 let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
1843 let mut v = Vec::new();
1844 let (a, b) = data.split_at(data.len() / 2);
1845 assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
1846 assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
1847 assert_eq!(v, data);
1a4d82fc
JJ
1848 }
1849
85aaf69f
SL
1850 #[test]
1851 fn read_to_string() {
c34b1796 1852 let mut c = Cursor::new(&b""[..]);
85aaf69f 1853 let mut v = String::new();
c34b1796 1854 assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
85aaf69f
SL
1855 assert_eq!(v, "");
1856
c34b1796 1857 let mut c = Cursor::new(&b"1"[..]);
85aaf69f 1858 let mut v = String::new();
c34b1796 1859 assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
85aaf69f
SL
1860 assert_eq!(v, "1");
1861
c34b1796 1862 let mut c = Cursor::new(&b"\xff"[..]);
85aaf69f
SL
1863 let mut v = String::new();
1864 assert!(c.read_to_string(&mut v).is_err());
1a4d82fc 1865 }
c34b1796 1866
e9174d1e
SL
1867 #[test]
1868 fn read_exact() {
1869 let mut buf = [0; 4];
1870
1871 let mut c = Cursor::new(&b""[..]);
1872 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
92a42be0 1873 io::ErrorKind::UnexpectedEof);
e9174d1e
SL
1874
1875 let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
1876 c.read_exact(&mut buf).unwrap();
1877 assert_eq!(&buf, b"1234");
1878 c.read_exact(&mut buf).unwrap();
1879 assert_eq!(&buf, b"5678");
1880 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
92a42be0 1881 io::ErrorKind::UnexpectedEof);
e9174d1e
SL
1882 }
1883
1884 #[test]
1885 fn read_exact_slice() {
1886 let mut buf = [0; 4];
1887
1888 let mut c = &b""[..];
1889 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
92a42be0 1890 io::ErrorKind::UnexpectedEof);
e9174d1e
SL
1891
1892 let mut c = &b"123"[..];
1893 assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
92a42be0 1894 io::ErrorKind::UnexpectedEof);
e9174d1e
SL
1895 // make sure the optimized (early returning) method is being used
1896 assert_eq!(&buf, &[0; 4]);
1897
1898 let mut c = &b"1234"[..];
1899 c.read_exact(&mut buf).unwrap();
1900 assert_eq!(&buf, b"1234");
1901
1902 let mut c = &b"56789"[..];
1903 c.read_exact(&mut buf).unwrap();
1904 assert_eq!(&buf, b"5678");
1905 assert_eq!(c, b"9");
1906 }
1907
c34b1796
AL
1908 #[test]
1909 fn take_eof() {
1910 struct R;
1911
1912 impl Read for R {
1913 fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
1914 Err(io::Error::new(io::ErrorKind::Other, ""))
1915 }
1916 }
a7813a04
XL
1917 impl BufRead for R {
1918 fn fill_buf(&mut self) -> io::Result<&[u8]> {
1919 Err(io::Error::new(io::ErrorKind::Other, ""))
1920 }
1921 fn consume(&mut self, _amt: usize) { }
1922 }
c34b1796
AL
1923
1924 let mut buf = [0; 1];
1925 assert_eq!(0, R.take(0).read(&mut buf).unwrap());
a7813a04 1926 assert_eq!(b"", R.take(0).fill_buf().unwrap());
c34b1796 1927 }
c1a9b12d 1928
54a0048b
SL
1929 fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
1930 let mut cat = Vec::new();
1931 loop {
1932 let consume = {
1933 let buf1 = br1.fill_buf().unwrap();
1934 let buf2 = br2.fill_buf().unwrap();
1935 let minlen = if buf1.len() < buf2.len() { buf1.len() } else { buf2.len() };
1936 assert_eq!(buf1[..minlen], buf2[..minlen]);
1937 cat.extend_from_slice(&buf1[..minlen]);
1938 minlen
1939 };
1940 if consume == 0 {
1941 break;
1942 }
1943 br1.consume(consume);
1944 br2.consume(consume);
1945 }
1946 assert_eq!(br1.fill_buf().unwrap().len(), 0);
1947 assert_eq!(br2.fill_buf().unwrap().len(), 0);
1948 assert_eq!(&cat[..], &exp[..])
1949 }
1950
1951 #[test]
1952 fn chain_bufread() {
1953 let testdata = b"ABCDEFGHIJKL";
1954 let chain1 = (&testdata[..3]).chain(&testdata[3..6])
1955 .chain(&testdata[6..9])
1956 .chain(&testdata[9..]);
1957 let chain2 = (&testdata[..4]).chain(&testdata[4..8])
1958 .chain(&testdata[8..]);
1959 cmp_bufread(chain1, chain2, &testdata[..]);
1960 }
1961
c1a9b12d
SL
1962 #[bench]
1963 fn bench_read_to_end(b: &mut test::Bencher) {
1964 b.iter(|| {
1965 let mut lr = repeat(1).take(10000000);
1966 let mut vec = Vec::with_capacity(1024);
9cc50fc6 1967 super::read_to_end(&mut lr, &mut vec)
c1a9b12d
SL
1968 });
1969 }
1a4d82fc 1970}