1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
11 //! Traits, helpers, and type definitions for core I/O functionality.
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.
18 //! [read]: trait.Read.html
19 //! [write]: trait.Write.html
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:
31 //! use std::io::prelude::*;
32 //! use std::fs::File;
34 //! # fn foo() -> io::Result<()> {
35 //! let mut f = try!(File::open("foo.txt"));
36 //! let mut buffer = [0; 10];
38 //! // read up to 10 bytes
39 //! try!(f.read(&mut buffer));
41 //! println!("The bytes: {:?}", buffer);
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!
50 //! ## Seek and BufRead
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
59 //! use std::io::prelude::*;
60 //! use std::io::SeekFrom;
61 //! use std::fs::File;
63 //! # fn foo() -> io::Result<()> {
64 //! let mut f = try!(File::open("foo.txt"));
65 //! let mut buffer = [0; 10];
67 //! // skip to the last 10 bytes of the file
68 //! try!(f.seek(SeekFrom::End(-10)));
70 //! // read up to 10 bytes
71 //! try!(f.read(&mut buffer));
73 //! println!("The bytes: {:?}", buffer);
78 //! [seek]: trait.Seek.html
79 //! [bufread]: trait.BufRead.html
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!
84 //! ## BufReader and BufWriter
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.
92 //! For example, `BufReader` works with the `BufRead` trait to add extra
93 //! methods to any reader:
97 //! use std::io::prelude::*;
98 //! use std::io::BufReader;
99 //! use std::fs::File;
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();
106 //! // read a line into buffer
107 //! try!(reader.read_line(&mut buffer));
109 //! println!("{}", buffer);
114 //! `BufWriter` doesn't add any new ways of writing; it just buffers every call
115 //! to [`write()`][write()]:
119 //! use std::io::prelude::*;
120 //! use std::io::BufWriter;
121 //! use std::fs::File;
123 //! # fn foo() -> io::Result<()> {
124 //! let f = try!(File::create("foo.txt"));
126 //! let mut writer = BufWriter::new(f);
128 //! // write a byte to the buffer
129 //! try!(writer.write(&[42]));
131 //! } // the buffer is flushed once writer goes out of scope
137 //! [write()]: trait.Write.html#tymethod.write
139 //! ## Standard input and output
141 //! A very common source of input is standard input:
146 //! # fn foo() -> io::Result<()> {
147 //! let mut input = String::new();
149 //! try!(io::stdin().read_line(&mut input));
151 //! println!("You typed: {}", input.trim());
156 //! And a very common source of output is standard output:
160 //! use std::io::prelude::*;
162 //! # fn foo() -> io::Result<()> {
163 //! try!(io::stdout().write(&[42]));
168 //! Of course, using `io::stdout()` directly is less common than something like
171 //! ## Iterator types
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
179 //! use std::io::prelude::*;
180 //! use std::io::BufReader;
181 //! use std::fs::File;
183 //! # fn foo() -> io::Result<()> {
184 //! let f = try!(File::open("foo.txt"));
185 //! let mut reader = BufReader::new(f);
187 //! for line in reader.lines() {
188 //! let line = try!(line);
189 //! println!("{}", line);
198 //! There are a number of [functions][functions] that offer access to various
199 //! features. For example, we can use three of these functions to copy everything
200 //! from standard input to standard output:
205 //! # fn foo() -> io::Result<()> {
206 //! try!(io::copy(&mut io::stdin(), &mut io::stdout()));
211 //! [functions]: #functions
215 //! Last, but certainly not least, is [`io::Result`][result]. This type is used
216 //! as the return type of many `std::io` functions that can cause an error, and
217 //! can be returned from your own functions as well. Many of the examples in this
218 //! module use the [`try!`][try] macro:
223 //! fn read_input() -> io::Result<()> {
224 //! let mut input = String::new();
226 //! try!(io::stdin().read_line(&mut input));
228 //! println!("You typed: {}", input.trim());
234 //! The return type of `read_input()`, `io::Result<()>`, is a very common type
235 //! for functions which don't have a 'real' return value, but do want to return
236 //! errors if they happen. In this case, the only purpose of this function is
237 //! to read the line and print it, so we use `()`.
239 //! [result]: type.Result.html
240 //! [try]: ../macro.try!.html
242 //! ## Platform-specific behavior
244 //! Many I/O functions throughout the standard library are documented to indicate
245 //! what various library or syscalls they are delegated to. This is done to help
246 //! applications both understand what's happening under the hood as well as investigate
247 //! any possibly unclear semantics. Note, however, that this is informative, not a binding
248 //! contract. The implementation of many of these functions are subject to change over
249 //! time and may call fewer or more syscalls/library functions.
251 #![stable(feature = "rust1", since = "1.0.0")]
254 use rustc_unicode
::str as core_str
;
255 use error
as std_error
;
257 use iter
::{Iterator}
;
259 use ops
::{Drop, FnOnce}
;
260 use option
::Option
::{self, Some, None}
;
261 use result
::Result
::{Ok, Err}
;
268 #[stable(feature = "rust1", since = "1.0.0")]
269 pub use self::buffered
::{BufReader, BufWriter, LineWriter}
;
270 #[stable(feature = "rust1", since = "1.0.0")]
271 pub use self::buffered
::IntoInnerError
;
272 #[stable(feature = "rust1", since = "1.0.0")]
273 pub use self::cursor
::Cursor
;
274 #[stable(feature = "rust1", since = "1.0.0")]
275 pub use self::error
::{Result, Error, ErrorKind}
;
276 #[stable(feature = "rust1", since = "1.0.0")]
277 pub use self::util
::{copy, sink, Sink, empty, Empty, repeat, Repeat}
;
278 #[stable(feature = "rust1", since = "1.0.0")]
279 pub use self::stdio
::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}
;
280 #[stable(feature = "rust1", since = "1.0.0")]
281 pub use self::stdio
::{StdoutLock, StderrLock, StdinLock}
;
282 #[unstable(feature = "libstd_io_internals", issue = "0")]
283 #[doc(no_inline, hidden)]
284 pub use self::stdio
::{set_panic, set_print}
;
295 const DEFAULT_BUF_SIZE
: usize = 8 * 1024;
297 // A few methods below (read_to_string, read_line) will append data into a
298 // `String` buffer, but we need to be pretty careful when doing this. The
299 // implementation will just call `.as_mut_vec()` and then delegate to a
300 // byte-oriented reading method, but we must ensure that when returning we never
301 // leave `buf` in a state such that it contains invalid UTF-8 in its bounds.
303 // To this end, we use an RAII guard (to protect against panics) which updates
304 // the length of the string when it is dropped. This guard initially truncates
305 // the string to the prior length and only after we've validated that the
306 // new contents are valid UTF-8 do we allow it to set a longer length.
308 // The unsafety in this function is twofold:
310 // 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
312 // 2. We're passing a raw buffer to the function `f`, and it is expected that
313 // the function only *appends* bytes to the buffer. We'll get undefined
314 // behavior if existing bytes are overwritten to have non-UTF-8 data.
315 fn append_to_string
<F
>(buf
: &mut String
, f
: F
) -> Result
<usize>
316 where F
: FnOnce(&mut Vec
<u8>) -> Result
<usize>
318 struct Guard
<'a
> { s: &'a mut Vec<u8>, len: usize }
319 impl<'a
> Drop
for Guard
<'a
> {
321 unsafe { self.s.set_len(self.len); }
326 let mut g
= Guard { len: buf.len(), s: buf.as_mut_vec() }
;
328 if str::from_utf8(&g
.s
[g
.len
..]).is_err() {
330 Err(Error
::new(ErrorKind
::InvalidData
,
331 "stream did not contain valid UTF-8"))
340 // This uses an adaptive system to extend the vector when it fills. We want to
341 // avoid paying to allocate and zero a huge chunk of memory if the reader only
342 // has 4 bytes while still making large reads if the reader does have a ton
343 // of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
344 // time is 4,500 times (!) slower than this if the reader has a very small
345 // amount of data to return.
346 fn read_to_end
<R
: Read
+ ?Sized
>(r
: &mut R
, buf
: &mut Vec
<u8>) -> Result
<usize> {
347 let start_len
= buf
.len();
348 let mut len
= start_len
;
349 let mut new_write_size
= 16;
352 if len
== buf
.len() {
353 if new_write_size
< DEFAULT_BUF_SIZE
{
356 buf
.resize(len
+ new_write_size
, 0);
359 match r
.read(&mut buf
[len
..]) {
361 ret
= Ok(len
- start_len
);
365 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> {}
377 /// The `Read` trait allows for reading bytes from a source.
379 /// Implementors of the `Read` trait are sometimes called 'readers'.
381 /// Readers are defined by one required method, `read()`. Each call to `read`
382 /// will attempt to pull bytes from this source into a provided buffer. A
383 /// number of other methods are implemented in terms of `read()`, giving
384 /// implementors a number of ways to read bytes while only needing to implement
387 /// Readers are intended to be composable with one another. Many implementors
388 /// throughout `std::io` take and provide types which implement the `Read`
391 /// Please note that each call to `read` may involve a system call, and
392 /// therefore, using something that implements [`BufRead`][bufread], such as
393 /// [`BufReader`][bufreader], will be more efficient.
395 /// [bufread]: trait.BufRead.html
396 /// [bufreader]: struct.BufReader.html
400 /// [`File`][file]s implement `Read`:
402 /// [file]: ../fs/struct.File.html
406 /// use std::io::prelude::*;
407 /// use std::fs::File;
409 /// # fn foo() -> io::Result<()> {
410 /// let mut f = try!(File::open("foo.txt"));
411 /// let mut buffer = [0; 10];
413 /// // read up to 10 bytes
414 /// try!(f.read(&mut buffer));
416 /// let mut buffer = vec![0; 10];
417 /// // read the whole file
418 /// try!(f.read_to_end(&mut buffer));
420 /// // read into a String, so that you don't need to do the conversion.
421 /// let mut buffer = String::new();
422 /// try!(f.read_to_string(&mut buffer));
424 /// // and more! See the other methods for more details.
428 #[stable(feature = "rust1", since = "1.0.0")]
430 /// Pull some bytes from this source into the specified buffer, returning
431 /// how many bytes were read.
433 /// This function does not provide any guarantees about whether it blocks
434 /// waiting for data, but if an object needs to block for a read but cannot
435 /// it will typically signal this via an `Err` return value.
437 /// If the return value of this method is `Ok(n)`, then it must be
438 /// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
439 /// that the buffer `buf` has been filled in with `n` bytes of data from this
440 /// source. If `n` is `0`, then it can indicate one of two scenarios:
442 /// 1. This reader has reached its "end of file" and will likely no longer
443 /// be able to produce bytes. Note that this does not mean that the
444 /// reader will *always* no longer be able to produce bytes.
445 /// 2. The buffer specified was 0 bytes in length.
447 /// No guarantees are provided about the contents of `buf` when this
448 /// function is called, implementations cannot rely on any property of the
449 /// contents of `buf` being true. It is recommended that implementations
450 /// only write data to `buf` instead of reading its contents.
454 /// If this function encounters any form of I/O or other error, an error
455 /// variant will be returned. If an error is returned then it must be
456 /// guaranteed that no bytes were read.
460 /// [`File`][file]s implement `Read`:
462 /// [file]: ../fs/struct.File.html
466 /// use std::io::prelude::*;
467 /// use std::fs::File;
469 /// # fn foo() -> io::Result<()> {
470 /// let mut f = try!(File::open("foo.txt"));
471 /// let mut buffer = [0; 10];
474 /// try!(f.read(&mut buffer[..]));
478 #[stable(feature = "rust1", since = "1.0.0")]
479 fn read(&mut self, buf
: &mut [u8]) -> Result
<usize>;
481 /// Read all bytes until EOF in this source, placing them into `buf`.
483 /// All bytes read from this source will be appended to the specified buffer
484 /// `buf`. This function will continuously call `read` to append more data to
485 /// `buf` until `read` returns either `Ok(0)` or an error of
486 /// non-`ErrorKind::Interrupted` kind.
488 /// If successful, this function will return the total number of bytes read.
492 /// If this function encounters an error of the kind
493 /// `ErrorKind::Interrupted` then the error is ignored and the operation
496 /// If any other read error is encountered then this function immediately
497 /// returns. Any bytes which have already been read will be appended to
502 /// [`File`][file]s implement `Read`:
504 /// [file]: ../fs/struct.File.html
508 /// use std::io::prelude::*;
509 /// use std::fs::File;
511 /// # fn foo() -> io::Result<()> {
512 /// let mut f = try!(File::open("foo.txt"));
513 /// let mut buffer = Vec::new();
515 /// // read the whole file
516 /// try!(f.read_to_end(&mut buffer));
520 #[stable(feature = "rust1", since = "1.0.0")]
521 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> Result
<usize> {
522 read_to_end(self, buf
)
525 /// Read all bytes until EOF in this source, placing them into `buf`.
527 /// If successful, this function returns the number of bytes which were read
528 /// and appended to `buf`.
532 /// If the data in this stream is *not* valid UTF-8 then an error is
533 /// returned and `buf` is unchanged.
535 /// See [`read_to_end()`][readtoend] for other error semantics.
537 /// [readtoend]: #method.read_to_end
541 /// [`File`][file]s implement `Read`:
543 /// [file]: ../fs/struct.File.html
547 /// use std::io::prelude::*;
548 /// use std::fs::File;
550 /// # fn foo() -> io::Result<()> {
551 /// let mut f = try!(File::open("foo.txt"));
552 /// let mut buffer = String::new();
554 /// try!(f.read_to_string(&mut buffer));
558 #[stable(feature = "rust1", since = "1.0.0")]
559 fn read_to_string(&mut self, buf
: &mut String
) -> Result
<usize> {
560 // Note that we do *not* call `.read_to_end()` here. We are passing
561 // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
562 // method to fill it up. An arbitrary implementation could overwrite the
563 // entire contents of the vector, not just append to it (which is what
564 // we are expecting).
566 // To prevent extraneously checking the UTF-8-ness of the entire buffer
567 // we pass it to our hardcoded `read_to_end` implementation which we
568 // know is guaranteed to only read data into the end of the buffer.
569 append_to_string(buf
, |b
| read_to_end(self, b
))
572 /// Read the exact number of bytes required to fill `buf`.
574 /// This function reads as many bytes as necessary to completely fill the
575 /// specified buffer `buf`.
577 /// No guarantees are provided about the contents of `buf` when this
578 /// function is called, implementations cannot rely on any property of the
579 /// contents of `buf` being true. It is recommended that implementations
580 /// only write data to `buf` instead of reading its contents.
584 /// If this function encounters an error of the kind
585 /// `ErrorKind::Interrupted` then the error is ignored and the operation
588 /// If this function encounters an "end of file" before completely filling
589 /// the buffer, it returns an error of the kind `ErrorKind::UnexpectedEof`.
590 /// The contents of `buf` are unspecified in this case.
592 /// If any other read error is encountered then this function immediately
593 /// returns. The contents of `buf` are unspecified in this case.
595 /// If this function returns an error, it is unspecified how many bytes it
596 /// has read, but it will never read more than would be necessary to
597 /// completely fill the buffer.
601 /// [`File`][file]s implement `Read`:
603 /// [file]: ../fs/struct.File.html
607 /// use std::io::prelude::*;
608 /// use std::fs::File;
610 /// # fn foo() -> io::Result<()> {
611 /// let mut f = try!(File::open("foo.txt"));
612 /// let mut buffer = [0; 10];
614 /// // read exactly 10 bytes
615 /// try!(f.read_exact(&mut buffer));
619 #[stable(feature = "read_exact", since = "1.6.0")]
620 fn read_exact(&mut self, mut buf
: &mut [u8]) -> Result
<()> {
621 while !buf
.is_empty() {
622 match self.read(buf
) {
624 Ok(n
) => { let tmp = buf; buf = &mut tmp[n..]; }
625 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> {}
626 Err(e
) => return Err(e
),
630 Err(Error
::new(ErrorKind
::UnexpectedEof
,
631 "failed to fill whole buffer"))
637 /// Creates a "by reference" adaptor for this instance of `Read`.
639 /// The returned adaptor also implements `Read` and will simply borrow this
644 /// [`File`][file]s implement `Read`:
646 /// [file]: ../fs/struct.File.html
650 /// use std::io::Read;
651 /// use std::fs::File;
653 /// # fn foo() -> io::Result<()> {
654 /// let mut f = try!(File::open("foo.txt"));
655 /// let mut buffer = Vec::new();
656 /// let mut other_buffer = Vec::new();
659 /// let reference = f.by_ref();
661 /// // read at most 5 bytes
662 /// try!(reference.take(5).read_to_end(&mut buffer));
664 /// } // drop our &mut reference so we can use f again
666 /// // original file still usable, read the rest
667 /// try!(f.read_to_end(&mut other_buffer));
671 #[stable(feature = "rust1", since = "1.0.0")]
672 fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
674 /// Transforms this `Read` instance to an `Iterator` over its bytes.
676 /// The returned type implements `Iterator` where the `Item` is `Result<u8,
677 /// R::Err>`. The yielded item is `Ok` if a byte was successfully read and
678 /// `Err` otherwise for I/O errors. EOF is mapped to returning `None` from
683 /// [`File`][file]s implement `Read`:
685 /// [file]: ../fs/struct.File.html
689 /// use std::io::prelude::*;
690 /// use std::fs::File;
692 /// # fn foo() -> io::Result<()> {
693 /// let mut f = try!(File::open("foo.txt"));
695 /// for byte in f.bytes() {
696 /// println!("{}", byte.unwrap());
701 #[stable(feature = "rust1", since = "1.0.0")]
702 fn bytes(self) -> Bytes
<Self> where Self: Sized
{
703 Bytes { inner: self }
706 /// Transforms this `Read` instance to an `Iterator` over `char`s.
708 /// This adaptor will attempt to interpret this reader as a UTF-8 encoded
709 /// sequence of characters. The returned iterator will return `None` once
710 /// EOF is reached for this reader. Otherwise each element yielded will be a
711 /// `Result<char, E>` where `E` may contain information about what I/O error
712 /// occurred or where decoding failed.
714 /// Currently this adaptor will discard intermediate data read, and should
715 /// be avoided if this is not desired.
719 /// [`File`][file]s implement `Read`:
721 /// [file]: ../fs/struct.File.html
726 /// use std::io::prelude::*;
727 /// use std::fs::File;
729 /// # fn foo() -> io::Result<()> {
730 /// let mut f = try!(File::open("foo.txt"));
732 /// for c in f.chars() {
733 /// println!("{}", c.unwrap());
738 #[unstable(feature = "io", reason = "the semantics of a partial read/write \
739 of where errors happen is currently \
740 unclear and may change",
742 fn chars(self) -> Chars
<Self> where Self: Sized
{
743 Chars { inner: self }
746 /// Creates an adaptor which will chain this stream with another.
748 /// The returned `Read` instance will first read all bytes from this object
749 /// until EOF is encountered. Afterwards the output is equivalent to the
750 /// output of `next`.
754 /// [`File`][file]s implement `Read`:
756 /// [file]: ../fs/struct.File.html
760 /// use std::io::prelude::*;
761 /// use std::fs::File;
763 /// # fn foo() -> io::Result<()> {
764 /// let mut f1 = try!(File::open("foo.txt"));
765 /// let mut f2 = try!(File::open("bar.txt"));
767 /// let mut handle = f1.chain(f2);
768 /// let mut buffer = String::new();
770 /// // read the value into a String. We could use any Read method here,
771 /// // this is just one example.
772 /// try!(handle.read_to_string(&mut buffer));
776 #[stable(feature = "rust1", since = "1.0.0")]
777 fn chain
<R
: Read
>(self, next
: R
) -> Chain
<Self, R
> where Self: Sized
{
778 Chain { first: self, second: next, done_first: false }
781 /// Creates an adaptor which will read at most `limit` bytes from it.
783 /// This function returns a new instance of `Read` which will read at most
784 /// `limit` bytes, after which it will always return EOF (`Ok(0)`). Any
785 /// read errors will not count towards the number of bytes read and future
786 /// calls to `read` may succeed.
790 /// [`File`][file]s implement `Read`:
792 /// [file]: ../fs/struct.File.html
796 /// use std::io::prelude::*;
797 /// use std::fs::File;
799 /// # fn foo() -> io::Result<()> {
800 /// let mut f = try!(File::open("foo.txt"));
801 /// let mut buffer = [0; 5];
803 /// // read at most five bytes
804 /// let mut handle = f.take(5);
806 /// try!(handle.read(&mut buffer));
810 #[stable(feature = "rust1", since = "1.0.0")]
811 fn take(self, limit
: u64) -> Take
<Self> where Self: Sized
{
812 Take { inner: self, limit: limit }
816 /// A trait for objects which are byte-oriented sinks.
818 /// Implementors of the `Write` trait are sometimes called 'writers'.
820 /// Writers are defined by two required methods, `write()` and `flush()`:
822 /// * The `write()` method will attempt to write some data into the object,
823 /// returning how many bytes were successfully written.
825 /// * The `flush()` method is useful for adaptors and explicit buffers
826 /// themselves for ensuring that all buffered data has been pushed out to the
829 /// Writers are intended to be composable with one another. Many implementors
830 /// throughout `std::io` take and provide types which implement the `Write`
836 /// use std::io::prelude::*;
837 /// use std::fs::File;
839 /// # fn foo() -> std::io::Result<()> {
840 /// let mut buffer = try!(File::create("foo.txt"));
842 /// try!(buffer.write(b"some bytes"));
846 #[stable(feature = "rust1", since = "1.0.0")]
848 /// Write a buffer into this object, returning how many bytes were written.
850 /// This function will attempt to write the entire contents of `buf`, but
851 /// the entire write may not succeed, or the write may also generate an
852 /// error. A call to `write` represents *at most one* attempt to write to
853 /// any wrapped object.
855 /// Calls to `write` are not guaranteed to block waiting for data to be
856 /// written, and a write which would otherwise block can be indicated through
857 /// an `Err` variant.
859 /// If the return value is `Ok(n)` then it must be guaranteed that
860 /// `0 <= n <= buf.len()`. A return value of `0` typically means that the
861 /// underlying object is no longer able to accept bytes and will likely not
862 /// be able to in the future as well, or that the buffer provided is empty.
866 /// Each call to `write` may generate an I/O error indicating that the
867 /// operation could not be completed. If an error is returned then no bytes
868 /// in the buffer were written to this writer.
870 /// It is **not** considered an error if the entire buffer could not be
871 /// written to this writer.
876 /// use std::io::prelude::*;
877 /// use std::fs::File;
879 /// # fn foo() -> std::io::Result<()> {
880 /// let mut buffer = try!(File::create("foo.txt"));
882 /// try!(buffer.write(b"some bytes"));
886 #[stable(feature = "rust1", since = "1.0.0")]
887 fn write(&mut self, buf
: &[u8]) -> Result
<usize>;
889 /// Flush this output stream, ensuring that all intermediately buffered
890 /// contents reach their destination.
894 /// It is considered an error if not all bytes could be written due to
895 /// I/O errors or EOF being reached.
900 /// use std::io::prelude::*;
901 /// use std::io::BufWriter;
902 /// use std::fs::File;
904 /// # fn foo() -> std::io::Result<()> {
905 /// let mut buffer = BufWriter::new(try!(File::create("foo.txt")));
907 /// try!(buffer.write(b"some bytes"));
908 /// try!(buffer.flush());
912 #[stable(feature = "rust1", since = "1.0.0")]
913 fn flush(&mut self) -> Result
<()>;
915 /// Attempts to write an entire buffer into this write.
917 /// This method will continuously call `write` while there is more data to
918 /// write. This method will not return until the entire buffer has been
919 /// successfully written or an error occurs. The first error generated from
920 /// this method will be returned.
924 /// This function will return the first error that `write` returns.
929 /// use std::io::prelude::*;
930 /// use std::fs::File;
932 /// # fn foo() -> std::io::Result<()> {
933 /// let mut buffer = try!(File::create("foo.txt"));
935 /// try!(buffer.write_all(b"some bytes"));
939 #[stable(feature = "rust1", since = "1.0.0")]
940 fn write_all(&mut self, mut buf
: &[u8]) -> Result
<()> {
941 while !buf
.is_empty() {
942 match self.write(buf
) {
943 Ok(0) => return Err(Error
::new(ErrorKind
::WriteZero
,
944 "failed to write whole buffer")),
945 Ok(n
) => buf
= &buf
[n
..],
946 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> {}
947 Err(e
) => return Err(e
),
953 /// Writes a formatted string into this writer, returning any error
956 /// This method is primarily used to interface with the
957 /// [`format_args!`][formatargs] macro, but it is rare that this should
958 /// explicitly be called. The [`write!`][write] macro should be favored to
959 /// invoke this method instead.
961 /// [formatargs]: ../macro.format_args!.html
962 /// [write]: ../macro.write!.html
964 /// This function internally uses the [`write_all`][writeall] method on
965 /// this trait and hence will continuously write data so long as no errors
966 /// are received. This also means that partial writes are not indicated in
969 /// [writeall]: #method.write_all
973 /// This function will return any I/O error reported while formatting.
978 /// use std::io::prelude::*;
979 /// use std::fs::File;
981 /// # fn foo() -> std::io::Result<()> {
982 /// let mut buffer = try!(File::create("foo.txt"));
985 /// try!(write!(buffer, "{:.*}", 2, 1.234567));
986 /// // turns into this:
987 /// try!(buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)));
991 #[stable(feature = "rust1", since = "1.0.0")]
992 fn write_fmt(&mut self, fmt
: fmt
::Arguments
) -> Result
<()> {
993 // Create a shim which translates a Write to a fmt::Write and saves
994 // off I/O errors. instead of discarding them
995 struct Adaptor
<'a
, T
: ?Sized
+ 'a
> {
1000 impl<'a
, T
: Write
+ ?Sized
> fmt
::Write
for Adaptor
<'a
, T
> {
1001 fn write_str(&mut self, s
: &str) -> fmt
::Result
{
1002 match self.inner
.write_all(s
.as_bytes()) {
1005 self.error
= Err(e
);
1012 let mut output
= Adaptor { inner: self, error: Ok(()) }
;
1013 match fmt
::write(&mut output
, fmt
) {
1016 // check if the error came from the underlying `Write` or not
1017 if output
.error
.is_err() {
1020 Err(Error
::new(ErrorKind
::Other
, "formatter error"))
1026 /// Creates a "by reference" adaptor for this instance of `Write`.
1028 /// The returned adaptor also implements `Write` and will simply borrow this
1034 /// use std::io::Write;
1035 /// use std::fs::File;
1037 /// # fn foo() -> std::io::Result<()> {
1038 /// let mut buffer = try!(File::create("foo.txt"));
1040 /// let reference = buffer.by_ref();
1042 /// // we can use reference just like our original buffer
1043 /// try!(reference.write_all(b"some bytes"));
1047 #[stable(feature = "rust1", since = "1.0.0")]
1048 fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
1051 /// The `Seek` trait provides a cursor which can be moved within a stream of
1054 /// The stream typically has a fixed size, allowing seeking relative to either
1055 /// end or the current offset.
1059 /// [`File`][file]s implement `Seek`:
1061 /// [file]: ../fs/struct.File.html
1065 /// use std::io::prelude::*;
1066 /// use std::fs::File;
1067 /// use std::io::SeekFrom;
1069 /// # fn foo() -> io::Result<()> {
1070 /// let mut f = try!(File::open("foo.txt"));
1072 /// // move the cursor 42 bytes from the start of the file
1073 /// try!(f.seek(SeekFrom::Start(42)));
1077 #[stable(feature = "rust1", since = "1.0.0")]
1079 /// Seek to an offset, in bytes, in a stream.
1081 /// A seek beyond the end of a stream is allowed, but implementation
1084 /// If the seek operation completed successfully,
1085 /// this method returns the new position from the start of the stream.
1086 /// That position can be used later with `SeekFrom::Start`.
1090 /// Seeking to a negative offset is considered an error.
1091 #[stable(feature = "rust1", since = "1.0.0")]
1092 fn seek(&mut self, pos
: SeekFrom
) -> Result
<u64>;
1095 /// Enumeration of possible methods to seek within an I/O object.
1096 #[derive(Copy, PartialEq, Eq, Clone, Debug)]
1097 #[stable(feature = "rust1", since = "1.0.0")]
1099 /// Set the offset to the provided number of bytes.
1100 #[stable(feature = "rust1", since = "1.0.0")]
1101 Start(#[stable(feature = "rust1", since = "1.0.0")] u64),
1103 /// Set the offset to the size of this object plus the specified number of
1106 /// It is possible to seek beyond the end of an object, but it's an error to
1107 /// seek before byte 0.
1108 #[stable(feature = "rust1", since = "1.0.0")]
1109 End(#[stable(feature = "rust1", since = "1.0.0")] i64),
1111 /// Set the offset to the current position plus the specified number of
1114 /// It is possible to seek beyond the end of an object, but it's an error to
1115 /// seek before byte 0.
1116 #[stable(feature = "rust1", since = "1.0.0")]
1117 Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
1120 fn read_until
<R
: BufRead
+ ?Sized
>(r
: &mut R
, delim
: u8, buf
: &mut Vec
<u8>)
1124 let (done
, used
) = {
1125 let available
= match r
.fill_buf() {
1127 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> continue,
1128 Err(e
) => return Err(e
)
1130 match memchr
::memchr(delim
, available
) {
1132 buf
.extend_from_slice(&available
[..i
+ 1]);
1136 buf
.extend_from_slice(available
);
1137 (false, available
.len())
1143 if done
|| used
== 0 {
1149 /// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
1150 /// to perform extra ways of reading.
1152 /// For example, reading line-by-line is inefficient without using a buffer, so
1153 /// if you want to read by line, you'll need `BufRead`, which includes a
1154 /// [`read_line()`][readline] method as well as a [`lines()`][lines] iterator.
1156 /// [readline]: #method.read_line
1157 /// [lines]: #method.lines
1161 /// A locked standard input implements `BufRead`:
1165 /// use std::io::prelude::*;
1167 /// let stdin = io::stdin();
1168 /// for line in stdin.lock().lines() {
1169 /// println!("{}", line.unwrap());
1173 /// If you have something that implements `Read`, you can use the [`BufReader`
1174 /// type][bufreader] to turn it into a `BufRead`.
1176 /// For example, [`File`][file] implements `Read`, but not `BufRead`.
1177 /// `BufReader` to the rescue!
1179 /// [bufreader]: struct.BufReader.html
1180 /// [file]: ../fs/struct.File.html
1183 /// use std::io::{self, BufReader};
1184 /// use std::io::prelude::*;
1185 /// use std::fs::File;
1187 /// # fn foo() -> io::Result<()> {
1188 /// let f = try!(File::open("foo.txt"));
1189 /// let f = BufReader::new(f);
1191 /// for line in f.lines() {
1192 /// println!("{}", line.unwrap());
1199 #[stable(feature = "rust1", since = "1.0.0")]
1200 pub trait BufRead
: Read
{
1201 /// Fills the internal buffer of this object, returning the buffer contents.
1203 /// This function is a lower-level call. It needs to be paired with the
1204 /// [`consume`][consume] method to function properly. When calling this
1205 /// method, none of the contents will be "read" in the sense that later
1206 /// calling `read` may return the same contents. As such, `consume` must be
1207 /// called with the number of bytes that are consumed from this buffer to
1208 /// ensure that the bytes are never returned twice.
1210 /// [consume]: #tymethod.consume
1212 /// An empty buffer returned indicates that the stream has reached EOF.
1216 /// This function will return an I/O error if the underlying reader was
1217 /// read, but returned an error.
1221 /// A locked standard input implements `BufRead`:
1225 /// use std::io::prelude::*;
1227 /// let stdin = io::stdin();
1228 /// let mut stdin = stdin.lock();
1230 /// // we can't have two `&mut` references to `stdin`, so use a block
1231 /// // to end the borrow early.
1233 /// let buffer = stdin.fill_buf().unwrap();
1235 /// // work with buffer
1236 /// println!("{:?}", buffer);
1241 /// // ensure the bytes we worked with aren't returned again later
1242 /// stdin.consume(length);
1244 #[stable(feature = "rust1", since = "1.0.0")]
1245 fn fill_buf(&mut self) -> Result
<&[u8]>;
1247 /// Tells this buffer that `amt` bytes have been consumed from the buffer,
1248 /// so they should no longer be returned in calls to `read`.
1250 /// This function is a lower-level call. It needs to be paired with the
1251 /// [`fill_buf`][fillbuf] method to function properly. This function does
1252 /// not perform any I/O, it simply informs this object that some amount of
1253 /// its buffer, returned from `fill_buf`, has been consumed and should no
1254 /// longer be returned. As such, this function may do odd things if
1255 /// `fill_buf` isn't called before calling it.
1257 /// [fillbuf]: #tymethod.fill_buf
1259 /// The `amt` must be `<=` the number of bytes in the buffer returned by
1264 /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf],
1265 /// that method's example includes an example of `consume()`.
1266 #[stable(feature = "rust1", since = "1.0.0")]
1267 fn consume(&mut self, amt
: usize);
1269 /// Read all bytes into `buf` until the delimiter `byte` is reached.
1271 /// This function will read bytes from the underlying stream until the
1272 /// delimiter or EOF is found. Once found, all bytes up to, and including,
1273 /// the delimiter (if found) will be appended to `buf`.
1275 /// If this reader is currently at EOF then this function will not modify
1276 /// `buf` and will return `Ok(n)` where `n` is the number of bytes which
1281 /// This function will ignore all instances of `ErrorKind::Interrupted` and
1282 /// will otherwise return any errors returned by `fill_buf`.
1284 /// If an I/O error is encountered then all bytes read so far will be
1285 /// present in `buf` and its length will have been adjusted appropriately.
1289 /// A locked standard input implements `BufRead`. In this example, we'll
1290 /// read from standard input until we see an `a` byte.
1294 /// use std::io::prelude::*;
1296 /// fn foo() -> io::Result<()> {
1297 /// let stdin = io::stdin();
1298 /// let mut stdin = stdin.lock();
1299 /// let mut buffer = Vec::new();
1301 /// try!(stdin.read_until(b'a', &mut buffer));
1303 /// println!("{:?}", buffer);
1307 #[stable(feature = "rust1", since = "1.0.0")]
1308 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> Result
<usize> {
1309 read_until(self, byte
, buf
)
1312 /// Read all bytes until a newline (the 0xA byte) is reached, and append
1313 /// them to the provided buffer.
1315 /// This function will read bytes from the underlying stream until the
1316 /// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
1317 /// up to, and including, the delimiter (if found) will be appended to
1320 /// If this reader is currently at EOF then this function will not modify
1321 /// `buf` and will return `Ok(n)` where `n` is the number of bytes which
1326 /// This function has the same error semantics as `read_until` and will also
1327 /// return an error if the read bytes are not valid UTF-8. If an I/O error
1328 /// is encountered then `buf` may contain some bytes already read in the
1329 /// event that all data read so far was valid UTF-8.
1333 /// A locked standard input implements `BufRead`. In this example, we'll
1334 /// read all of the lines from standard input. If we were to do this in
1335 /// an actual project, the [`lines()`][lines] method would be easier, of
1338 /// [lines]: #method.lines
1342 /// use std::io::prelude::*;
1344 /// let stdin = io::stdin();
1345 /// let mut stdin = stdin.lock();
1346 /// let mut buffer = String::new();
1348 /// while stdin.read_line(&mut buffer).unwrap() > 0 {
1349 /// // work with buffer
1350 /// println!("{:?}", buffer);
1355 #[stable(feature = "rust1", since = "1.0.0")]
1356 fn read_line(&mut self, buf
: &mut String
) -> Result
<usize> {
1357 // Note that we are not calling the `.read_until` method here, but
1358 // rather our hardcoded implementation. For more details as to why, see
1359 // the comments in `read_to_end`.
1360 append_to_string(buf
, |b
| read_until(self, b'
\n'
, b
))
1363 /// Returns an iterator over the contents of this reader split on the byte
1366 /// The iterator returned from this function will return instances of
1367 /// `io::Result<Vec<u8>>`. Each vector returned will *not* have the
1368 /// delimiter byte at the end.
1370 /// This function will yield errors whenever `read_until` would have also
1371 /// yielded an error.
1375 /// A locked standard input implements `BufRead`. In this example, we'll
1376 /// read some input from standard input, splitting on commas.
1380 /// use std::io::prelude::*;
1382 /// let stdin = io::stdin();
1384 /// for content in stdin.lock().split(b',') {
1385 /// println!("{:?}", content.unwrap());
1388 #[stable(feature = "rust1", since = "1.0.0")]
1389 fn split(self, byte
: u8) -> Split
<Self> where Self: Sized
{
1390 Split { buf: self, delim: byte }
1393 /// Returns an iterator over the lines of this reader.
1395 /// The iterator returned from this function will yield instances of
1396 /// `io::Result<String>`. Each string returned will *not* have a newline
1397 /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
1401 /// A locked standard input implements `BufRead`:
1405 /// use std::io::prelude::*;
1407 /// let stdin = io::stdin();
1409 /// for line in stdin.lock().lines() {
1410 /// println!("{}", line.unwrap());
1413 #[stable(feature = "rust1", since = "1.0.0")]
1414 fn lines(self) -> Lines
<Self> where Self: Sized
{
1419 /// Adaptor to chain together two readers.
1421 /// This struct is generally created by calling [`chain()`][chain] on a reader.
1422 /// Please see the documentation of `chain()` for more details.
1424 /// [chain]: trait.Read.html#method.chain
1425 #[stable(feature = "rust1", since = "1.0.0")]
1426 pub struct Chain
<T
, U
> {
1432 #[stable(feature = "rust1", since = "1.0.0")]
1433 impl<T
: Read
, U
: Read
> Read
for Chain
<T
, U
> {
1434 fn read(&mut self, buf
: &mut [u8]) -> Result
<usize> {
1435 if !self.done_first
{
1436 match self.first
.read(buf
)?
{
1437 0 => { self.done_first = true; }
1441 self.second
.read(buf
)
1445 #[stable(feature = "chain_bufread", since = "1.9.0")]
1446 impl<T
: BufRead
, U
: BufRead
> BufRead
for Chain
<T
, U
> {
1447 fn fill_buf(&mut self) -> Result
<&[u8]> {
1448 if !self.done_first
{
1449 match self.first
.fill_buf()?
{
1450 buf
if buf
.len() == 0 => { self.done_first = true; }
1451 buf
=> return Ok(buf
),
1454 self.second
.fill_buf()
1457 fn consume(&mut self, amt
: usize) {
1458 if !self.done_first
{
1459 self.first
.consume(amt
)
1461 self.second
.consume(amt
)
1466 /// Reader adaptor which limits the bytes read from an underlying reader.
1468 /// This struct is generally created by calling [`take()`][take] on a reader.
1469 /// Please see the documentation of `take()` for more details.
1471 /// [take]: trait.Read.html#method.take
1472 #[stable(feature = "rust1", since = "1.0.0")]
1473 pub struct Take
<T
> {
1479 /// Returns the number of bytes that can be read before this instance will
1484 /// This instance may reach EOF after reading fewer bytes than indicated by
1485 /// this method if the underlying `Read` instance reaches EOF.
1486 #[stable(feature = "rust1", since = "1.0.0")]
1487 pub fn limit(&self) -> u64 { self.limit }
1490 #[stable(feature = "rust1", since = "1.0.0")]
1491 impl<T
: Read
> Read
for Take
<T
> {
1492 fn read(&mut self, buf
: &mut [u8]) -> Result
<usize> {
1493 // Don't call into inner reader at all at EOF because it may still block
1494 if self.limit
== 0 {
1498 let max
= cmp
::min(buf
.len() as u64, self.limit
) as usize;
1499 let n
= self.inner
.read(&mut buf
[..max
])?
;
1500 self.limit
-= n
as u64;
1505 #[stable(feature = "rust1", since = "1.0.0")]
1506 impl<T
: BufRead
> BufRead
for Take
<T
> {
1507 fn fill_buf(&mut self) -> Result
<&[u8]> {
1508 let buf
= self.inner
.fill_buf()?
;
1509 let cap
= cmp
::min(buf
.len() as u64, self.limit
) as usize;
1513 fn consume(&mut self, amt
: usize) {
1514 // Don't let callers reset the limit by passing an overlarge value
1515 let amt
= cmp
::min(amt
as u64, self.limit
) as usize;
1516 self.limit
-= amt
as u64;
1517 self.inner
.consume(amt
);
1521 /// An iterator over `u8` values of a reader.
1523 /// This struct is generally created by calling [`bytes()`][bytes] on a reader.
1524 /// Please see the documentation of `bytes()` for more details.
1526 /// [bytes]: trait.Read.html#method.bytes
1527 #[stable(feature = "rust1", since = "1.0.0")]
1528 pub struct Bytes
<R
> {
1532 #[stable(feature = "rust1", since = "1.0.0")]
1533 impl<R
: Read
> Iterator
for Bytes
<R
> {
1534 type Item
= Result
<u8>;
1536 fn next(&mut self) -> Option
<Result
<u8>> {
1538 match self.inner
.read(&mut buf
) {
1540 Ok(..) => Some(Ok(buf
[0])),
1541 Err(e
) => Some(Err(e
)),
1546 /// An iterator over the `char`s of a reader.
1548 /// This struct is generally created by calling [`chars()`][chars] on a reader.
1549 /// Please see the documentation of `chars()` for more details.
1551 /// [chars]: trait.Read.html#method.chars
1552 #[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1554 pub struct Chars
<R
> {
1558 /// An enumeration of possible errors that can be generated from the `Chars`
1561 #[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1563 pub enum CharsError
{
1564 /// Variant representing that the underlying stream was read successfully
1565 /// but it did not contain valid utf8 data.
1568 /// Variant representing that an I/O error occurred.
1572 #[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1574 impl<R
: Read
> Iterator
for Chars
<R
> {
1575 type Item
= result
::Result
<char, CharsError
>;
1577 fn next(&mut self) -> Option
<result
::Result
<char, CharsError
>> {
1579 let first_byte
= match self.inner
.read(&mut buf
) {
1580 Ok(0) => return None
,
1582 Err(e
) => return Some(Err(CharsError
::Other(e
))),
1584 let width
= core_str
::utf8_char_width(first_byte
);
1585 if width
== 1 { return Some(Ok(first_byte as char)) }
1586 if width
== 0 { return Some(Err(CharsError::NotUtf8)) }
1587 let mut buf
= [first_byte
, 0, 0, 0];
1590 while start
< width
{
1591 match self.inner
.read(&mut buf
[start
..width
]) {
1592 Ok(0) => return Some(Err(CharsError
::NotUtf8
)),
1593 Ok(n
) => start
+= n
,
1594 Err(e
) => return Some(Err(CharsError
::Other(e
))),
1598 Some(match str::from_utf8(&buf
[..width
]).ok() {
1599 Some(s
) => Ok(s
.chars().next().unwrap()),
1600 None
=> Err(CharsError
::NotUtf8
),
1605 #[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1607 impl std_error
::Error
for CharsError
{
1608 fn description(&self) -> &str {
1610 CharsError
::NotUtf8
=> "invalid utf8 encoding",
1611 CharsError
::Other(ref e
) => std_error
::Error
::description(e
),
1614 fn cause(&self) -> Option
<&std_error
::Error
> {
1616 CharsError
::NotUtf8
=> None
,
1617 CharsError
::Other(ref e
) => e
.cause(),
1622 #[unstable(feature = "io", reason = "awaiting stability of Read::chars",
1624 impl fmt
::Display
for CharsError
{
1625 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1627 CharsError
::NotUtf8
=> {
1628 "byte stream did not contain valid utf8".fmt(f
)
1630 CharsError
::Other(ref e
) => e
.fmt(f
),
1635 /// An iterator over the contents of an instance of `BufRead` split on a
1636 /// particular byte.
1638 /// This struct is generally created by calling [`split()`][split] on a
1639 /// `BufRead`. Please see the documentation of `split()` for more details.
1641 /// [split]: trait.BufRead.html#method.split
1642 #[stable(feature = "rust1", since = "1.0.0")]
1643 pub struct Split
<B
> {
1648 #[stable(feature = "rust1", since = "1.0.0")]
1649 impl<B
: BufRead
> Iterator
for Split
<B
> {
1650 type Item
= Result
<Vec
<u8>>;
1652 fn next(&mut self) -> Option
<Result
<Vec
<u8>>> {
1653 let mut buf
= Vec
::new();
1654 match self.buf
.read_until(self.delim
, &mut buf
) {
1657 if buf
[buf
.len() - 1] == self.delim
{
1662 Err(e
) => Some(Err(e
))
1667 /// An iterator over the lines of an instance of `BufRead`.
1669 /// This struct is generally created by calling [`lines()`][lines] on a
1670 /// `BufRead`. Please see the documentation of `lines()` for more details.
1672 /// [lines]: trait.BufRead.html#method.lines
1673 #[stable(feature = "rust1", since = "1.0.0")]
1674 pub struct Lines
<B
> {
1678 #[stable(feature = "rust1", since = "1.0.0")]
1679 impl<B
: BufRead
> Iterator
for Lines
<B
> {
1680 type Item
= Result
<String
>;
1682 fn next(&mut self) -> Option
<Result
<String
>> {
1683 let mut buf
= String
::new();
1684 match self.buf
.read_line(&mut buf
) {
1687 if buf
.ends_with("\n") {
1689 if buf
.ends_with("\r") {
1695 Err(e
) => Some(Err(e
))
1711 let mut buf
= Cursor
::new(&b
"12"[..]);
1712 let mut v
= Vec
::new();
1713 assert_eq
!(buf
.read_until(b'
3'
, &mut v
).unwrap(), 2);
1714 assert_eq
!(v
, b
"12");
1716 let mut buf
= Cursor
::new(&b
"1233"[..]);
1717 let mut v
= Vec
::new();
1718 assert_eq
!(buf
.read_until(b'
3'
, &mut v
).unwrap(), 3);
1719 assert_eq
!(v
, b
"123");
1721 assert_eq
!(buf
.read_until(b'
3'
, &mut v
).unwrap(), 1);
1722 assert_eq
!(v
, b
"3");
1724 assert_eq
!(buf
.read_until(b'
3'
, &mut v
).unwrap(), 0);
1730 let buf
= Cursor
::new(&b
"12"[..]);
1731 let mut s
= buf
.split(b'
3'
);
1732 assert_eq
!(s
.next().unwrap().unwrap(), vec
![b'
1'
, b'
2'
]);
1733 assert
!(s
.next().is_none());
1735 let buf
= Cursor
::new(&b
"1233"[..]);
1736 let mut s
= buf
.split(b'
3'
);
1737 assert_eq
!(s
.next().unwrap().unwrap(), vec
![b'
1'
, b'
2'
]);
1738 assert_eq
!(s
.next().unwrap().unwrap(), vec
![]);
1739 assert
!(s
.next().is_none());
1744 let mut buf
= Cursor
::new(&b
"12"[..]);
1745 let mut v
= String
::new();
1746 assert_eq
!(buf
.read_line(&mut v
).unwrap(), 2);
1747 assert_eq
!(v
, "12");
1749 let mut buf
= Cursor
::new(&b
"12\n\n"[..]);
1750 let mut v
= String
::new();
1751 assert_eq
!(buf
.read_line(&mut v
).unwrap(), 3);
1752 assert_eq
!(v
, "12\n");
1754 assert_eq
!(buf
.read_line(&mut v
).unwrap(), 1);
1755 assert_eq
!(v
, "\n");
1757 assert_eq
!(buf
.read_line(&mut v
).unwrap(), 0);
1763 let buf
= Cursor
::new(&b
"12\r"[..]);
1764 let mut s
= buf
.lines();
1765 assert_eq
!(s
.next().unwrap().unwrap(), "12\r".to_string());
1766 assert
!(s
.next().is_none());
1768 let buf
= Cursor
::new(&b
"12\r\n\n"[..]);
1769 let mut s
= buf
.lines();
1770 assert_eq
!(s
.next().unwrap().unwrap(), "12".to_string());
1771 assert_eq
!(s
.next().unwrap().unwrap(), "".to_string());
1772 assert
!(s
.next().is_none());
1777 let mut c
= Cursor
::new(&b
""[..]);
1778 let mut v
= Vec
::new();
1779 assert_eq
!(c
.read_to_end(&mut v
).unwrap(), 0);
1782 let mut c
= Cursor
::new(&b
"1"[..]);
1783 let mut v
= Vec
::new();
1784 assert_eq
!(c
.read_to_end(&mut v
).unwrap(), 1);
1785 assert_eq
!(v
, b
"1");
1787 let cap
= 1024 * 1024;
1788 let data
= (0..cap
).map(|i
| (i
/ 3) as u8).collect
::<Vec
<_
>>();
1789 let mut v
= Vec
::new();
1790 let (a
, b
) = data
.split_at(data
.len() / 2);
1791 assert_eq
!(Cursor
::new(a
).read_to_end(&mut v
).unwrap(), a
.len());
1792 assert_eq
!(Cursor
::new(b
).read_to_end(&mut v
).unwrap(), b
.len());
1793 assert_eq
!(v
, data
);
1797 fn read_to_string() {
1798 let mut c
= Cursor
::new(&b
""[..]);
1799 let mut v
= String
::new();
1800 assert_eq
!(c
.read_to_string(&mut v
).unwrap(), 0);
1803 let mut c
= Cursor
::new(&b
"1"[..]);
1804 let mut v
= String
::new();
1805 assert_eq
!(c
.read_to_string(&mut v
).unwrap(), 1);
1808 let mut c
= Cursor
::new(&b
"\xff"[..]);
1809 let mut v
= String
::new();
1810 assert
!(c
.read_to_string(&mut v
).is_err());
1815 let mut buf
= [0; 4];
1817 let mut c
= Cursor
::new(&b
""[..]);
1818 assert_eq
!(c
.read_exact(&mut buf
).unwrap_err().kind(),
1819 io
::ErrorKind
::UnexpectedEof
);
1821 let mut c
= Cursor
::new(&b
"123"[..]).chain(Cursor
::new(&b
"456789"[..]));
1822 c
.read_exact(&mut buf
).unwrap();
1823 assert_eq
!(&buf
, b
"1234");
1824 c
.read_exact(&mut buf
).unwrap();
1825 assert_eq
!(&buf
, b
"5678");
1826 assert_eq
!(c
.read_exact(&mut buf
).unwrap_err().kind(),
1827 io
::ErrorKind
::UnexpectedEof
);
1831 fn read_exact_slice() {
1832 let mut buf
= [0; 4];
1834 let mut c
= &b
""[..];
1835 assert_eq
!(c
.read_exact(&mut buf
).unwrap_err().kind(),
1836 io
::ErrorKind
::UnexpectedEof
);
1838 let mut c
= &b
"123"[..];
1839 assert_eq
!(c
.read_exact(&mut buf
).unwrap_err().kind(),
1840 io
::ErrorKind
::UnexpectedEof
);
1841 // make sure the optimized (early returning) method is being used
1842 assert_eq
!(&buf
, &[0; 4]);
1844 let mut c
= &b
"1234"[..];
1845 c
.read_exact(&mut buf
).unwrap();
1846 assert_eq
!(&buf
, b
"1234");
1848 let mut c
= &b
"56789"[..];
1849 c
.read_exact(&mut buf
).unwrap();
1850 assert_eq
!(&buf
, b
"5678");
1851 assert_eq
!(c
, b
"9");
1859 fn read(&mut self, _
: &mut [u8]) -> io
::Result
<usize> {
1860 Err(io
::Error
::new(io
::ErrorKind
::Other
, ""))
1864 let mut buf
= [0; 1];
1865 assert_eq
!(0, R
.take(0).read(&mut buf
).unwrap());
1868 fn cmp_bufread
<Br1
: BufRead
, Br2
: BufRead
>(mut br1
: Br1
, mut br2
: Br2
, exp
: &[u8]) {
1869 let mut cat
= Vec
::new();
1872 let buf1
= br1
.fill_buf().unwrap();
1873 let buf2
= br2
.fill_buf().unwrap();
1874 let minlen
= if buf1
.len() < buf2
.len() { buf1.len() }
else { buf2.len() }
;
1875 assert_eq
!(buf1
[..minlen
], buf2
[..minlen
]);
1876 cat
.extend_from_slice(&buf1
[..minlen
]);
1882 br1
.consume(consume
);
1883 br2
.consume(consume
);
1885 assert_eq
!(br1
.fill_buf().unwrap().len(), 0);
1886 assert_eq
!(br2
.fill_buf().unwrap().len(), 0);
1887 assert_eq
!(&cat
[..], &exp
[..])
1891 fn chain_bufread() {
1892 let testdata
= b
"ABCDEFGHIJKL";
1893 let chain1
= (&testdata
[..3]).chain(&testdata
[3..6])
1894 .chain(&testdata
[6..9])
1895 .chain(&testdata
[9..]);
1896 let chain2
= (&testdata
[..4]).chain(&testdata
[4..8])
1897 .chain(&testdata
[8..]);
1898 cmp_bufread(chain1
, chain2
, &testdata
[..]);
1902 fn bench_read_to_end(b
: &mut test
::Bencher
) {
1904 let mut lr
= repeat(1).take(10000000);
1905 let mut vec
= Vec
::with_capacity(1024);
1906 super::read_to_end(&mut lr
, &mut vec
)