PageRenderTime 86ms CodeModel.GetById 32ms RepoModel.GetById 1ms 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
  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 - start => { start += n; }
  1365. _ => return Err(standard_error(InvalidInput)),
  1366. }
  1367. }
  1368. }
  1369. match str::from_utf8(&buf[..width]).ok() {
  1370. Some(s) => Ok(s.char_at(0)),
  1371. None => Err(standard_error(InvalidInput))
  1372. }
  1373. }
  1374. }
  1375. /// Extension methods for the Buffer trait which are included in the prelude.
  1376. pub trait BufferPrelude {
  1377. /// Create an iterator that reads a utf8-encoded character on each iteration
  1378. /// until EOF.
  1379. ///
  1380. /// # Error
  1381. ///
  1382. /// Any error other than `EndOfFile` that is produced by the underlying Reader
  1383. /// is returned by the iterator and should be handled by the caller.
  1384. fn chars<'r>(&'r mut self) -> Chars<'r, Self>;
  1385. /// Create an iterator that reads a line on each iteration until EOF.
  1386. ///
  1387. /// # Error
  1388. ///
  1389. /// Any error other than `EndOfFile` that is produced by the underlying Reader
  1390. /// is returned by the iterator and should be handled by the caller.
  1391. fn lines<'r>(&'r mut self) -> Lines<'r, Self>;
  1392. }
  1393. impl<T: Buffer> BufferPrelude for T {
  1394. fn chars<'r>(&'r mut self) -> Chars<'r, T> {
  1395. Chars { buffer: self }
  1396. }
  1397. fn lines<'r>(&'r mut self) -> Lines<'r, T> {
  1398. Lines { buffer: self }
  1399. }
  1400. }
  1401. /// When seeking, the resulting cursor is offset from a base by the offset given
  1402. /// to the `seek` function. The base used is specified by this enumeration.
  1403. #[derive(Copy)]
  1404. pub enum SeekStyle {
  1405. /// Seek from the beginning of the stream
  1406. SeekSet,
  1407. /// Seek from the end of the stream
  1408. SeekEnd,
  1409. /// Seek from the current position
  1410. SeekCur,
  1411. }
  1412. /// An object implementing `Seek` internally has some form of cursor which can
  1413. /// be moved within a stream of bytes. The stream typically has a fixed size,
  1414. /// allowing seeking relative to either end.
  1415. pub trait Seek {
  1416. /// Return position of file cursor in the stream
  1417. fn tell(&self) -> IoResult<u64>;
  1418. /// Seek to an offset in a stream
  1419. ///
  1420. /// A successful seek clears the EOF indicator. Seeking beyond EOF is
  1421. /// allowed, but seeking before position 0 is not allowed.
  1422. ///
  1423. /// # Errors
  1424. ///
  1425. /// * Seeking to a negative offset is considered an error
  1426. /// * Seeking past the end of the stream does not modify the underlying
  1427. /// stream, but the next write may cause the previous data to be filled in
  1428. /// with a bit pattern.
  1429. fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>;
  1430. }
  1431. /// A listener is a value that can consume itself to start listening for
  1432. /// connections.
  1433. ///
  1434. /// Doing so produces some sort of Acceptor.
  1435. pub trait Listener<T, A: Acceptor<T>>
  1436. : PhantomFn<T,T> // FIXME should be an assoc type anyhow
  1437. {
  1438. /// Spin up the listener and start queuing incoming connections
  1439. ///
  1440. /// # Error
  1441. ///
  1442. /// Returns `Err` if this listener could not be bound to listen for
  1443. /// connections. In all cases, this listener is consumed.
  1444. fn listen(self) -> IoResult<A>;
  1445. }
  1446. /// An acceptor is a value that presents incoming connections
  1447. pub trait Acceptor<T> {
  1448. /// Wait for and accept an incoming connection
  1449. ///
  1450. /// # Error
  1451. ///
  1452. /// Returns `Err` if an I/O error is encountered.
  1453. fn accept(&mut self) -> IoResult<T>;
  1454. /// Create an iterator over incoming connection attempts.
  1455. ///
  1456. /// Note that I/O errors will be yielded by the iterator itself.
  1457. fn incoming<'r>(&'r mut self) -> IncomingConnections<'r, Self> {
  1458. IncomingConnections { inc: self }
  1459. }
  1460. }
  1461. /// An infinite iterator over incoming connection attempts.
  1462. /// Calling `next` will block the task until a connection is attempted.
  1463. ///
  1464. /// Since connection attempts can continue forever, this iterator always returns
  1465. /// `Some`. The `Some` contains the `IoResult` representing whether the
  1466. /// connection attempt was successful. A successful connection will be wrapped
  1467. /// in `Ok`. A failed connection is represented as an `Err`.
  1468. pub struct IncomingConnections<'a, A: ?Sized +'a> {
  1469. inc: &'a mut A,
  1470. }
  1471. #[old_impl_check]
  1472. impl<'a, T, A: ?Sized + Acceptor<T>> Iterator for IncomingConnections<'a, A> {
  1473. type Item = IoResult<T>;
  1474. fn next(&mut self) -> Option<IoResult<T>> {
  1475. Some(self.inc.accept())
  1476. }
  1477. }
  1478. /// Creates a standard error for a commonly used flavor of error. The `detail`
  1479. /// field of the returned error will always be `None`.
  1480. ///
  1481. /// # Examples
  1482. ///
  1483. /// ```
  1484. /// use std::old_io as io;
  1485. ///
  1486. /// let eof = io::standard_error(io::EndOfFile);
  1487. /// let einval = io::standard_error(io::InvalidInput);
  1488. /// ```
  1489. pub fn standard_error(kind: IoErrorKind) -> IoError {
  1490. let desc = match kind {
  1491. EndOfFile => "end of file",
  1492. IoUnavailable => "I/O is unavailable",
  1493. InvalidInput => "invalid input",
  1494. OtherIoError => "unknown I/O error",
  1495. FileNotFound => "file not found",
  1496. PermissionDenied => "permission denied",
  1497. ConnectionFailed => "connection failed",
  1498. Closed => "stream is closed",
  1499. ConnectionRefused => "connection refused",
  1500. ConnectionReset => "connection reset",
  1501. ConnectionAborted => "connection aborted",
  1502. NotConnected => "not connected",
  1503. BrokenPipe => "broken pipe",
  1504. PathAlreadyExists => "file already exists",
  1505. PathDoesntExist => "no such file",
  1506. MismatchedFileTypeForOperation => "mismatched file type",
  1507. ResourceUnavailable => "resource unavailable",
  1508. TimedOut => "operation timed out",
  1509. ShortWrite(..) => "short write",
  1510. NoProgress => "no progress",
  1511. };
  1512. IoError {
  1513. kind: kind,
  1514. desc: desc,
  1515. detail: None,
  1516. }
  1517. }
  1518. /// A mode specifies how a file should be opened or created. These modes are
  1519. /// passed to `File::open_mode` and are used to control where the file is
  1520. /// positioned when it is initially opened.
  1521. #[derive(Copy, Clone, PartialEq, Eq, Debug)]
  1522. pub enum FileMode {
  1523. /// Opens a file positioned at the beginning.
  1524. Open,
  1525. /// Opens a file positioned at EOF.
  1526. Append,
  1527. /// Opens a file, truncating it if it already exists.
  1528. Truncate,
  1529. }
  1530. /// Access permissions with which the file should be opened. `File`s
  1531. /// opened with `Read` will return an error if written to.
  1532. #[derive(Copy, Clone, PartialEq, Eq, Debug)]
  1533. pub enum FileAccess {
  1534. /// Read-only access, requests to write will result in an error
  1535. Read,
  1536. /// Write-only access, requests to read will result in an error
  1537. Write,
  1538. /// Read-write access, no requests are denied by default
  1539. ReadWrite,
  1540. }
  1541. /// Different kinds of files which can be identified by a call to stat
  1542. #[derive(Copy, PartialEq, Debug, Hash, Clone)]
  1543. pub enum FileType {
  1544. /// This is a normal file, corresponding to `S_IFREG`
  1545. RegularFile,
  1546. /// This file is a directory, corresponding to `S_IFDIR`
  1547. Directory,
  1548. /// This file is a named pipe, corresponding to `S_IFIFO`
  1549. NamedPipe,
  1550. /// This file is a block device, corresponding to `S_IFBLK`
  1551. BlockSpecial,
  1552. /// This file is a symbolic link to another file, corresponding to `S_IFLNK`
  1553. Symlink,
  1554. /// The type of this file is not recognized as one of the other categories
  1555. Unknown,
  1556. }
  1557. /// A structure used to describe metadata information about a file. This
  1558. /// structure is created through the `stat` method on a `Path`.
  1559. ///
  1560. /// # Examples
  1561. ///
  1562. /// ```no_run
  1563. ///
  1564. /// use std::old_io::fs::PathExtensions;
  1565. ///
  1566. /// let info = match Path::new("foo.txt").stat() {
  1567. /// Ok(stat) => stat,
  1568. /// Err(e) => panic!("couldn't read foo.txt: {}", e),
  1569. /// };
  1570. ///
  1571. /// println!("byte size: {}", info.size);
  1572. /// ```
  1573. #[derive(Copy, Hash)]
  1574. pub struct FileStat {
  1575. /// The size of the file, in bytes
  1576. pub size: u64,
  1577. /// The kind of file this path points to (directory, file, pipe, etc.)
  1578. pub kind: FileType,
  1579. /// The file permissions currently on the file
  1580. pub perm: FilePermission,
  1581. // FIXME(#10301): These time fields are pretty useless without an actual
  1582. // time representation, what are the milliseconds relative
  1583. // to?
  1584. /// The time that the file was created at, in platform-dependent
  1585. /// milliseconds
  1586. pub created: u64,
  1587. /// The time that this file was last modified, in platform-dependent
  1588. /// milliseconds
  1589. pub modified: u64,
  1590. /// The time that this file was last accessed, in platform-dependent
  1591. /// milliseconds
  1592. pub accessed: u64,
  1593. /// Information returned by stat() which is not guaranteed to be
  1594. /// platform-independent. This information may be useful on some platforms,
  1595. /// but it may have different meanings or no meaning at all on other
  1596. /// platforms.
  1597. ///
  1598. /// Usage of this field is discouraged, but if access is desired then the
  1599. /// fields are located here.
  1600. #[unstable(feature = "io")]
  1601. pub unstable: UnstableFileStat,
  1602. }
  1603. /// This structure represents all of the possible information which can be
  1604. /// returned from a `stat` syscall which is not contained in the `FileStat`
  1605. /// structure. This information is not necessarily platform independent, and may
  1606. /// have different meanings or no meaning at all on some platforms.
  1607. #[unstable(feature = "io")]
  1608. #[derive(Copy, Hash)]
  1609. pub struct UnstableFileStat {
  1610. /// The ID of the device containing the file.
  1611. pub device: u64,
  1612. /// The file serial number.
  1613. pub inode: u64,
  1614. /// The device ID.
  1615. pub rdev: u64,
  1616. /// The number of hard links to this file.
  1617. pub nlink: u64,
  1618. /// The user ID of the file.
  1619. pub uid: u64,
  1620. /// The group ID of the file.
  1621. pub gid: u64,
  1622. /// The optimal block size for I/O.
  1623. pub blksize: u64,
  1624. /// The blocks allocated for this file.
  1625. pub blocks: u64,
  1626. /// User-defined flags for the file.
  1627. pub flags: u64,
  1628. /// The file generation number.
  1629. pub gen: u64,
  1630. }
  1631. bitflags! {
  1632. /// A set of permissions for a file or directory is represented by a set of
  1633. /// flags which are or'd together.
  1634. #[derive(Debug)]
  1635. flags FilePermission: u32 {
  1636. const USER_READ = 0o400,
  1637. const USER_WRITE = 0o200,
  1638. const USER_EXECUTE = 0o100,
  1639. const GROUP_READ = 0o040,
  1640. const GROUP_WRITE = 0o020,
  1641. const GROUP_EXECUTE = 0o010,
  1642. const OTHER_READ = 0o004,
  1643. const OTHER_WRITE = 0o002,
  1644. const OTHER_EXECUTE = 0o001,
  1645. const USER_RWX = USER_READ.bits | USER_WRITE.bits | USER_EXECUTE.bits,
  1646. const GROUP_RWX = GROUP_READ.bits | GROUP_WRITE.bits | GROUP_EXECUTE.bits,
  1647. const OTHER_RWX = OTHER_READ.bits | OTHER_WRITE.bits | OTHER_EXECUTE.bits,
  1648. /// Permissions for user owned files, equivalent to 0644 on unix-like
  1649. /// systems.
  1650. const USER_FILE = USER_READ.bits | USER_WRITE.bits | GROUP_READ.bits | OTHER_READ.bits,
  1651. /// Permissions for user owned directories, equivalent to 0755 on
  1652. /// unix-like systems.
  1653. const USER_DIR = USER_RWX.bits | GROUP_READ.bits | GROUP_EXECUTE.bits |
  1654. OTHER_READ.bits | OTHER_EXECUTE.bits,
  1655. /// Permissions for user owned executables, equivalent to 0755
  1656. /// on unix-like systems.
  1657. const USER_EXEC = USER_DIR.bits,
  1658. /// All possible permissions enabled.
  1659. const ALL_PERMISSIONS = USER_RWX.bits | GROUP_RWX.bits | OTHER_RWX.bits,
  1660. }
  1661. }
  1662. #[stable(feature = "rust1", since = "1.0.0")]
  1663. impl Default for FilePermission {
  1664. #[stable(feature = "rust1", since = "1.0.0")]
  1665. #[inline]
  1666. fn default() -> FilePermission { FilePermission::empty() }
  1667. }
  1668. #[stable(feature = "rust1", since = "1.0.0")]
  1669. impl fmt::Display for FilePermission {
  1670. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  1671. write!(f, "{:04o}", self.bits)
  1672. }
  1673. }
  1674. #[cfg(test)]
  1675. mod tests {
  1676. use self::BadReaderBehavior::*;
  1677. use super::{IoResult, Reader, MemReader, NoProgress, InvalidInput, Writer};
  1678. use prelude::v1::{Ok, Vec, Buffer};
  1679. use usize;
  1680. #[derive(Clone, PartialEq, Debug)]
  1681. enum BadReaderBehavior {
  1682. GoodBehavior(uint),
  1683. BadBehavior(uint)
  1684. }
  1685. struct BadReader<T> {
  1686. r: T,
  1687. behavior: Vec<BadReaderBehavior>,
  1688. }
  1689. impl<T: Reader> BadReader<T> {
  1690. fn new(r: T, behavior: Vec<BadReaderBehavior>) -> BadReader<T> {
  1691. BadReader { behavior: behavior, r: r }
  1692. }
  1693. }
  1694. impl<T: Reader> Reader for BadReader<T> {
  1695. fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
  1696. let BadReader { ref mut behavior, ref mut r } = *self;
  1697. loop {
  1698. if behavior.is_empty() {
  1699. // fall back on good
  1700. return r.read(buf);
  1701. }
  1702. match (&mut **behavior)[0] {
  1703. GoodBehavior(0) => (),
  1704. GoodBehavior(ref mut x) => {
  1705. *x -= 1;
  1706. return r.read(buf);
  1707. }
  1708. BadBehavior(0) => (),
  1709. BadBehavior(ref mut x) => {
  1710. *x -= 1;
  1711. return Ok(0);
  1712. }
  1713. };
  1714. behavior.remove(0);
  1715. }
  1716. }
  1717. }
  1718. #[test]
  1719. fn test_read_at_least() {
  1720. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1721. vec![GoodBehavior(usize::MAX)]);
  1722. let buf = &mut [0; 5];
  1723. assert!(r.read_at_least(1, buf).unwrap() >= 1);
  1724. assert!(r.read_exact(5).unwrap().len() == 5); // read_exact uses read_at_least
  1725. assert!(r.read_at_least(0, buf).is_ok());
  1726. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1727. vec![BadBehavior(50), GoodBehavior(usize::MAX)]);
  1728. assert!(r.read_at_least(1, buf).unwrap() >= 1);
  1729. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1730. vec![BadBehavior(1), GoodBehavior(1),
  1731. BadBehavior(50), GoodBehavior(usize::MAX)]);
  1732. assert!(r.read_at_least(1, buf).unwrap() >= 1);
  1733. assert!(r.read_at_least(1, buf).unwrap() >= 1);
  1734. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1735. vec![BadBehavior(usize::MAX)]);
  1736. assert_eq!(r.read_at_least(1, buf).unwrap_err().kind, NoProgress);
  1737. let mut r = MemReader::new(b"hello, world!".to_vec());
  1738. assert_eq!(r.read_at_least(5, buf).unwrap(), 5);
  1739. assert_eq!(r.read_at_least(6, buf).unwrap_err().kind, InvalidInput);
  1740. }
  1741. #[test]
  1742. fn test_push_at_least() {
  1743. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1744. vec![GoodBehavior(usize::MAX)]);
  1745. let mut buf = Vec::new();
  1746. assert!(r.push_at_least(1, 5, &mut buf).unwrap() >= 1);
  1747. assert!(r.push_at_least(0, 5, &mut buf).is_ok());
  1748. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1749. vec![BadBehavior(50), GoodBehavior(usize::MAX)]);
  1750. assert!(r.push_at_least(1, 5, &mut buf).unwrap() >= 1);
  1751. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1752. vec![BadBehavior(1), GoodBehavior(1),
  1753. BadBehavior(50), GoodBehavior(usize::MAX)]);
  1754. assert!(r.push_at_least(1, 5, &mut buf).unwrap() >= 1);
  1755. assert!(r.push_at_least(1, 5, &mut buf).unwrap() >= 1);
  1756. let mut r = BadReader::new(MemReader::new(b"hello, world!".to_vec()),
  1757. vec![BadBehavior(usize::MAX)]);
  1758. assert_eq!(r.push_at_least(1, 5, &mut buf).unwrap_err().kind, NoProgress);
  1759. let mut r = MemReader::new(b"hello, world!".to_vec());
  1760. assert_eq!(r.push_at_least(5, 1, &mut buf).unwrap_err().kind, InvalidInput);
  1761. }
  1762. #[test]
  1763. fn test_show() {
  1764. use super::*;
  1765. assert_eq!(format!("{}", USER_READ), "0400");
  1766. assert_eq!(format!("{}", USER_FILE), "0644");
  1767. assert_eq!(format!("{}", USER_EXEC), "0755");
  1768. assert_eq!(format!("{}", USER_RWX), "0700");
  1769. assert_eq!(format!("{}", GROUP_RWX), "0070");
  1770. assert_eq!(format!("{}", OTHER_RWX), "0007");
  1771. assert_eq!(format!("{}", ALL_PERMISSIONS), "0777");
  1772. assert_eq!(format!("{}", USER_READ | USER_WRITE | OTHER_WRITE), "0602");
  1773. }
  1774. fn _ensure_buffer_is_object_safe<T: Buffer>(x: &T) -> &Buffer {
  1775. x as &Buffer
  1776. }
  1777. }