PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/libstd/old_io/mod.rs

http://github.com/eholk/rust
Rust | 1972 lines | 924 code | 195 blank | 853 comment | 51 complexity | d9f836ee0e49aaa91f83379b46310a2d MD5 | raw file
Possible License(s): AGPL-1.0, BSD-2-Clause, 0BSD, Apache-2.0, MIT, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // Copyright 2013-2014 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.
  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.
  10. //
  11. // ignore-lexer-test FIXME #15883
  12. // FIXME: cover these topics:
  13. // path, reader, writer, stream, raii (close not needed),
  14. // stdio, print!, println!, file access, process spawning,
  15. // error handling
  16. //! I/O, including files, networking, timers, and processes
  17. //!
  18. //! > **Warning**: This module is currently called `old_io` for a reason! The
  19. //! > module is currently being redesigned in a number of RFCs. For more details
  20. //! > follow the RFC repository in connection with [RFC 517][base] or follow
  21. //! > some of these sub-RFCs
  22. //! >
  23. //! > * [String handling][osstr]
  24. //! > * [Core I/O support][core]
  25. //! > * [Deadlines][deadlines]
  26. //! > * [std::env][env]
  27. //! > * [std::process][process]
  28. //!
  29. //! [base]: https://github.com/rust-lang/rfcs/blob/master/text/0517-io-os-reform.md
  30. //! [osstr]: https://github.com/rust-lang/rfcs/pull/575
  31. //! [core]: https://github.com/rust-lang/rfcs/pull/576
  32. //! [deadlines]: https://github.com/rust-lang/rfcs/pull/577
  33. //! [env]: https://github.com/rust-lang/rfcs/pull/578
  34. //! [process]: https://github.com/rust-lang/rfcs/pull/579
  35. //!
  36. //! `std::io` provides Rust's basic I/O types,
  37. //! for reading and writing to files, TCP, UDP,
  38. //! and other types of sockets and pipes,
  39. //! manipulating the file system, spawning processes.
  40. //!
  41. //! # Examples
  42. //!
  43. //! Some examples of obvious things you might want to do
  44. //!
  45. //! * Read lines from stdin
  46. //!
  47. //! ```rust
  48. //! use std::old_io as io;
  49. //!
  50. //! let mut stdin = io::stdin();
  51. //! for line in stdin.lock().lines() {
  52. //! print!("{}", line.unwrap());
  53. //! }
  54. //! ```
  55. //!
  56. //! * Read a complete file
  57. //!
  58. //! ```rust
  59. //! use std::old_io::File;
  60. //!
  61. //! let contents = File::open(&Path::new("message.txt")).read_to_end();
  62. //! ```
  63. //!
  64. //! * Write a line to a file
  65. //!
  66. //! ```rust
  67. //! # #![allow(unused_must_use)]
  68. //! use std::old_io::File;
  69. //!
  70. //! let mut file = File::create(&Path::new("message.txt"));
  71. //! file.write_all(b"hello, file!\n");
  72. //! # drop(file);
  73. //! # ::std::old_io::fs::unlink(&Path::new("message.txt"));
  74. //! ```
  75. //!
  76. //! * Iterate over the lines of a file
  77. //!
  78. //! ```rust,no_run
  79. //! use std::old_io::BufferedReader;
  80. //! use std::old_io::File;
  81. //!
  82. //! let path = Path::new("message.txt");
  83. //! let mut file = BufferedReader::new(File::open(&path));
  84. //! for line in file.lines() {
  85. //! print!("{}", line.unwrap());
  86. //! }
  87. //! ```
  88. //!
  89. //! * Pull the lines of a file into a vector of strings
  90. //!
  91. //! ```rust,no_run
  92. //! use std::old_io::BufferedReader;
  93. //! use std::old_io::File;
  94. //!
  95. //! let path = Path::new("message.txt");
  96. //! let mut file = BufferedReader::new(File::open(&path));
  97. //! let lines: Vec<String> = file.lines().map(|x| x.unwrap()).collect();
  98. //! ```
  99. //!
  100. //! * Make a simple TCP client connection and request
  101. //!
  102. //! ```rust
  103. //! # #![allow(unused_must_use)]
  104. //! use std::old_io::TcpStream;
  105. //!
  106. //! # // connection doesn't fail if a server is running on 8080
  107. //! # // locally, we still want to be type checking this code, so lets
  108. //! # // just stop it running (#11576)
  109. //! # if false {
  110. //! let mut socket = TcpStream::connect("127.0.0.1:8080").unwrap();
  111. //! socket.write_all(b"GET / HTTP/1.0\n\n");
  112. //! let response = socket.read_to_end();
  113. //! # }
  114. //! ```
  115. //!
  116. //! * Make a simple TCP server
  117. //!
  118. //! ```rust
  119. //! # fn main() { }
  120. //! # fn foo() {
  121. //! # #![allow(dead_code)]
  122. //! use std::old_io::{TcpListener, TcpStream};
  123. //! use std::old_io::{Acceptor, Listener};
  124. //! use std::thread;
  125. //!
  126. //! let listener = TcpListener::bind("127.0.0.1:80");
  127. //!
  128. //! // bind the listener to the specified address
  129. //! let mut acceptor = listener.listen();
  130. //!
  131. //! fn handle_client(mut stream: TcpStream) {
  132. //! // ...
  133. //! # &mut stream; // silence unused mutability/variable warning
  134. //! }
  135. //! // accept connections and process them, spawning a new tasks for each one
  136. //! for stream in acceptor.incoming() {
  137. //! match stream {
  138. //! Err(e) => { /* connection failed */ }
  139. //! Ok(stream) => {
  140. //! thread::spawn(move|| {
  141. //! // connection succeeded
  142. //! handle_client(stream)
  143. //! });
  144. //! }
  145. //! }
  146. //! }
  147. //!
  148. //! // close the socket server
  149. //! drop(acceptor);
  150. //! # }
  151. //! ```
  152. //!
  153. //!
  154. //! # Error Handling
  155. //!
  156. //! I/O is an area where nearly every operation can result in unexpected
  157. //! errors. Errors should be painfully visible when they happen, and handling them
  158. //! should be easy to work with. It should be convenient to handle specific I/O
  159. //! errors, and it should also be convenient to not deal with I/O errors.
  160. //!
  161. //! Rust's I/O employs a combination of techniques to reduce boilerplate
  162. //! while still providing feedback about errors. The basic strategy:
  163. //!
  164. //! * All I/O operations return `IoResult<T>` which is equivalent to
  165. //! `Result<T, IoError>`. The `Result` type is defined in the `std::result`
  166. //! module.
  167. //! * If the `Result` type goes unused, then the compiler will by default emit a
  168. //! warning about the unused result. This is because `Result` has the
  169. //! `#[must_use]` attribute.
  170. //! * Common traits are implemented for `IoResult`, e.g.
  171. //! `impl<R: Reader> Reader for IoResult<R>`, so that error values do not have
  172. //! to be 'unwrapped' before use.
  173. //!
  174. //! These features combine in the API to allow for expressions like
  175. //! `File::create(&Path::new("diary.txt")).write_all(b"Met a girl.\n")`
  176. //! without having to worry about whether "diary.txt" exists or whether
  177. //! the write succeeds. As written, if either `new` or `write_line`
  178. //! encounters an error then the result of the entire expression will
  179. //! be an error.
  180. //!
  181. //! If you wanted to handle the error though you might write:
  182. //!
  183. //! ```rust
  184. //! # #![allow(unused_must_use)]
  185. //! use std::old_io::File;
  186. //!
  187. //! match File::create(&Path::new("diary.txt")).write_all(b"Met a girl.\n") {
  188. //! Ok(()) => (), // succeeded
  189. //! Err(e) => println!("failed to write to my diary: {}", e),
  190. //! }
  191. //!
  192. //! # ::std::old_io::fs::unlink(&Path::new("diary.txt"));
  193. //! ```
  194. //!
  195. //! So what actually happens if `create` encounters an error?
  196. //! It's important to know that what `new` returns is not a `File`
  197. //! but an `IoResult<File>`. If the file does not open, then `new` will simply
  198. //! return `Err(..)`. Because there is an implementation of `Writer` (the trait
  199. //! required ultimately required for types to implement `write_line`) there is no
  200. //! need to inspect or unwrap the `IoResult<File>` and we simply call `write_line`
  201. //! on it. If `new` returned an `Err(..)` then the followup call to `write_line`
  202. //! will also return an error.
  203. //!
  204. //! ## `try!`
  205. //!
  206. //! Explicit pattern matching on `IoResult`s can get quite verbose, especially
  207. //! when performing many I/O operations. Some examples (like those above) are
  208. //! alleviated with extra methods implemented on `IoResult`, but others have more
  209. //! complex interdependencies among each I/O operation.
  210. //!
  211. //! The `try!` macro from `std::macros` is provided as a method of early-return
  212. //! inside `Result`-returning functions. It expands to an early-return on `Err`
  213. //! and otherwise unwraps the contained `Ok` value.
  214. //!
  215. //! If you wanted to read several `u32`s from a file and return their product:
  216. //!
  217. //! ```rust
  218. //! use std::old_io::{File, IoResult};
  219. //!
  220. //! fn file_product(p: &Path) -> IoResult<u32> {
  221. //! let mut f = File::open(p);
  222. //! let x1 = try!(f.read_le_u32());
  223. //! let x2 = try!(f.read_le_u32());
  224. //!
  225. //! Ok(x1 * x2)
  226. //! }
  227. //!
  228. //! match file_product(&Path::new("numbers.bin")) {
  229. //! Ok(x) => println!("{}", x),
  230. //! Err(e) => println!("Failed to read numbers!")
  231. //! }
  232. //! ```
  233. //!
  234. //! With `try!` in `file_product`, each `read_le_u32` need not be directly
  235. //! concerned with error handling; instead its caller is responsible for
  236. //! responding to errors that may occur while attempting to read the numbers.
  237. #![unstable(feature = "old_io")]
  238. #![deny(unused_must_use)]
  239. #![allow(deprecated)] // seriously this is all deprecated
  240. #![allow(unused_imports)]
  241. #![deprecated(since = "1.0.0",
  242. reasons = "APIs have been replaced with new I/O modules such as \
  243. std::{io, fs, net, process}")]
  244. pub use self::SeekStyle::*;
  245. pub use self::FileMode::*;
  246. pub use self::FileAccess::*;
  247. pub use self::IoErrorKind::*;
  248. #[cfg(stage0)]
  249. use char::CharExt;
  250. use default::Default;
  251. use error::Error;
  252. use fmt;
  253. use isize;
  254. use iter::{Iterator, IteratorExt};
  255. use marker::{PhantomFn, Sized};
  256. use mem::transmute;
  257. use ops::FnOnce;
  258. use option::Option;
  259. use option::Option::{Some, None};
  260. use os;
  261. use boxed::Box;
  262. use result::Result;
  263. use result::Result::{Ok, Err};
  264. use sys;
  265. #[cfg(stage0)]
  266. use slice::SliceExt;
  267. #[cfg(stage0)]
  268. use str::StrExt;
  269. use str;
  270. use string::String;
  271. use usize;
  272. use unicode;
  273. use vec::Vec;
  274. // Reexports
  275. pub use self::stdio::stdin;
  276. pub use self::stdio::stdout;
  277. pub use self::stdio::stderr;
  278. pub use self::stdio::print;
  279. pub use self::stdio::println;
  280. pub use self::fs::File;
  281. pub use self::timer::Timer;
  282. pub use self::net::ip::IpAddr;
  283. pub use self::net::tcp::TcpListener;
  284. pub use self::net::tcp::TcpStream;
  285. pub use self::pipe::PipeStream;
  286. pub use self::process::{Process, Command};
  287. pub use self::tempfile::TempDir;
  288. pub use self::mem::{MemReader, BufReader, MemWriter, BufWriter};
  289. pub use self::buffered::{BufferedReader, BufferedWriter, BufferedStream,
  290. LineBufferedWriter};
  291. pub use self::comm_adapters::{ChanReader, ChanWriter};
  292. mod buffered;
  293. mod comm_adapters;
  294. mod mem;
  295. mod result;
  296. mod tempfile;
  297. pub mod extensions;
  298. pub mod fs;
  299. pub mod net;
  300. pub mod pipe;
  301. pub mod process;
  302. pub mod stdio;
  303. pub mod timer;
  304. pub mod util;
  305. #[macro_use]
  306. pub mod test;
  307. /// The default buffer size for various I/O operations
  308. // libuv recommends 64k buffers to maximize throughput
  309. // https://groups.google.com/forum/#!topic/libuv/oQO1HJAIDdA
  310. const DEFAULT_BUF_SIZE: uint = 1024 * 64;
  311. /// A convenient typedef of the return value of any I/O action.
  312. pub type IoResult<T> = Result<T, IoError>;
  313. /// The type passed to I/O condition handlers to indicate error
  314. ///
  315. /// # FIXME
  316. ///
  317. /// Is something like this sufficient? It's kind of archaic
  318. #[derive(PartialEq, Eq, Clone, Debug)]
  319. pub struct IoError {
  320. /// An enumeration which can be matched against for determining the flavor
  321. /// of error.
  322. pub kind: IoErrorKind,
  323. /// A human-readable description about the error
  324. pub desc: &'static str,
  325. /// Detailed information about this error, not always available
  326. pub detail: Option<String>
  327. }
  328. impl IoError {
  329. /// Convert an `errno` value into an `IoError`.
  330. ///
  331. /// If `detail` is `true`, the `detail` field of the `IoError`
  332. /// struct is filled with an allocated string describing the error
  333. /// in more detail, retrieved from the operating system.
  334. pub fn from_errno(errno: i32, detail: bool) -> IoError {
  335. let mut err = sys::decode_error(errno as i32);
  336. if detail && err.kind == OtherIoError {
  337. err.detail = Some(os::error_string(errno).to_lowercase());
  338. }
  339. err
  340. }
  341. /// Retrieve the last error to occur as a (detailed) IoError.
  342. ///
  343. /// This uses the OS `errno`, and so there should not be any task
  344. /// descheduling or migration (other than that performed by the
  345. /// operating system) between the call(s) for which errors are
  346. /// being checked and the call of this function.
  347. pub fn last_error() -> IoError {
  348. IoError::from_errno(os::errno(), true)
  349. }
  350. }
  351. #[stable(feature = "rust1", since = "1.0.0")]
  352. impl fmt::Display for IoError {
  353. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
  354. match *self {
  355. IoError { kind: OtherIoError, desc: "unknown error", detail: Some(ref detail) } =>
  356. write!(fmt, "{}", detail),
  357. IoError { detail: None, desc, .. } =>
  358. write!(fmt, "{}", desc),
  359. IoError { detail: Some(ref detail), desc, .. } =>
  360. write!(fmt, "{} ({})", desc, detail)
  361. }
  362. }
  363. }
  364. impl Error for IoError {
  365. fn description(&self) -> &str { self.desc }
  366. }
  367. /// A list specifying general categories of I/O error.
  368. #[derive(Copy, PartialEq, Eq, Clone, Debug)]
  369. pub enum IoErrorKind {
  370. /// Any I/O error not part of this list.
  371. OtherIoError,
  372. /// The operation could not complete because end of file was reached.
  373. EndOfFile,
  374. /// The file was not found.
  375. FileNotFound,
  376. /// The file permissions disallowed access to this file.
  377. PermissionDenied,
  378. /// A network connection failed for some reason not specified in this list.
  379. ConnectionFailed,
  380. /// The network operation failed because the network connection was closed.
  381. Closed,
  382. /// The connection was refused by the remote server.
  383. ConnectionRefused,
  384. /// The connection was reset by the remote server.
  385. ConnectionReset,
  386. /// The connection was aborted (terminated) by the remote server.
  387. ConnectionAborted,
  388. /// The network operation failed because it was not connected yet.
  389. NotConnected,
  390. /// The operation failed because a pipe was closed.
  391. BrokenPipe,
  392. /// A file already existed with that name.
  393. PathAlreadyExists,
  394. /// No file exists at that location.
  395. PathDoesntExist,
  396. /// The path did not specify the type of file that this operation required. For example,
  397. /// attempting to copy a directory with the `fs::copy()` operation will fail with this error.
  398. MismatchedFileTypeForOperation,
  399. /// The operation temporarily failed (for example, because a signal was received), and retrying
  400. /// may succeed.
  401. ResourceUnavailable,
  402. /// No I/O functionality is available for this task.
  403. IoUnavailable,
  404. /// A parameter was incorrect in a way that caused an I/O error not part of this list.
  405. InvalidInput,
  406. /// The I/O operation's timeout expired, causing it to be canceled.
  407. TimedOut,
  408. /// This write operation failed to write all of its data.
  409. ///
  410. /// Normally the write() method on a Writer guarantees that all of its data
  411. /// has been written, but some operations may be terminated after only
  412. /// partially writing some data. An example of this is a timed out write
  413. /// which successfully wrote a known number of bytes, but bailed out after
  414. /// doing so.
  415. ///
  416. /// The payload contained as part of this variant is the number of bytes
  417. /// which are known to have been successfully written.
  418. ShortWrite(uint),
  419. /// The Reader returned 0 bytes from `read()` too many times.
  420. NoProgress,
  421. }
  422. /// A trait that lets you add a `detail` to an IoError easily
  423. trait UpdateIoError {
  424. /// Returns an IoError with updated description and detail
  425. fn update_err<D>(self, desc: &'static str, detail: D) -> Self where
  426. D: FnOnce(&IoError) -> String;
  427. /// Returns an IoError with updated detail
  428. fn update_detail<D>(self, detail: D) -> Self where
  429. D: FnOnce(&IoError) -> String;
  430. /// Returns an IoError with update description
  431. fn update_desc(self, desc: &'static str) -> Self;
  432. }
  433. impl<T> UpdateIoError for IoResult<T> {
  434. fn update_err<D>(self, desc: &'static str, detail: D) -> IoResult<T> where
  435. D: FnOnce(&IoError) -> String,
  436. {
  437. self.map_err(move |mut e| {
  438. let detail = detail(&e);
  439. e.desc = desc;
  440. e.detail = Some(detail);
  441. e
  442. })
  443. }
  444. fn update_detail<D>(self, detail: D) -> IoResult<T> where
  445. D: FnOnce(&IoError) -> String,
  446. {
  447. self.map_err(move |mut e| { e.detail = Some(detail(&e)); e })
  448. }
  449. fn update_desc(self, desc: &'static str) -> IoResult<T> {
  450. self.map_err(|mut e| { e.desc = desc; e })
  451. }
  452. }
  453. static NO_PROGRESS_LIMIT: uint = 1000;
  454. /// A trait for objects which are byte-oriented streams. Readers are defined by
  455. /// one method, `read`. This function will block until data is available,
  456. /// filling in the provided buffer with any data read.
  457. ///
  458. /// Readers are intended to be composable with one another. Many objects
  459. /// throughout the I/O and related libraries take and provide types which
  460. /// implement the `Reader` trait.
  461. pub trait Reader {
  462. // Only method which need to get implemented for this trait
  463. /// Read bytes, up to the length of `buf` and place them in `buf`.
  464. /// Returns the number of bytes read. The number of bytes read may
  465. /// be less than the number requested, even 0. Returns `Err` on EOF.
  466. ///
  467. /// # Error
  468. ///
  469. /// If an error occurs during this I/O operation, then it is returned as
  470. /// `Err(IoError)`. Note that end-of-file is considered an error, and can be
  471. /// inspected for in the error's `kind` field. Also note that reading 0
  472. /// bytes is not considered an error in all circumstances
  473. ///
  474. /// # Implementation Note
  475. ///
  476. /// When implementing this method on a new Reader, you are strongly encouraged
  477. /// not to return 0 if you can avoid it.
  478. fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
  479. // Convenient helper methods based on the above methods
  480. /// Reads at least `min` bytes and places them in `buf`.
  481. /// Returns the number of bytes read.
  482. ///
  483. /// This will continue to call `read` until at least `min` bytes have been
  484. /// read. If `read` returns 0 too many times, `NoProgress` will be
  485. /// returned.
  486. ///
  487. /// # Error
  488. ///
  489. /// If an error occurs at any point, that error is returned, and no further
  490. /// bytes are read.
  491. fn read_at_least(&mut self, min: uint, buf: &mut [u8]) -> IoResult<uint> {
  492. if min > buf.len() {
  493. return Err(IoError {
  494. detail: Some(String::from_str("the buffer is too short")),
  495. ..standard_error(InvalidInput)
  496. });
  497. }
  498. let mut read = 0;
  499. while read < min {
  500. let mut zeroes = 0;
  501. loop {
  502. match self.read(&mut buf[read..]) {
  503. Ok(0) => {
  504. zeroes += 1;
  505. if zeroes >= NO_PROGRESS_LIMIT {
  506. return Err(standard_error(NoProgress));
  507. }
  508. }
  509. Ok(n) => {
  510. read += n;
  511. break;
  512. }
  513. err@Err(_) => return err
  514. }
  515. }
  516. }
  517. Ok(read)
  518. }
  519. /// Reads a single byte. Returns `Err` on EOF.
  520. fn read_byte(&mut self) -> IoResult<u8> {
  521. let mut buf = [0];
  522. try!(self.read_at_least(1, &mut buf));
  523. Ok(buf[0])
  524. }
  525. /// Reads up to `len` bytes and appends them to a vector.
  526. /// Returns the number of bytes read. The number of bytes read may be
  527. /// less than the number requested, even 0. Returns Err on EOF.
  528. ///
  529. /// # Error
  530. ///
  531. /// If an error occurs during this I/O operation, then it is returned
  532. /// as `Err(IoError)`. See `read()` for more details.
  533. fn push(&mut self, len: uint, buf: &mut Vec<u8>) -> IoResult<uint> {
  534. let start_len = buf.len();
  535. buf.reserve(len);
  536. let n = {
  537. let s = unsafe { slice_vec_capacity(buf, start_len, start_len + len) };
  538. try!(self.read(s))
  539. };
  540. unsafe { buf.set_len(start_len + n) };
  541. Ok(n)
  542. }
  543. /// Reads at least `min` bytes, but no more than `len`, and appends them to
  544. /// a vector.
  545. /// Returns the number of bytes read.
  546. ///
  547. /// This will continue to call `read` until at least `min` bytes have been
  548. /// read. If `read` returns 0 too many times, `NoProgress` will be
  549. /// returned.
  550. ///
  551. /// # Error
  552. ///
  553. /// If an error occurs at any point, that error is returned, and no further
  554. /// bytes are read.
  555. fn push_at_least(&mut self, min: uint, len: uint, buf: &mut Vec<u8>) -> IoResult<uint> {
  556. if min > len {
  557. return Err(IoError {
  558. detail: Some(String::from_str("the buffer is too short")),
  559. ..standard_error(InvalidInput)
  560. });
  561. }
  562. let start_len = buf.len();
  563. buf.reserve(len);
  564. // we can't just use self.read_at_least(min, slice) because we need to push
  565. // successful reads onto the vector before any returned errors.
  566. let mut read = 0;
  567. while read < min {
  568. read += {
  569. let s = unsafe { slice_vec_capacity(buf, start_len + read, start_len + len) };
  570. try!(self.read_at_least(1, s))
  571. };
  572. unsafe { buf.set_len(start_len + read) };
  573. }
  574. Ok(read)
  575. }
  576. /// Reads exactly `len` bytes and gives you back a new vector of length
  577. /// `len`
  578. ///
  579. /// # Error
  580. ///
  581. /// Fails with the same conditions as `read`. Additionally returns error
  582. /// on EOF. Note that if an error is returned, then some number of bytes may
  583. /// have already been consumed from the underlying reader, and they are lost
  584. /// (not returned as part of the error). If this is unacceptable, then it is
  585. /// recommended to use the `push_at_least` or `read` methods.
  586. fn read_exact(&mut self, len: uint) -> IoResult<Vec<u8>> {
  587. let mut buf = Vec::with_capacity(len);
  588. match self.push_at_least(len, len, &mut buf) {
  589. Ok(_) => Ok(buf),
  590. Err(e) => Err(e),
  591. }
  592. }
  593. /// Reads all remaining bytes from the stream.
  594. ///
  595. /// # Error
  596. ///
  597. /// Returns any non-EOF error immediately. Previously read bytes are
  598. /// discarded when an error is returned.
  599. ///
  600. /// When EOF is encountered, all bytes read up to that point are returned.
  601. fn read_to_end(&mut self) -> IoResult<Vec<u8>> {
  602. let mut buf = Vec::with_capacity(DEFAULT_BUF_SIZE);
  603. loop {
  604. match self.push_at_least(1, DEFAULT_BUF_SIZE, &mut buf) {
  605. Ok(_) => {}
  606. Err(ref e) if e.kind == EndOfFile => break,
  607. Err(e) => return Err(e)
  608. }
  609. }
  610. return Ok(buf);
  611. }
  612. /// Reads all of the remaining bytes of this stream, interpreting them as a
  613. /// UTF-8 encoded stream. The corresponding string is returned.
  614. ///
  615. /// # Error
  616. ///
  617. /// This function returns all of the same errors as `read_to_end` with an
  618. /// additional error if the reader's contents are not a valid sequence of
  619. /// UTF-8 bytes.
  620. fn read_to_string(&mut self) -> IoResult<String> {
  621. self.read_to_end().and_then(|s| {
  622. match String::from_utf8(s) {
  623. Ok(s) => Ok(s),
  624. Err(_) => Err(standard_error(InvalidInput)),
  625. }
  626. })
  627. }
  628. // Byte conversion helpers
  629. /// Reads `n` little-endian unsigned integer bytes.
  630. ///
  631. /// `n` must be between 1 and 8, inclusive.
  632. fn read_le_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
  633. assert!(nbytes > 0 && nbytes <= 8);
  634. let mut val = 0;
  635. let mut pos = 0;
  636. let mut i = nbytes;
  637. while i > 0 {
  638. val += (try!(self.read_u8()) as u64) << pos;
  639. pos += 8;
  640. i -= 1;
  641. }
  642. Ok(val)
  643. }
  644. /// Reads `n` little-endian signed integer bytes.
  645. ///
  646. /// `n` must be between 1 and 8, inclusive.
  647. fn read_le_int_n(&mut self, nbytes: uint) -> IoResult<i64> {
  648. self.read_le_uint_n(nbytes).map(|i| extend_sign(i, nbytes))
  649. }
  650. /// Reads `n` big-endian unsigned integer bytes.
  651. ///
  652. /// `n` must be between 1 and 8, inclusive.
  653. fn read_be_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
  654. assert!(nbytes > 0 && nbytes <= 8);
  655. let mut val = 0;
  656. let mut i = nbytes;
  657. while i > 0 {
  658. i -= 1;
  659. val += (try!(self.read_u8()) as u64) << i * 8;
  660. }
  661. Ok(val)
  662. }
  663. /// Reads `n` big-endian signed integer bytes.
  664. ///
  665. /// `n` must be between 1 and 8, inclusive.
  666. fn read_be_int_n(&mut self, nbytes: uint) -> IoResult<i64> {
  667. self.read_be_uint_n(nbytes).map(|i| extend_sign(i, nbytes))
  668. }
  669. /// Reads a little-endian unsigned integer.
  670. ///
  671. /// The number of bytes returned is system-dependent.
  672. fn read_le_uint(&mut self) -> IoResult<uint> {
  673. self.read_le_uint_n(usize::BYTES as usize).map(|i| i as uint)
  674. }
  675. /// Reads a little-endian integer.
  676. ///
  677. /// The number of bytes returned is system-dependent.
  678. fn read_le_int(&mut self) -> IoResult<int> {
  679. self.read_le_int_n(isize::BYTES as usize).map(|i| i as int)
  680. }
  681. /// Reads a big-endian unsigned integer.
  682. ///
  683. /// The number of bytes returned is system-dependent.
  684. fn read_be_uint(&mut self) -> IoResult<uint> {
  685. self.read_be_uint_n(usize::BYTES as usize).map(|i| i as uint)
  686. }
  687. /// Reads a big-endian integer.
  688. ///
  689. /// The number of bytes returned is system-dependent.
  690. fn read_be_int(&mut self) -> IoResult<int> {
  691. self.read_be_int_n(isize::BYTES as usize).map(|i| i as int)
  692. }
  693. /// Reads a big-endian `u64`.
  694. ///
  695. /// `u64`s are 8 bytes long.
  696. fn read_be_u64(&mut self) -> IoResult<u64> {
  697. self.read_be_uint_n(8)
  698. }
  699. /// Reads a big-endian `u32`.
  700. ///
  701. /// `u32`s are 4 bytes long.
  702. fn read_be_u32(&mut self) -> IoResult<u32> {
  703. self.read_be_uint_n(4).map(|i| i as u32)
  704. }
  705. /// Reads a big-endian `u16`.
  706. ///
  707. /// `u16`s are 2 bytes long.
  708. fn read_be_u16(&mut self) -> IoResult<u16> {
  709. self.read_be_uint_n(2).map(|i| i as u16)
  710. }
  711. /// Reads a big-endian `i64`.
  712. ///
  713. /// `i64`s are 8 bytes long.
  714. fn read_be_i64(&mut self) -> IoResult<i64> {
  715. self.read_be_int_n(8)
  716. }
  717. /// Reads a big-endian `i32`.
  718. ///
  719. /// `i32`s are 4 bytes long.
  720. fn read_be_i32(&mut self) -> IoResult<i32> {
  721. self.read_be_int_n(4).map(|i| i as i32)
  722. }
  723. /// Reads a big-endian `i16`.
  724. ///
  725. /// `i16`s are 2 bytes long.
  726. fn read_be_i16(&mut self) -> IoResult<i16> {
  727. self.read_be_int_n(2).map(|i| i as i16)
  728. }
  729. /// Reads a big-endian `f64`.
  730. ///
  731. /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
  732. fn read_be_f64(&mut self) -> IoResult<f64> {
  733. self.read_be_u64().map(|i| unsafe {
  734. transmute::<u64, f64>(i)
  735. })
  736. }
  737. /// Reads a big-endian `f32`.
  738. ///
  739. /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
  740. fn read_be_f32(&mut self) -> IoResult<f32> {
  741. self.read_be_u32().map(|i| unsafe {
  742. transmute::<u32, f32>(i)
  743. })
  744. }
  745. /// Reads a little-endian `u64`.
  746. ///
  747. /// `u64`s are 8 bytes long.
  748. fn read_le_u64(&mut self) -> IoResult<u64> {
  749. self.read_le_uint_n(8)
  750. }
  751. /// Reads a little-endian `u32`.
  752. ///
  753. /// `u32`s are 4 bytes long.
  754. fn read_le_u32(&mut self) -> IoResult<u32> {
  755. self.read_le_uint_n(4).map(|i| i as u32)
  756. }
  757. /// Reads a little-endian `u16`.
  758. ///
  759. /// `u16`s are 2 bytes long.
  760. fn read_le_u16(&mut self) -> IoResult<u16> {
  761. self.read_le_uint_n(2).map(|i| i as u16)
  762. }
  763. /// Reads a little-endian `i64`.
  764. ///
  765. /// `i64`s are 8 bytes long.
  766. fn read_le_i64(&mut self) -> IoResult<i64> {
  767. self.read_le_int_n(8)
  768. }
  769. /// Reads a little-endian `i32`.
  770. ///
  771. /// `i32`s are 4 bytes long.
  772. fn read_le_i32(&mut self) -> IoResult<i32> {
  773. self.read_le_int_n(4).map(|i| i as i32)
  774. }
  775. /// Reads a little-endian `i16`.
  776. ///
  777. /// `i16`s are 2 bytes long.
  778. fn read_le_i16(&mut self) -> IoResult<i16> {
  779. self.read_le_int_n(2).map(|i| i as i16)
  780. }
  781. /// Reads a little-endian `f64`.
  782. ///
  783. /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
  784. fn read_le_f64(&mut self) -> IoResult<f64> {
  785. self.read_le_u64().map(|i| unsafe {
  786. transmute::<u64, f64>(i)
  787. })
  788. }
  789. /// Reads a little-endian `f32`.
  790. ///
  791. /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
  792. fn read_le_f32(&mut self) -> IoResult<f32> {
  793. self.read_le_u32().map(|i| unsafe {
  794. transmute::<u32, f32>(i)
  795. })
  796. }
  797. /// Read a u8.
  798. ///
  799. /// `u8`s are 1 byte.
  800. fn read_u8(&mut self) -> IoResult<u8> {
  801. self.read_byte()
  802. }
  803. /// Read an i8.
  804. ///
  805. /// `i8`s are 1 byte.
  806. fn read_i8(&mut self) -> IoResult<i8> {
  807. self.read_byte().map(|i| i as i8)
  808. }
  809. }
  810. /// A reader which can be converted to a RefReader.
  811. pub trait ByRefReader {
  812. /// Creates a wrapper around a mutable reference to the reader.
  813. ///
  814. /// This is useful to allow applying adaptors while still
  815. /// retaining ownership of the original value.
  816. fn by_ref<'a>(&'a mut self) -> RefReader<'a, Self>;
  817. }
  818. impl<T: Reader> ByRefReader for T {
  819. fn by_ref<'a>(&'a mut self) -> RefReader<'a, T> {
  820. RefReader { inner: self }
  821. }
  822. }
  823. /// A reader which can be converted to bytes.
  824. pub trait BytesReader {
  825. /// Create an iterator that reads a single byte on
  826. /// each iteration, until EOF.
  827. ///
  828. /// # Error
  829. ///
  830. /// Any error other than `EndOfFile` that is produced by the underlying Reader
  831. /// is returned by the iterator and should be handled by the caller.
  832. fn bytes<'r>(&'r mut self) -> extensions::Bytes<'r, Self>;
  833. }
  834. impl<T: Reader> BytesReader for T {
  835. fn bytes<'r>(&'r mut self) -> extensions::Bytes<'r, T> {
  836. extensions::Bytes::new(self)
  837. }
  838. }
  839. impl<'a> Reader for Box<Reader+'a> {
  840. fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
  841. let reader: &mut Reader = &mut **self;
  842. reader.read(buf)
  843. }
  844. }
  845. impl<'a> Reader for &'a mut (Reader+'a) {
  846. fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { (*self).read(buf) }
  847. }
  848. /// Returns a slice of `v` between `start` and `end`.
  849. ///
  850. /// Similar to `slice()` except this function only bounds the slice on the
  851. /// capacity of `v`, not the length.
  852. ///
  853. /// # Panics
  854. ///
  855. /// Panics when `start` or `end` point outside the capacity of `v`, or when
  856. /// `start` > `end`.
  857. // Private function here because we aren't sure if we want to expose this as
  858. // API yet. If so, it should be a method on Vec.
  859. unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -> &'a mut [T] {
  860. use slice;
  861. #[cfg(stage0)]
  862. use ptr::PtrExt;
  863. assert!(start <= end);
  864. assert!(end <= v.capacity());
  865. slice::from_raw_parts_mut(
  866. v.as_mut_ptr().offset(start as int),
  867. end - start
  868. )
  869. }
  870. /// A `RefReader` is a struct implementing `Reader` which contains a reference
  871. /// to another reader. This is often useful when composing streams.
  872. ///
  873. /// # Examples
  874. ///
  875. /// ```
  876. /// use std::old_io as io;
  877. /// use std::old_io::ByRefReader;
  878. /// use std::old_io::util::LimitReader;
  879. ///
  880. /// fn process_input<R: Reader>(r: R) {}
  881. ///
  882. /// let mut stream = io::stdin();
  883. ///
  884. /// // Only allow the function to process at most one kilobyte of input
  885. /// {
  886. /// let stream = LimitReader::new(stream.by_ref(), 1024);
  887. /// process_input(stream);
  888. /// }
  889. ///
  890. /// // 'stream' is still available for use here
  891. /// ```
  892. pub struct RefReader<'a, R:'a> {
  893. /// The underlying reader which this is referencing
  894. inner: &'a mut R
  895. }
  896. impl<'a, R: Reader> Reader for RefReader<'a, R> {
  897. fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.inner.read(buf) }
  898. }
  899. impl<'a, R: Buffer> Buffer for RefReader<'a, R> {
  900. fn fill_buf(&mut self) -> IoResult<&[u8]> { self.inner.fill_buf() }
  901. fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
  902. }
  903. fn extend_sign(val: u64, nbytes: uint) -> i64 {
  904. let shift = (8 - nbytes) * 8;
  905. (val << shift) as i64 >> shift
  906. }
  907. /// A trait for objects which are byte-oriented streams. Writers are defined by
  908. /// one method, `write`. This function will block until the provided buffer of
  909. /// bytes has been entirely written, and it will return any failures which occur.
  910. ///
  911. /// Another commonly overridden method is the `flush` method for writers such as
  912. /// buffered writers.
  913. ///
  914. /// Writers are intended to be composable with one another. Many objects
  915. /// throughout the I/O and related libraries take and provide types which
  916. /// implement the `Writer` trait.
  917. pub trait Writer {
  918. /// Write the entirety of a given buffer
  919. ///
  920. /// # Errors
  921. ///
  922. /// If an error happens during the I/O operation, the error is returned as
  923. /// `Err`. Note that it is considered an error if the entire buffer could
  924. /// not be written, and if an error is returned then it is unknown how much
  925. /// data (if any) was actually written.
  926. fn write_all(&mut self, buf: &[u8]) -> IoResult<()>;
  927. /// Deprecated, this method was renamed to `write_all`
  928. #[unstable(feature = "io")]
  929. #[deprecated(since = "1.0.0", reason = "renamed to `write_all`")]
  930. fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.write_all(buf) }
  931. /// Flush this output stream, ensuring that all intermediately buffered
  932. /// contents reach their destination.
  933. ///
  934. /// This is by default a no-op and implementers of the `Writer` trait should
  935. /// decide whether their stream needs to be buffered or not.
  936. fn flush(&mut self) -> IoResult<()> { Ok(()) }
  937. /// Writes a formatted string into this writer, returning any error
  938. /// encountered.
  939. ///
  940. /// This method is primarily used to interface with the `format_args!`
  941. /// macro, but it is rare that this should explicitly be called. The
  942. /// `write!` macro should be favored to invoke this method instead.
  943. ///
  944. /// # Errors
  945. ///
  946. /// This function will return any I/O error reported while formatting.
  947. fn write_fmt(&mut self, fmt: fmt::Arguments) -> IoResult<()> {
  948. // Create a shim which translates a Writer to a fmt::Write and saves
  949. // off I/O errors. instead of discarding them
  950. struct Adaptor<'a, T: ?Sized +'a> {
  951. inner: &'a mut T,
  952. error: IoResult<()>,
  953. }
  954. impl<'a, T: ?Sized + Writer> fmt::Write for Adaptor<'a, T> {
  955. fn write_str(&mut self, s: &str) -> fmt::Result {
  956. match self.inner.write_all(s.as_bytes()) {
  957. Ok(()) => Ok(()),
  958. Err(e) => {
  959. self.error = Err(e);
  960. Err(fmt::Error)
  961. }
  962. }
  963. }
  964. }
  965. let mut output = Adaptor { inner: self, error: Ok(()) };
  966. match fmt::write(&mut output, fmt) {
  967. Ok(()) => Ok(()),
  968. Err(..) => output.error
  969. }
  970. }
  971. /// Write a rust string into this sink.
  972. ///
  973. /// The bytes written will be the UTF-8 encoded version of the input string.
  974. /// If other encodings are desired, it is recommended to compose this stream
  975. /// with another performing the conversion, or to use `write` with a
  976. /// converted byte-array instead.
  977. #[inline]
  978. fn write_str(&mut self, s: &str) -> IoResult<()> {
  979. self.write_all(s.as_bytes())
  980. }
  981. /// Writes a string into this sink, and then writes a literal newline (`\n`)
  982. /// byte afterwards. Note that the writing of the newline is *not* atomic in
  983. /// the sense that the call to `write` is invoked twice (once with the
  984. /// string and once with a newline character).
  985. ///
  986. /// If other encodings or line ending flavors are desired, it is recommended
  987. /// that the `write` method is used specifically instead.
  988. #[inline]
  989. fn write_line(&mut self, s: &str) -> IoResult<()> {
  990. self.write_str(s).and_then(|()| self.write_all(&[b'\n']))
  991. }
  992. /// Write a single char, encoded as UTF-8.
  993. #[inline]
  994. fn write_char(&mut self, c: char) -> IoResult<()> {
  995. let mut buf = [0; 4];
  996. let n = c.encode_utf8(&mut buf).unwrap_or(0);
  997. self.write_all(&buf[..n])
  998. }
  999. /// Write the result of passing n through `int::to_str_bytes`.
  1000. #[inline]
  1001. fn write_int(&mut self, n: int) -> IoResult<()> {
  1002. write!(self, "{}", n)
  1003. }
  1004. /// Write the result of passing n through `uint::to_str_bytes`.
  1005. #[inline]
  1006. fn write_uint(&mut self, n: uint) -> IoResult<()> {
  1007. write!(self, "{}", n)
  1008. }
  1009. /// Write a little-endian uint (number of bytes depends on system).
  1010. #[inline]
  1011. fn write_le_uint(&mut self, n: uint) -> IoResult<()> {
  1012. extensions::u64_to_le_bytes(n as u64, usize::BYTES as usize, |v| self.write_all(v))
  1013. }
  1014. /// Write a little-endian int (number of bytes depends on system).
  1015. #[inline]
  1016. fn write_le_int(&mut self, n: int) -> IoResult<()> {
  1017. extensions::u64_to_le_bytes(n as u64, isize::BYTES as usize, |v| self.write_all(v))
  1018. }
  1019. /// Write a big-endian uint (number of bytes depends on system).
  1020. #[inline]
  1021. fn write_be_uint(&mut self, n: uint) -> IoResult<()> {
  1022. extensions::u64_to_be_bytes(n as u64, usize::BYTES as usize, |v| self.write_all(v))
  1023. }
  1024. /// Write a big-endian int (number of bytes depends on system).
  1025. #[inline]
  1026. fn write_be_int(&mut self, n: int) -> IoResult<()> {
  1027. extensions::u64_to_be_bytes(n as u64, isize::BYTES as usize, |v| self.write_all(v))
  1028. }
  1029. /// Write a big-endian u64 (8 bytes).
  1030. #[inline]
  1031. fn write_be_u64(&mut self, n: u64) -> IoResult<()> {
  1032. extensions::u64_to_be_bytes(n, 8, |v| self.write_all(v))
  1033. }
  1034. /// Write a big-endian u32 (4 bytes).
  1035. #[inline]
  1036. fn write_be_u32(&mut self, n: u32) -> IoResult<()> {
  1037. extensions::u64_to_be_bytes(n as u64, 4, |v| self.write_all(v))
  1038. }
  1039. /// Write a big-endian u16 (2 bytes).
  1040. #[inline]
  1041. fn write_be_u16(&mut self, n: u16) -> IoResult<()> {
  1042. extensions::u64_to_be_bytes(n as u64, 2, |v| self.write_all(v))
  1043. }
  1044. /// Write a big-endian i64 (8 bytes).
  1045. #[inline]
  1046. fn write_be_i64(&mut self, n: i64) -> IoResult<()> {
  1047. extensions::u64_to_be_bytes(n as u64, 8, |v| self.write_all(v))
  1048. }
  1049. /// Write a big-endian i32 (4 bytes).
  1050. #[inline]
  1051. fn write_be_i32(&mut self, n: i32) -> IoResult<()> {
  1052. extensions::u64_to_be_bytes(n as u64, 4, |v| self.write_all(v))
  1053. }
  1054. /// Write a big-endian i16 (2 bytes).
  1055. #[inline]
  1056. fn write_be_i16(&mut self, n: i16) -> IoResult<()> {
  1057. extensions::u64_to_be_bytes(n as u64, 2, |v| self.write_all(v))
  1058. }
  1059. /// Write a big-endian IEEE754 double-precision floating-point (8 bytes).
  1060. #[inline]
  1061. fn write_be_f64(&mut self, f: f64) -> IoResult<()> {
  1062. unsafe {
  1063. self.write_be_u64(transmute(f))
  1064. }
  1065. }
  1066. /// Write a big-endian IEEE754 single-precision floating-point (4 bytes).
  1067. #[inline]
  1068. fn write_be_f32(&mut self, f: f32) -> IoResult<()> {
  1069. unsafe {
  1070. self.write_be_u32(transmute(f))
  1071. }
  1072. }
  1073. /// Write a little-endian u64 (8 bytes).
  1074. #[inline]
  1075. fn write_le_u64(&mut self, n: u64) -> IoResult<()> {
  1076. extensions::u64_to_le_bytes(n, 8, |v| self.write_all(v))
  1077. }
  1078. /// Write a little-endian u32 (4 bytes).
  1079. #[inline]
  1080. fn write_le_u32(&mut self, n: u32) -> IoResult<()> {
  1081. extensions::u64_to_le_bytes(n as u64, 4, |v| self.write_all(v))
  1082. }
  1083. /// Write a little-endian u16 (2 bytes).
  1084. #[inline]
  1085. fn write_le_u16(&mut self, n: u16) -> IoResult<()> {
  1086. extensions::u64_to_le_bytes(n as u64, 2, |v| self.write_all(v))
  1087. }
  1088. /// Write a little-endian i64 (8 bytes).
  1089. #[inline]
  1090. fn write_le_i64(&mut self, n: i64) -> IoResult<()> {
  1091. extensions::u64_to_le_bytes(n as u64, 8, |v| self.write_all(v))
  1092. }
  1093. /// Write a little-endian i32 (4 bytes).
  1094. #[inline]
  1095. fn write_le_i32(&mut self, n: i32) -> IoResult<()> {
  1096. extensions::u64_to_le_bytes(n as u64, 4, |v| self.write_all(v))
  1097. }
  1098. /// Write a little-endian i16 (2 bytes).
  1099. #[inline]
  1100. fn write_le_i16(&mut self, n: i16) -> IoResult<()> {
  1101. extensions::u64_to_le_bytes(n as u64, 2, |v| self.write_all(v))
  1102. }
  1103. /// Write a little-endian IEEE754 double-precision floating-point
  1104. /// (8 bytes).
  1105. #[inline]
  1106. fn write_le_f64(&mut self, f: f64) -> IoResult<()> {
  1107. unsafe {
  1108. self.write_le_u64(transmute(f))
  1109. }
  1110. }
  1111. /// Write a little-endian IEEE754 single-precision floating-point
  1112. /// (4 bytes).
  1113. #[inline]
  1114. fn write_le_f32(&mut self, f: f32) -> IoResult<()> {
  1115. unsafe {
  1116. self.write_le_u32(transmute(f))
  1117. }
  1118. }
  1119. /// Write a u8 (1 byte).
  1120. #[inline]
  1121. fn write_u8(&mut self, n: u8) -> IoResult<()> {
  1122. self.write_all(&[n])
  1123. }
  1124. /// Write an i8 (1 byte).
  1125. #[inline]
  1126. fn write_i8(&mut self, n: i8) -> IoResult<()> {
  1127. self.write_all(&[n as u8])
  1128. }
  1129. }
  1130. /// A writer which can be converted to a RefWriter.
  1131. pub trait ByRefWriter {
  1132. /// Creates a wrapper around a mutable reference to the writer.
  1133. ///
  1134. /// This is useful to allow applying wrappers while still
  1135. /// retaining ownership of the original value.
  1136. #[inline]
  1137. fn by_ref<'a>(&'a mut self) -> RefWriter<'a, Self>;
  1138. }
  1139. impl<T: Writer> ByRefWriter for T {
  1140. fn by_ref<'a>(&'a mut self) -> RefWriter<'a, T> {
  1141. RefWriter { inner: self }
  1142. }
  1143. }
  1144. impl<'a> Writer for Box<Writer+'a> {
  1145. #[inline]
  1146. fn write_all(&mut self, buf: &[u8]) -> IoResult<()> {
  1147. (&mut **self).write_all(buf)
  1148. }
  1149. #[inline]
  1150. fn flush(&mut self) -> IoResult<()> {
  1151. (&mut **self).flush()
  1152. }
  1153. }
  1154. impl<'a> Writer for &'a mut (Writer+'a) {
  1155. #[inline]
  1156. fn write_all(&mut self, buf: &[u8]) -> IoResult<()> { (**self).write_all(buf) }
  1157. #[inline]
  1158. fn flush(&mut self) -> IoResult<()> { (**self).flush() }
  1159. }
  1160. /// A `RefWriter` is a struct implementing `Writer` which contains a reference
  1161. /// to another writer. This is often useful when composing streams.
  1162. ///
  1163. /// # Examples
  1164. ///
  1165. /// ```
  1166. /// use std::old_io::util::TeeReader;
  1167. /// use std::old_io::{stdin, ByRefWriter};
  1168. ///
  1169. /// fn process_input<R: Reader>(r: R) {}
  1170. ///
  1171. /// let mut output = Vec::new();
  1172. ///
  1173. /// {
  1174. /// // Don't give ownership of 'output' to the 'tee'. Instead we keep a
  1175. /// // handle to it in the outer scope
  1176. /// let mut tee = TeeReader::new(stdin(), output.by_ref());
  1177. /// process_input(tee);
  1178. /// }
  1179. ///
  1180. /// println!("input processed: {:?}", output);
  1181. /// ```
  1182. pub struct RefWriter<'a, W:'a> {
  1183. /// The underlying writer which this is referencing
  1184. inner: &'a mut W
  1185. }
  1186. impl<'a, W: Writer> Writer for RefWriter<'a, W> {
  1187. #[inline]
  1188. fn write_all(&mut self, buf: &[u8]) -> IoResult<()> { self.inner.write_all(buf) }
  1189. #[inline]
  1190. fn flush(&mut self) -> IoResult<()> { self.inner.flush() }
  1191. }
  1192. /// A Stream is a readable and a writable object. Data written is typically
  1193. /// received by the object which reads receive data from.
  1194. pub trait Stream: Reader + Writer { }
  1195. impl<T: Reader + Writer> Stream for T {}
  1196. /// An iterator that reads a line on each iteration,
  1197. /// until `.read_line()` encounters `EndOfFile`.
  1198. ///
  1199. /// # Notes about the Iteration Protocol
  1200. ///
  1201. /// The `Lines` may yield `None` and thus terminate
  1202. /// an iteration, but continue to yield elements if iteration
  1203. /// is attempted again.
  1204. ///
  1205. /// # Error
  1206. ///
  1207. /// Any error other than `EndOfFile` that is produced by the underlying Reader
  1208. /// is returned by the iterator and should be handled by the caller.
  1209. pub struct Lines<'r, T:'r> {
  1210. buffer: &'r mut T,
  1211. }
  1212. impl<'r, T: Buffer> Iterator for Lines<'r, T> {
  1213. type Item = IoResult<String>;
  1214. fn next(&mut self) -> Option<IoResult<String>> {
  1215. match self.buffer.read_line() {
  1216. Ok(x) => Some(Ok(x)),
  1217. Err(IoError { kind: EndOfFile, ..}) => None,
  1218. Err(y) => Some(Err(y))
  1219. }
  1220. }
  1221. }
  1222. /// An iterator that reads a utf8-encoded character on each iteration,
  1223. /// until `.read_char()` encounters `EndOfFile`.
  1224. ///
  1225. /// # Notes about the Iteration Protocol
  1226. ///
  1227. /// The `Chars` may yield `None` and thus terminate
  1228. /// an iteration, but continue to yield elements if iteration
  1229. /// is attempted again.
  1230. ///
  1231. /// # Error
  1232. ///
  1233. /// Any error other than `EndOfFile` that is produced by the underlying Reader
  1234. /// is returned by the iterator and should be handled by the caller.
  1235. pub struct Chars<'r, T:'r> {
  1236. buffer: &'r mut T
  1237. }
  1238. impl<'r, T: Buffer> Iterator for Chars<'r, T> {
  1239. type Item = IoResult<char>;
  1240. fn next(&mut self) -> Option<IoResult<char>> {
  1241. match self.buffer.read_char() {
  1242. Ok(x) => Some(Ok(x)),
  1243. Err(IoError { kind: EndOfFile, ..}) => None,
  1244. Err(y) => Some(Err(y))
  1245. }
  1246. }
  1247. }
  1248. /// A Buffer is a type of reader which has some form of internal buffering to
  1249. /// allow certain kinds of reading operations to be more optimized than others.
  1250. /// This type extends the `Reader` trait with a few methods that are not
  1251. /// possible to reasonably implement with purely a read interface.
  1252. pub trait Buffer: Reader {
  1253. /// Fills the internal buffer of this object, returning the buffer contents.
  1254. /// Note that none of the contents will be "read" in the sense that later
  1255. /// calling `read` may return the same contents.
  1256. ///
  1257. /// The `consume` function must be called with the number of bytes that are
  1258. /// consumed from this buffer returned to ensure that the bytes are never
  1259. /// returned twice.
  1260. ///
  1261. /// # Error
  1262. ///
  1263. /// This function will return an I/O error if the underlying reader was
  1264. /// read, but returned an error. Note that it is not an error to return a
  1265. /// 0-length buffer.
  1266. fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]>;
  1267. /// Tells this buffer that `amt` bytes have been consumed from the buffer,
  1268. /// so they should no longer be returned in calls to `read`.
  1269. fn consume(&mut self, amt: uint);
  1270. /// Reads the next line of input, interpreted as a sequence of UTF-8
  1271. /// encoded Unicode codepoints. If a newline is encountered, then the
  1272. /// newline is contained in the returned string.
  1273. ///
  1274. /// # Examples
  1275. ///
  1276. /// ```
  1277. /// use std::old_io::BufReader;
  1278. ///
  1279. /// let mut reader = BufReader::new(b"hello\nworld");
  1280. /// assert_eq!("hello\n", &*reader.read_line().unwrap());
  1281. /// ```
  1282. ///
  1283. /// # Error
  1284. ///
  1285. /// This function has the same error semantics as `read_until`:
  1286. ///
  1287. /// * All non-EOF errors will be returned immediately
  1288. /// * If an error is returned previously consumed bytes are lost
  1289. /// * EOF is only returned if no bytes have been read
  1290. /// * Reach EOF may mean that the delimiter is not present in the return
  1291. /// value
  1292. ///
  1293. /// Additionally, this function can fail if the line of input read is not a
  1294. /// valid UTF-8 sequence of bytes.
  1295. fn read_line(&mut self) -> IoResult<String> {
  1296. self.read_until(b'\n').and_then(|line|
  1297. match String::from_utf8(line) {
  1298. Ok(s) => Ok(s),
  1299. Err(_) => Err(standard_error(InvalidInput)),
  1300. }
  1301. )
  1302. }
  1303. /// Reads a sequence of bytes leading up to a specified delimiter. Once the
  1304. /// specified byte is encountered, reading ceases and the bytes up to and
  1305. /// including the delimiter are returned.
  1306. ///
  1307. /// # Error
  1308. ///
  1309. /// If any I/O error is encountered other than EOF, the error is immediately
  1310. /// returned. Note that this may discard bytes which have already been read,
  1311. /// and those bytes will *not* be returned. It is recommended to use other
  1312. /// methods if this case is worrying.
  1313. ///
  1314. /// If EOF is encountered, then this function will return EOF if 0 bytes
  1315. /// have been read, otherwise the pending byte buffer is returned. This
  1316. /// is the reason that the byte buffer returned may not always contain the
  1317. /// delimiter.
  1318. fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> {
  1319. let mut res = Vec::new();
  1320. loop {
  1321. let (done, used) = {
  1322. let available = match self.fill_buf() {
  1323. Ok(n) => n,
  1324. Err(ref e) if res.len() > 0 && e.kind == EndOfFile => {
  1325. return Ok(res);
  1326. }
  1327. Err(e) => return Err(e)
  1328. };
  1329. match available.iter().position(|&b| b == byte) {
  1330. Some(i) => {
  1331. res.push_all(&available[..i + 1]);
  1332. (true, i + 1)
  1333. }
  1334. None => {
  1335. res.push_all(available);
  1336. (false, available.len())
  1337. }
  1338. }
  1339. };
  1340. self.consume(used);
  1341. if done {
  1342. return Ok(res);
  1343. }
  1344. }
  1345. }
  1346. /// Reads the next utf8-encoded character from the underlying stream.
  1347. ///
  1348. /// # Error
  1349. ///
  1350. /// If an I/O error occurs, or EOF, then this function will return `Err`.
  1351. /// This function will also return error if the stream does not contain a
  1352. /// valid utf-8 encoded codepoint as the next few bytes in the stream.
  1353. fn read_char(&mut self) -> IoResult<char> {
  1354. let first_byte = try!(self.read_byte());
  1355. let width = unicode::str::utf8_char_width(first_byte);
  1356. if width == 1 { return Ok(first_byte as char) }
  1357. if width == 0 { return Err(standard_error(InvalidInput)) } // not utf8
  1358. let mut buf = [first_byte, 0, 0, 0];
  1359. {
  1360. let mut start = 1;
  1361. while start < width {
  1362. match try!(self.read(&mut buf[start .. width])) {
  1363. n if n == width - start => break,
  1364. n if n < width -

Large files files are truncated, but you can click here to view the full file