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.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // FIXME: cover these topics:
12 // path, reader, writer, stream, raii (close not needed),
13 // stdio, print!, println!, file access, process spawning,
17 /*! I/O, including files, networking, timers, and processes
19 `std::io` provides Rust's basic I/O types,
20 for reading and writing to files, TCP, UDP,
21 and other types of sockets and pipes,
22 manipulating the file system, spawning processes and signal handling.
26 Some examples of obvious things you might want to do
28 * Read lines from stdin
33 for line in io::stdin().lines() {
34 print!("{}", line.unwrap());
38 * Read a complete file
43 let contents = File::open(&Path::new("message.txt")).read_to_end();
46 * Write a line to a file
49 # #![allow(unused_must_use)]
52 let mut file = File::create(&Path::new("message.txt"));
53 file.write(bytes!("hello, file!\n"));
55 # ::std::io::fs::unlink(&Path::new("message.txt"));
58 * Iterate over the lines of a file
61 use std::io::BufferedReader;
64 let path = Path::new("message.txt");
65 let mut file = BufferedReader::new(File::open(&path));
66 for line in file.lines() {
67 print!("{}", line.unwrap());
71 * Pull the lines of a file into a vector of strings
74 use std::io::BufferedReader;
77 let path = Path::new("message.txt");
78 let mut file = BufferedReader::new(File::open(&path));
79 let lines: ~[~str] = file.lines().map(|x| x.unwrap()).collect();
82 * Make a simple TCP client connection and request
85 # #![allow(unused_must_use)]
86 use std::io::net::ip::SocketAddr;
87 use std::io::net::tcp::TcpStream;
89 let addr = from_str::<SocketAddr>("127.0.0.1:8080").unwrap();
90 let mut socket = TcpStream::connect(addr).unwrap();
91 socket.write(bytes!("GET / HTTP/1.0\n\n"));
92 let response = socket.read_to_end();
95 * Make a simple TCP server
100 # #![allow(dead_code)]
101 use std::io::{TcpListener, TcpStream};
102 use std::io::net::ip::{Ipv4Addr, SocketAddr};
103 use std::io::{Acceptor, Listener};
105 let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 80 };
106 let listener = TcpListener::bind(addr);
108 // bind the listener to the specified address
109 let mut acceptor = listener.listen();
111 fn handle_client(mut stream: TcpStream) {
113 # &mut stream; // silence unused mutability/variable warning
115 // accept connections and process them, spawning a new tasks for each one
116 for stream in acceptor.incoming() {
118 Err(e) => { /* connection failed */ }
119 Ok(stream) => spawn(proc() {
120 // connection succeeded
121 handle_client(stream)
126 // close the socket server
134 I/O is an area where nearly every operation can result in unexpected
135 errors. Errors should be painfully visible when they happen, and handling them
136 should be easy to work with. It should be convenient to handle specific I/O
137 errors, and it should also be convenient to not deal with I/O errors.
139 Rust's I/O employs a combination of techniques to reduce boilerplate
140 while still providing feedback about errors. The basic strategy:
142 * All I/O operations return `IoResult<T>` which is equivalent to
143 `Result<T, IoError>`. The `Result` type is defined in the `std::result`
145 * If the `Result` type goes unused, then the compiler will by default emit a
146 warning about the unused result. This is because `Result` has the
147 `#[must_use]` attribute.
148 * Common traits are implemented for `IoResult`, e.g.
149 `impl<R: Reader> Reader for IoResult<R>`, so that error values do not have
150 to be 'unwrapped' before use.
152 These features combine in the API to allow for expressions like
153 `File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"))`
154 without having to worry about whether "diary.txt" exists or whether
155 the write succeeds. As written, if either `new` or `write_line`
156 encounters an error then the result of the entire expression will
159 If you wanted to handle the error though you might write:
162 # #![allow(unused_must_use)]
165 match File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n")) {
166 Ok(()) => (), // succeeded
167 Err(e) => println!("failed to write to my diary: {}", e),
170 # ::std::io::fs::unlink(&Path::new("diary.txt"));
173 So what actually happens if `create` encounters an error?
174 It's important to know that what `new` returns is not a `File`
175 but an `IoResult<File>`. If the file does not open, then `new` will simply
176 return `Err(..)`. Because there is an implementation of `Writer` (the trait
177 required ultimately required for types to implement `write_line`) there is no
178 need to inspect or unwrap the `IoResult<File>` and we simply call `write_line`
179 on it. If `new` returned an `Err(..)` then the followup call to `write_line`
180 will also return an error.
184 Explicit pattern matching on `IoResult`s can get quite verbose, especially
185 when performing many I/O operations. Some examples (like those above) are
186 alleviated with extra methods implemented on `IoResult`, but others have more
187 complex interdependencies among each I/O operation.
189 The `try!` macro from `std::macros` is provided as a method of early-return
190 inside `Result`-returning functions. It expands to an early-return on `Err`
191 and otherwise unwraps the contained `Ok` value.
193 If you wanted to read several `u32`s from a file and return their product:
196 use std::io::{File, IoResult};
198 fn file_product(p: &Path) -> IoResult<u32> {
199 let mut f = File::open(p);
200 let x1 = try!(f.read_le_u32());
201 let x2 = try!(f.read_le_u32());
206 match file_product(&Path::new("numbers.bin")) {
207 Ok(x) => println!("{}", x),
208 Err(e) => println!("Failed to read numbers!")
212 With `try!` in `file_product`, each `read_le_u32` need not be directly
213 concerned with error handling; instead its caller is responsible for
214 responding to errors that may occur while attempting to read the numbers.
218 #![deny(unused_must_use)]
222 use container::Container;
227 use ops::{BitOr, BitAnd, Sub};
229 use option::{Option, Some, None};
232 use result::{Ok, Err, Result};
236 use unstable::finally::try_finally;
237 use slice::{Vector, MutableVector, ImmutableVector};
241 pub use self::stdio::stdin;
242 pub use self::stdio::stdout;
243 pub use self::stdio::stderr;
244 pub use self::stdio::print;
245 pub use self::stdio::println;
247 pub use self::fs::File;
248 pub use self::timer::Timer;
249 pub use self::net::ip::IpAddr;
250 pub use self::net::tcp::TcpListener;
251 pub use self::net::tcp::TcpStream;
252 pub use self::net::udp::UdpStream;
253 pub use self::pipe::PipeStream;
254 pub use self::process::{Process, ProcessConfig};
255 pub use self::tempfile::TempDir;
257 pub use self::mem::{MemReader, BufReader, MemWriter, BufWriter};
258 pub use self::buffered::{BufferedReader, BufferedWriter, BufferedStream,
260 pub use self::comm_adapters::{ChanReader, ChanWriter};
262 // this comes first to get the iotest! macro
280 /// The default buffer size for various I/O operations
281 // libuv recommends 64k buffers to maximize throughput
282 // https://groups.google.com/forum/#!topic/libuv/oQO1HJAIDdA
283 static DEFAULT_BUF_SIZE: uint = 1024 * 64;
285 /// A convenient typedef of the return value of any I/O action.
286 pub type IoResult<T> = Result<T, IoError>;
288 /// The type passed to I/O condition handlers to indicate error
292 /// Is something like this sufficient? It's kind of archaic
293 #[deriving(Eq, Clone)]
295 /// An enumeration which can be matched against for determining the flavor
297 pub kind: IoErrorKind,
298 /// A human-readable description about the error
299 pub desc: &'static str,
300 /// Detailed information about this error, not always available
301 pub detail: Option<~str>
305 /// Convert an `errno` value into an `IoError`.
307 /// If `detail` is `true`, the `detail` field of the `IoError`
308 /// struct is filled with an allocated string describing the error
309 /// in more detail, retrieved from the operating system.
310 pub fn from_errno(errno: uint, detail: bool) -> IoError {
312 fn get_err(errno: i32) -> (IoErrorKind, &'static str) {
314 libc::EOF => (EndOfFile, "end of file"),
315 libc::ERROR_NO_DATA => (BrokenPipe, "the pipe is being closed"),
316 libc::ERROR_FILE_NOT_FOUND => (FileNotFound, "file not found"),
317 libc::ERROR_INVALID_NAME => (InvalidInput, "invalid file name"),
318 libc::WSAECONNREFUSED => (ConnectionRefused, "connection refused"),
319 libc::WSAECONNRESET => (ConnectionReset, "connection reset"),
320 libc::WSAEACCES => (PermissionDenied, "permission denied"),
321 libc::WSAEWOULDBLOCK => {
322 (ResourceUnavailable, "resource temporarily unavailable")
324 libc::WSAENOTCONN => (NotConnected, "not connected"),
325 libc::WSAECONNABORTED => (ConnectionAborted, "connection aborted"),
326 libc::WSAEADDRNOTAVAIL => (ConnectionRefused, "address not available"),
327 libc::WSAEADDRINUSE => (ConnectionRefused, "address in use"),
328 libc::ERROR_BROKEN_PIPE => (EndOfFile, "the pipe has ended"),
330 // libuv maps this error code to EISDIR. we do too. if it is found
331 // to be incorrect, we can add in some more machinery to only
332 // return this message when ERROR_INVALID_FUNCTION after certain
334 libc::ERROR_INVALID_FUNCTION => (InvalidInput,
335 "illegal operation on a directory"),
337 _ => (OtherIoError, "unknown error")
342 fn get_err(errno: i32) -> (IoErrorKind, &'static str) {
343 // FIXME: this should probably be a bit more descriptive...
345 libc::EOF => (EndOfFile, "end of file"),
346 libc::ECONNREFUSED => (ConnectionRefused, "connection refused"),
347 libc::ECONNRESET => (ConnectionReset, "connection reset"),
348 libc::EPERM | libc::EACCES =>
349 (PermissionDenied, "permission denied"),
350 libc::EPIPE => (BrokenPipe, "broken pipe"),
351 libc::ENOTCONN => (NotConnected, "not connected"),
352 libc::ECONNABORTED => (ConnectionAborted, "connection aborted"),
353 libc::EADDRNOTAVAIL => (ConnectionRefused, "address not available"),
354 libc::EADDRINUSE => (ConnectionRefused, "address in use"),
355 libc::ENOENT => (FileNotFound, "no such file or directory"),
356 libc::EISDIR => (InvalidInput, "illegal operation on a directory"),
358 // These two constants can have the same value on some systems, but
359 // different values on others, so we can't use a match clause
360 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
361 (ResourceUnavailable, "resource temporarily unavailable"),
363 _ => (OtherIoError, "unknown error")
367 let (kind, desc) = get_err(errno as i32);
371 detail: if detail {Some(os::error_string(errno))} else {None},
375 /// Retrieve the last error to occur as a (detailed) IoError.
377 /// This uses the OS `errno`, and so there should not be any task
378 /// descheduling or migration (other than that performed by the
379 /// operating system) between the call(s) for which errors are
380 /// being checked and the call of this function.
381 pub fn last_error() -> IoError {
382 IoError::from_errno(os::errno() as uint, true)
386 impl fmt::Show for IoError {
387 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
388 try!(fmt.buf.write_str(self.desc));
390 Some(ref s) => write!(fmt.buf, " ({})", *s),
396 /// A list specifying general categories of I/O error.
397 #[deriving(Eq, Clone, Show)]
398 pub enum IoErrorKind {
399 /// Any I/O error not part of this list.
401 /// The operation could not complete because end of file was reached.
403 /// The file was not found.
405 /// The file permissions disallowed access to this file.
407 /// A network connection failed for some reason not specified in this list.
409 /// The network operation failed because the network connection was cloesd.
411 /// The connection was refused by the remote server.
413 /// The connection was reset by the remote server.
415 /// The connection was aborted (terminated) by the remote server.
417 /// The network operation failed because it was not connected yet.
419 /// The operation failed because a pipe was closed.
421 /// A file already existed with that name.
423 /// No file exists at that location.
425 /// The path did not specify the type of file that this operation required. For example,
426 /// attempting to copy a directory with the `fs::copy()` operation will fail with this error.
427 MismatchedFileTypeForOperation,
428 /// The operation temporarily failed (for example, because a signal was received), and retrying
431 /// No I/O functionality is available for this task.
433 /// A parameter was incorrect in a way that caused an I/O error not part of this list.
435 /// The I/O operation's timeout expired, causing it to be canceled.
439 /// A trait for objects which are byte-oriented streams. Readers are defined by
440 /// one method, `read`. This function will block until data is available,
441 /// filling in the provided buffer with any data read.
443 /// Readers are intended to be composable with one another. Many objects
444 /// throughout the I/O and related libraries take and provide types which
445 /// implement the `Reader` trait.
448 // Only method which need to get implemented for this trait
450 /// Read bytes, up to the length of `buf` and place them in `buf`.
451 /// Returns the number of bytes read. The number of bytes read my
452 /// be less than the number requested, even 0. Returns `Err` on EOF.
456 /// If an error occurs during this I/O operation, then it is returned as
457 /// `Err(IoError)`. Note that end-of-file is considered an error, and can be
458 /// inspected for in the error's `kind` field. Also note that reading 0
459 /// bytes is not considered an error in all circumstances
460 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
462 // Convenient helper methods based on the above methods
464 /// Reads a single byte. Returns `Err` on EOF.
465 fn read_byte(&mut self) -> IoResult<u8> {
468 match self.read(buf) {
470 Ok(1) => return Ok(buf[0]),
471 Ok(_) => unreachable!(),
472 Err(e) => return Err(e)
477 /// Fills the provided slice with bytes from this reader
479 /// This will continue to call `read` until the slice has been completely
480 /// filled with bytes.
484 /// If an error occurs at any point, that error is returned, and no further
486 fn fill(&mut self, buf: &mut [u8]) -> IoResult<()> {
488 while read < buf.len() {
489 read += try!(self.read(buf.mut_slice_from(read)));
494 /// Reads exactly `len` bytes and appends them to a vector.
496 /// May push fewer than the requested number of bytes on error
497 /// or EOF. If `Ok(())` is returned, then all of the requested bytes were
498 /// pushed on to the vector, otherwise the amount `len` bytes couldn't be
499 /// read (an error was encountered), and the error is returned.
500 fn push_exact(&mut self, buf: &mut Vec<u8>, len: uint) -> IoResult<()> {
502 buf: &'a mut Vec<u8>,
506 let start_len = buf.len();
507 let mut s = State { buf: buf, total_read: 0 };
509 s.buf.reserve_additional(len);
510 unsafe { s.buf.set_len(start_len + len); }
515 while s.total_read < len {
516 let len = s.buf.len();
517 let slice = s.buf.mut_slice(start_len + s.total_read, len);
518 match self.read(slice) {
520 s.total_read += nread;
522 Err(e) => return Err(e)
527 |s| unsafe { s.buf.set_len(start_len + s.total_read) })
530 /// Reads exactly `len` bytes and gives you back a new vector of length
535 /// Fails with the same conditions as `read`. Additionally returns error
536 /// on EOF. Note that if an error is returned, then some number of bytes may
537 /// have already been consumed from the underlying reader, and they are lost
538 /// (not returned as part of the error). If this is unacceptable, then it is
539 /// recommended to use the `push_exact` or `read` methods.
540 fn read_exact(&mut self, len: uint) -> IoResult<Vec<u8>> {
541 let mut buf = Vec::with_capacity(len);
542 match self.push_exact(&mut buf, len) {
548 /// Reads all remaining bytes from the stream.
552 /// Returns any non-EOF error immediately. Previously read bytes are
553 /// discarded when an error is returned.
555 /// When EOF is encountered, all bytes read up to that point are returned.
556 fn read_to_end(&mut self) -> IoResult<Vec<u8>> {
557 let mut buf = Vec::with_capacity(DEFAULT_BUF_SIZE);
559 match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) {
561 Err(ref e) if e.kind == EndOfFile => break,
562 Err(e) => return Err(e)
568 /// Reads all of the remaining bytes of this stream, interpreting them as a
569 /// UTF-8 encoded stream. The corresponding string is returned.
573 /// This function returns all of the same errors as `read_to_end` with an
574 /// additional error if the reader's contents are not a valid sequence of
576 fn read_to_str(&mut self) -> IoResult<~str> {
577 self.read_to_end().and_then(|s| {
578 match str::from_utf8(s.as_slice()) {
579 Some(s) => Ok(s.to_owned()),
580 None => Err(standard_error(InvalidInput)),
585 /// Create an iterator that reads a single byte on
586 /// each iteration, until EOF.
590 /// Any error other than `EndOfFile` that is produced by the underlying Reader
591 /// is returned by the iterator and should be handled by the caller.
592 fn bytes<'r>(&'r mut self) -> extensions::Bytes<'r, Self> {
593 extensions::Bytes::new(self)
596 // Byte conversion helpers
598 /// Reads `n` little-endian unsigned integer bytes.
600 /// `n` must be between 1 and 8, inclusive.
601 fn read_le_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
602 assert!(nbytes > 0 && nbytes <= 8);
608 val += (try!(self.read_u8()) as u64) << pos;
615 /// Reads `n` little-endian signed integer bytes.
617 /// `n` must be between 1 and 8, inclusive.
618 fn read_le_int_n(&mut self, nbytes: uint) -> IoResult<i64> {
619 self.read_le_uint_n(nbytes).map(|i| extend_sign(i, nbytes))
622 /// Reads `n` big-endian unsigned integer bytes.
624 /// `n` must be between 1 and 8, inclusive.
625 fn read_be_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
626 assert!(nbytes > 0 && nbytes <= 8);
632 val += (try!(self.read_u8()) as u64) << i * 8;
637 /// Reads `n` big-endian signed integer bytes.
639 /// `n` must be between 1 and 8, inclusive.
640 fn read_be_int_n(&mut self, nbytes: uint) -> IoResult<i64> {
641 self.read_be_uint_n(nbytes).map(|i| extend_sign(i, nbytes))
644 /// Reads a little-endian unsigned integer.
646 /// The number of bytes returned is system-dependent.
647 fn read_le_uint(&mut self) -> IoResult<uint> {
648 self.read_le_uint_n(uint::BYTES).map(|i| i as uint)
651 /// Reads a little-endian integer.
653 /// The number of bytes returned is system-dependent.
654 fn read_le_int(&mut self) -> IoResult<int> {
655 self.read_le_int_n(int::BYTES).map(|i| i as int)
658 /// Reads a big-endian unsigned integer.
660 /// The number of bytes returned is system-dependent.
661 fn read_be_uint(&mut self) -> IoResult<uint> {
662 self.read_be_uint_n(uint::BYTES).map(|i| i as uint)
665 /// Reads a big-endian integer.
667 /// The number of bytes returned is system-dependent.
668 fn read_be_int(&mut self) -> IoResult<int> {
669 self.read_be_int_n(int::BYTES).map(|i| i as int)
672 /// Reads a big-endian `u64`.
674 /// `u64`s are 8 bytes long.
675 fn read_be_u64(&mut self) -> IoResult<u64> {
676 self.read_be_uint_n(8)
679 /// Reads a big-endian `u32`.
681 /// `u32`s are 4 bytes long.
682 fn read_be_u32(&mut self) -> IoResult<u32> {
683 self.read_be_uint_n(4).map(|i| i as u32)
686 /// Reads a big-endian `u16`.
688 /// `u16`s are 2 bytes long.
689 fn read_be_u16(&mut self) -> IoResult<u16> {
690 self.read_be_uint_n(2).map(|i| i as u16)
693 /// Reads a big-endian `i64`.
695 /// `i64`s are 8 bytes long.
696 fn read_be_i64(&mut self) -> IoResult<i64> {
697 self.read_be_int_n(8)
700 /// Reads a big-endian `i32`.
702 /// `i32`s are 4 bytes long.
703 fn read_be_i32(&mut self) -> IoResult<i32> {
704 self.read_be_int_n(4).map(|i| i as i32)
707 /// Reads a big-endian `i16`.
709 /// `i16`s are 2 bytes long.
710 fn read_be_i16(&mut self) -> IoResult<i16> {
711 self.read_be_int_n(2).map(|i| i as i16)
714 /// Reads a big-endian `f64`.
716 /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
717 fn read_be_f64(&mut self) -> IoResult<f64> {
718 self.read_be_u64().map(|i| unsafe {
719 cast::transmute::<u64, f64>(i)
723 /// Reads a big-endian `f32`.
725 /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
726 fn read_be_f32(&mut self) -> IoResult<f32> {
727 self.read_be_u32().map(|i| unsafe {
728 cast::transmute::<u32, f32>(i)
732 /// Reads a little-endian `u64`.
734 /// `u64`s are 8 bytes long.
735 fn read_le_u64(&mut self) -> IoResult<u64> {
736 self.read_le_uint_n(8)
739 /// Reads a little-endian `u32`.
741 /// `u32`s are 4 bytes long.
742 fn read_le_u32(&mut self) -> IoResult<u32> {
743 self.read_le_uint_n(4).map(|i| i as u32)
746 /// Reads a little-endian `u16`.
748 /// `u16`s are 2 bytes long.
749 fn read_le_u16(&mut self) -> IoResult<u16> {
750 self.read_le_uint_n(2).map(|i| i as u16)
753 /// Reads a little-endian `i64`.
755 /// `i64`s are 8 bytes long.
756 fn read_le_i64(&mut self) -> IoResult<i64> {
757 self.read_le_int_n(8)
760 /// Reads a little-endian `i32`.
762 /// `i32`s are 4 bytes long.
763 fn read_le_i32(&mut self) -> IoResult<i32> {
764 self.read_le_int_n(4).map(|i| i as i32)
767 /// Reads a little-endian `i16`.
769 /// `i16`s are 2 bytes long.
770 fn read_le_i16(&mut self) -> IoResult<i16> {
771 self.read_le_int_n(2).map(|i| i as i16)
774 /// Reads a little-endian `f64`.
776 /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
777 fn read_le_f64(&mut self) -> IoResult<f64> {
778 self.read_le_u64().map(|i| unsafe {
779 cast::transmute::<u64, f64>(i)
783 /// Reads a little-endian `f32`.
785 /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
786 fn read_le_f32(&mut self) -> IoResult<f32> {
787 self.read_le_u32().map(|i| unsafe {
788 cast::transmute::<u32, f32>(i)
794 /// `u8`s are 1 byte.
795 fn read_u8(&mut self) -> IoResult<u8> {
801 /// `i8`s are 1 byte.
802 fn read_i8(&mut self) -> IoResult<i8> {
803 self.read_byte().map(|i| i as i8)
806 /// Creates a wrapper around a mutable reference to the reader.
808 /// This is useful to allow applying adaptors while still
809 /// retaining ownership of the original value.
810 fn by_ref<'a>(&'a mut self) -> RefReader<'a, Self> {
811 RefReader { inner: self }
815 impl Reader for Box<Reader> {
816 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
819 impl<'a> Reader for &'a mut Reader {
820 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
823 /// A `RefReader` is a struct implementing `Reader` which contains a reference
824 /// to another reader. This is often useful when composing streams.
830 /// # fn process_input<R: Reader>(r: R) {}
833 /// use std::io::util::LimitReader;
835 /// let mut stream = io::stdin();
837 /// // Only allow the function to process at most one kilobyte of input
839 /// let stream = LimitReader::new(stream.by_ref(), 1024);
840 /// process_input(stream);
843 /// // 'stream' is still available for use here
847 pub struct RefReader<'a, R> {
848 /// The underlying reader which this is referencing
852 impl<'a, R: Reader> Reader for RefReader<'a, R> {
853 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.inner.read(buf) }
856 fn extend_sign(val: u64, nbytes: uint) -> i64 {
857 let shift = (8 - nbytes) * 8;
858 (val << shift) as i64 >> shift
861 /// A trait for objects which are byte-oriented streams. Writers are defined by
862 /// one method, `write`. This function will block until the provided buffer of
863 /// bytes has been entirely written, and it will return any failurs which occur.
865 /// Another commonly overriden method is the `flush` method for writers such as
866 /// buffered writers.
868 /// Writers are intended to be composable with one another. Many objects
869 /// throughout the I/O and related libraries take and provide types which
870 /// implement the `Writer` trait.
872 /// Write the entirety of a given buffer
876 /// If an error happens during the I/O operation, the error is returned as
877 /// `Err`. Note that it is considered an error if the entire buffer could
878 /// not be written, and if an error is returned then it is unknown how much
879 /// data (if any) was actually written.
880 fn write(&mut self, buf: &[u8]) -> IoResult<()>;
882 /// Flush this output stream, ensuring that all intermediately buffered
883 /// contents reach their destination.
885 /// This is by default a no-op and implementers of the `Writer` trait should
886 /// decide whether their stream needs to be buffered or not.
887 fn flush(&mut self) -> IoResult<()> { Ok(()) }
889 /// Write a rust string into this sink.
891 /// The bytes written will be the UTF-8 encoded version of the input string.
892 /// If other encodings are desired, it is recommended to compose this stream
893 /// with another performing the conversion, or to use `write` with a
894 /// converted byte-array instead.
895 fn write_str(&mut self, s: &str) -> IoResult<()> {
896 self.write(s.as_bytes())
899 /// Writes a string into this sink, and then writes a literal newline (`\n`)
900 /// byte afterwards. Note that the writing of the newline is *not* atomic in
901 /// the sense that the call to `write` is invoked twice (once with the
902 /// string and once with a newline character).
904 /// If other encodings or line ending flavors are desired, it is recommended
905 /// that the `write` method is used specifically instead.
906 fn write_line(&mut self, s: &str) -> IoResult<()> {
907 self.write_str(s).and_then(|()| self.write(['\n' as u8]))
910 /// Write a single char, encoded as UTF-8.
911 fn write_char(&mut self, c: char) -> IoResult<()> {
912 let mut buf = [0u8, ..4];
913 let n = c.encode_utf8(buf.as_mut_slice());
914 self.write(buf.slice_to(n))
917 /// Write the result of passing n through `int::to_str_bytes`.
918 fn write_int(&mut self, n: int) -> IoResult<()> {
919 write!(self, "{:d}", n)
922 /// Write the result of passing n through `uint::to_str_bytes`.
923 fn write_uint(&mut self, n: uint) -> IoResult<()> {
924 write!(self, "{:u}", n)
927 /// Write a little-endian uint (number of bytes depends on system).
928 fn write_le_uint(&mut self, n: uint) -> IoResult<()> {
929 extensions::u64_to_le_bytes(n as u64, uint::BYTES, |v| self.write(v))
932 /// Write a little-endian int (number of bytes depends on system).
933 fn write_le_int(&mut self, n: int) -> IoResult<()> {
934 extensions::u64_to_le_bytes(n as u64, int::BYTES, |v| self.write(v))
937 /// Write a big-endian uint (number of bytes depends on system).
938 fn write_be_uint(&mut self, n: uint) -> IoResult<()> {
939 extensions::u64_to_be_bytes(n as u64, uint::BYTES, |v| self.write(v))
942 /// Write a big-endian int (number of bytes depends on system).
943 fn write_be_int(&mut self, n: int) -> IoResult<()> {
944 extensions::u64_to_be_bytes(n as u64, int::BYTES, |v| self.write(v))
947 /// Write a big-endian u64 (8 bytes).
948 fn write_be_u64(&mut self, n: u64) -> IoResult<()> {
949 extensions::u64_to_be_bytes(n, 8u, |v| self.write(v))
952 /// Write a big-endian u32 (4 bytes).
953 fn write_be_u32(&mut self, n: u32) -> IoResult<()> {
954 extensions::u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
957 /// Write a big-endian u16 (2 bytes).
958 fn write_be_u16(&mut self, n: u16) -> IoResult<()> {
959 extensions::u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
962 /// Write a big-endian i64 (8 bytes).
963 fn write_be_i64(&mut self, n: i64) -> IoResult<()> {
964 extensions::u64_to_be_bytes(n as u64, 8u, |v| self.write(v))
967 /// Write a big-endian i32 (4 bytes).
968 fn write_be_i32(&mut self, n: i32) -> IoResult<()> {
969 extensions::u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
972 /// Write a big-endian i16 (2 bytes).
973 fn write_be_i16(&mut self, n: i16) -> IoResult<()> {
974 extensions::u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
977 /// Write a big-endian IEEE754 double-precision floating-point (8 bytes).
978 fn write_be_f64(&mut self, f: f64) -> IoResult<()> {
980 self.write_be_u64(cast::transmute(f))
984 /// Write a big-endian IEEE754 single-precision floating-point (4 bytes).
985 fn write_be_f32(&mut self, f: f32) -> IoResult<()> {
987 self.write_be_u32(cast::transmute(f))
991 /// Write a little-endian u64 (8 bytes).
992 fn write_le_u64(&mut self, n: u64) -> IoResult<()> {
993 extensions::u64_to_le_bytes(n, 8u, |v| self.write(v))
996 /// Write a little-endian u32 (4 bytes).
997 fn write_le_u32(&mut self, n: u32) -> IoResult<()> {
998 extensions::u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
1001 /// Write a little-endian u16 (2 bytes).
1002 fn write_le_u16(&mut self, n: u16) -> IoResult<()> {
1003 extensions::u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
1006 /// Write a little-endian i64 (8 bytes).
1007 fn write_le_i64(&mut self, n: i64) -> IoResult<()> {
1008 extensions::u64_to_le_bytes(n as u64, 8u, |v| self.write(v))
1011 /// Write a little-endian i32 (4 bytes).
1012 fn write_le_i32(&mut self, n: i32) -> IoResult<()> {
1013 extensions::u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
1016 /// Write a little-endian i16 (2 bytes).
1017 fn write_le_i16(&mut self, n: i16) -> IoResult<()> {
1018 extensions::u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
1021 /// Write a little-endian IEEE754 double-precision floating-point
1023 fn write_le_f64(&mut self, f: f64) -> IoResult<()> {
1025 self.write_le_u64(cast::transmute(f))
1029 /// Write a little-endian IEEE754 single-precision floating-point
1031 fn write_le_f32(&mut self, f: f32) -> IoResult<()> {
1033 self.write_le_u32(cast::transmute(f))
1037 /// Write a u8 (1 byte).
1038 fn write_u8(&mut self, n: u8) -> IoResult<()> {
1042 /// Write an i8 (1 byte).
1043 fn write_i8(&mut self, n: i8) -> IoResult<()> {
1044 self.write([n as u8])
1047 /// Creates a wrapper around a mutable reference to the writer.
1049 /// This is useful to allow applying wrappers while still
1050 /// retaining ownership of the original value.
1051 fn by_ref<'a>(&'a mut self) -> RefWriter<'a, Self> {
1052 RefWriter { inner: self }
1056 impl Writer for Box<Writer> {
1057 fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.write(buf) }
1058 fn flush(&mut self) -> IoResult<()> { self.flush() }
1061 impl<'a> Writer for &'a mut Writer {
1062 fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.write(buf) }
1063 fn flush(&mut self) -> IoResult<()> { self.flush() }
1066 /// A `RefWriter` is a struct implementing `Writer` which contains a reference
1067 /// to another writer. This is often useful when composing streams.
1073 /// # fn process_input<R: Reader>(r: R) {}
1075 /// use std::io::util::TeeReader;
1076 /// use std::io::{stdin, MemWriter};
1078 /// let mut output = MemWriter::new();
1081 /// // Don't give ownership of 'output' to the 'tee'. Instead we keep a
1082 /// // handle to it in the outer scope
1083 /// let mut tee = TeeReader::new(stdin(), output.by_ref());
1084 /// process_input(tee);
1087 /// println!("input processed: {}", output.unwrap());
1090 pub struct RefWriter<'a, W> {
1091 /// The underlying writer which this is referencing
1095 impl<'a, W: Writer> Writer for RefWriter<'a, W> {
1096 fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.inner.write(buf) }
1097 fn flush(&mut self) -> IoResult<()> { self.inner.flush() }
1101 /// A Stream is a readable and a writable object. Data written is typically
1102 /// received by the object which reads receive data from.
1103 pub trait Stream: Reader + Writer { }
1105 impl<T: Reader + Writer> Stream for T {}
1107 /// An iterator that reads a line on each iteration,
1108 /// until `.read_line()` encounters `EndOfFile`.
1110 /// # Notes about the Iteration Protocol
1112 /// The `Lines` may yield `None` and thus terminate
1113 /// an iteration, but continue to yield elements if iteration
1114 /// is attempted again.
1118 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1119 /// is returned by the iterator and should be handled by the caller.
1120 pub struct Lines<'r, T> {
1124 impl<'r, T: Buffer> Iterator<IoResult<~str>> for Lines<'r, T> {
1125 fn next(&mut self) -> Option<IoResult<~str>> {
1126 match self.buffer.read_line() {
1127 Ok(x) => Some(Ok(x)),
1128 Err(IoError { kind: EndOfFile, ..}) => None,
1129 Err(y) => Some(Err(y))
1134 /// An iterator that reads a utf8-encoded character on each iteration,
1135 /// until `.read_char()` encounters `EndOfFile`.
1137 /// # Notes about the Iteration Protocol
1139 /// The `Chars` may yield `None` and thus terminate
1140 /// an iteration, but continue to yield elements if iteration
1141 /// is attempted again.
1145 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1146 /// is returned by the iterator and should be handled by the caller.
1147 pub struct Chars<'r, T> {
1151 impl<'r, T: Buffer> Iterator<IoResult<char>> for Chars<'r, T> {
1152 fn next(&mut self) -> Option<IoResult<char>> {
1153 match self.buffer.read_char() {
1154 Ok(x) => Some(Ok(x)),
1155 Err(IoError { kind: EndOfFile, ..}) => None,
1156 Err(y) => Some(Err(y))
1161 /// A Buffer is a type of reader which has some form of internal buffering to
1162 /// allow certain kinds of reading operations to be more optimized than others.
1163 /// This type extends the `Reader` trait with a few methods that are not
1164 /// possible to reasonably implement with purely a read interface.
1165 pub trait Buffer: Reader {
1166 /// Fills the internal buffer of this object, returning the buffer contents.
1167 /// Note that none of the contents will be "read" in the sense that later
1168 /// calling `read` may return the same contents.
1170 /// The `consume` function must be called with the number of bytes that are
1171 /// consumed from this buffer returned to ensure that the bytes are never
1176 /// This function will return an I/O error if the underlying reader was
1177 /// read, but returned an error. Note that it is not an error to return a
1178 /// 0-length buffer.
1179 fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]>;
1181 /// Tells this buffer that `amt` bytes have been consumed from the buffer,
1182 /// so they should no longer be returned in calls to `fill` or `read`.
1183 fn consume(&mut self, amt: uint);
1185 /// Reads the next line of input, interpreted as a sequence of UTF-8
1186 /// encoded unicode codepoints. If a newline is encountered, then the
1187 /// newline is contained in the returned string.
1194 /// let mut reader = io::stdin();
1195 /// let input = reader.read_line().ok().unwrap_or("nothing".to_owned());
1200 /// This function has the same error semantics as `read_until`:
1202 /// * All non-EOF errors will be returned immediately
1203 /// * If an error is returned previously consumed bytes are lost
1204 /// * EOF is only returned if no bytes have been read
1205 /// * Reach EOF may mean that the delimiter is not present in the return
1208 /// Additionally, this function can fail if the line of input read is not a
1209 /// valid UTF-8 sequence of bytes.
1210 fn read_line(&mut self) -> IoResult<~str> {
1211 self.read_until('\n' as u8).and_then(|line|
1212 match str::from_utf8(line.as_slice()) {
1213 Some(s) => Ok(s.to_owned()),
1214 None => Err(standard_error(InvalidInput)),
1219 /// Create an iterator that reads a line on each iteration until EOF.
1223 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1224 /// is returned by the iterator and should be handled by the caller.
1225 fn lines<'r>(&'r mut self) -> Lines<'r, Self> {
1226 Lines { buffer: self }
1229 /// Reads a sequence of bytes leading up to a specified delimiter. Once the
1230 /// specified byte is encountered, reading ceases and the bytes up to and
1231 /// including the delimiter are returned.
1235 /// If any I/O error is encountered other than EOF, the error is immediately
1236 /// returned. Note that this may discard bytes which have already been read,
1237 /// and those bytes will *not* be returned. It is recommended to use other
1238 /// methods if this case is worrying.
1240 /// If EOF is encountered, then this function will return EOF if 0 bytes
1241 /// have been read, otherwise the pending byte buffer is returned. This
1242 /// is the reason that the byte buffer returned may not always contain the
1244 fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> {
1245 let mut res = Vec::new();
1250 let available = match self.fill_buf() {
1252 Err(ref e) if res.len() > 0 && e.kind == EndOfFile => {
1256 Err(e) => return Err(e)
1258 match available.iter().position(|&b| b == byte) {
1260 res.push_all(available.slice_to(i + 1));
1265 res.push_all(available);
1266 used = available.len();
1276 /// Reads the next utf8-encoded character from the underlying stream.
1280 /// If an I/O error occurs, or EOF, then this function will return `Err`.
1281 /// This function will also return error if the stream does not contain a
1282 /// valid utf-8 encoded codepoint as the next few bytes in the stream.
1283 fn read_char(&mut self) -> IoResult<char> {
1284 let first_byte = try!(self.read_byte());
1285 let width = str::utf8_char_width(first_byte);
1286 if width == 1 { return Ok(first_byte as char) }
1287 if width == 0 { return Err(standard_error(InvalidInput)) } // not utf8
1288 let mut buf = [first_byte, 0, 0, 0];
1291 while start < width {
1292 match try!(self.read(buf.mut_slice(start, width))) {
1293 n if n == width - start => break,
1294 n if n < width - start => { start += n; }
1295 _ => return Err(standard_error(InvalidInput)),
1299 match str::from_utf8(buf.slice_to(width)) {
1300 Some(s) => Ok(s.char_at(0)),
1301 None => Err(standard_error(InvalidInput))
1305 /// Create an iterator that reads a utf8-encoded character on each iteration
1310 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1311 /// is returned by the iterator and should be handled by the caller.
1312 fn chars<'r>(&'r mut self) -> Chars<'r, Self> {
1313 Chars { buffer: self }
1317 /// When seeking, the resulting cursor is offset from a base by the offset given
1318 /// to the `seek` function. The base used is specified by this enumeration.
1319 pub enum SeekStyle {
1320 /// Seek from the beginning of the stream
1322 /// Seek from the end of the stream
1324 /// Seek from the current position
1328 /// An object implementing `Seek` internally has some form of cursor which can
1329 /// be moved within a stream of bytes. The stream typically has a fixed size,
1330 /// allowing seeking relative to either end.
1332 /// Return position of file cursor in the stream
1333 fn tell(&self) -> IoResult<u64>;
1335 /// Seek to an offset in a stream
1337 /// A successful seek clears the EOF indicator. Seeking beyond EOF is
1338 /// allowed, but seeking before position 0 is not allowed.
1342 /// * Seeking to a negative offset is considered an error
1343 /// * Seeking past the end of the stream does not modify the underlying
1344 /// stream, but the next write may cause the previous data to be filled in
1345 /// with a bit pattern.
1346 fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>;
1349 /// A listener is a value that can consume itself to start listening for
1352 /// Doing so produces some sort of Acceptor.
1353 pub trait Listener<T, A: Acceptor<T>> {
1354 /// Spin up the listener and start queuing incoming connections
1358 /// Returns `Err` if this listener could not be bound to listen for
1359 /// connections. In all cases, this listener is consumed.
1360 fn listen(self) -> IoResult<A>;
1363 /// An acceptor is a value that presents incoming connections
1364 pub trait Acceptor<T> {
1365 /// Wait for and accept an incoming connection
1369 /// Returns `Err` if an I/O error is encountered.
1370 fn accept(&mut self) -> IoResult<T>;
1372 /// Create an iterator over incoming connection attempts.
1374 /// Note that I/O errors will be yielded by the iterator itself.
1375 fn incoming<'r>(&'r mut self) -> IncomingConnections<'r, Self> {
1376 IncomingConnections { inc: self }
1380 /// An infinite iterator over incoming connection attempts.
1381 /// Calling `next` will block the task until a connection is attempted.
1383 /// Since connection attempts can continue forever, this iterator always returns
1384 /// `Some`. The `Some` contains the `IoResult` representing whether the
1385 /// connection attempt was successful. A successful connection will be wrapped
1386 /// in `Ok`. A failed connection is represented as an `Err`.
1387 pub struct IncomingConnections<'a, A> {
1391 impl<'a, T, A: Acceptor<T>> Iterator<IoResult<T>> for IncomingConnections<'a, A> {
1392 fn next(&mut self) -> Option<IoResult<T>> {
1393 Some(self.inc.accept())
1397 /// Creates a standard error for a commonly used flavor of error. The `detail`
1398 /// field of the returned error will always be `None`.
1405 /// let eof = io::standard_error(io::EndOfFile);
1406 /// let einval = io::standard_error(io::InvalidInput);
1408 pub fn standard_error(kind: IoErrorKind) -> IoError {
1409 let desc = match kind {
1410 EndOfFile => "end of file",
1411 IoUnavailable => "I/O is unavailable",
1412 InvalidInput => "invalid input",
1413 OtherIoError => "unknown I/O error",
1414 FileNotFound => "file not found",
1415 PermissionDenied => "permission denied",
1416 ConnectionFailed => "connection failed",
1417 Closed => "stream is closed",
1418 ConnectionRefused => "connection refused",
1419 ConnectionReset => "connection reset",
1420 ConnectionAborted => "connection aborted",
1421 NotConnected => "not connected",
1422 BrokenPipe => "broken pipe",
1423 PathAlreadyExists => "file exists",
1424 PathDoesntExist => "no such file",
1425 MismatchedFileTypeForOperation => "mismatched file type",
1426 ResourceUnavailable => "resource unavailable",
1427 TimedOut => "operation timed out"
1436 /// A mode specifies how a file should be opened or created. These modes are
1437 /// passed to `File::open_mode` and are used to control where the file is
1438 /// positioned when it is initially opened.
1440 /// Opens a file positioned at the beginning.
1442 /// Opens a file positioned at EOF.
1444 /// Opens a file, truncating it if it already exists.
1448 /// Access permissions with which the file should be opened. `File`s
1449 /// opened with `Read` will return an error if written to.
1450 pub enum FileAccess {
1451 /// Read-only access, requests to write will result in an error
1453 /// Write-only access, requests to read will result in an error
1455 /// Read-write access, no requests are denied by default
1459 /// Different kinds of files which can be identified by a call to stat
1460 #[deriving(Eq, Show, Hash)]
1462 /// This is a normal file, corresponding to `S_IFREG`
1465 /// This file is a directory, corresponding to `S_IFDIR`
1468 /// This file is a named pipe, corresponding to `S_IFIFO`
1471 /// This file is a block device, corresponding to `S_IFBLK`
1474 /// This file is a symbolic link to another file, corresponding to `S_IFLNK`
1477 /// The type of this file is not recognized as one of the other categories
1481 /// A structure used to describe metadata information about a file. This
1482 /// structure is created through the `stat` method on a `Path`.
1489 /// let info = match Path::new("foo.txt").stat() {
1490 /// Ok(stat) => stat,
1491 /// Err(e) => fail!("couldn't read foo.txt: {}", e),
1494 /// println!("path: {}", info.path.display());
1495 /// println!("byte size: {}", info.size);
1499 pub struct FileStat {
1500 /// The path that this stat structure is describing
1502 /// The size of the file, in bytes
1504 /// The kind of file this path points to (directory, file, pipe, etc.)
1506 /// The file permissions currently on the file
1507 pub perm: FilePermission,
1509 // FIXME(#10301): These time fields are pretty useless without an actual
1510 // time representation, what are the milliseconds relative
1513 /// The time that the file was created at, in platform-dependent
1516 /// The time that this file was last modified, in platform-dependent
1519 /// The time that this file was last accessed, in platform-dependent
1523 /// Information returned by stat() which is not guaranteed to be
1524 /// platform-independent. This information may be useful on some platforms,
1525 /// but it may have different meanings or no meaning at all on other
1528 /// Usage of this field is discouraged, but if access is desired then the
1529 /// fields are located here.
1531 pub unstable: UnstableFileStat,
1534 /// This structure represents all of the possible information which can be
1535 /// returned from a `stat` syscall which is not contained in the `FileStat`
1536 /// structure. This information is not necessarily platform independent, and may
1537 /// have different meanings or no meaning at all on some platforms.
1540 pub struct UnstableFileStat {
1541 /// The ID of the device containing the file.
1543 /// The file serial number.
1547 /// The number of hard links to this file.
1549 /// The user ID of the file.
1551 /// The group ID of the file.
1553 /// The optimal block size for I/O.
1555 /// The blocks allocated for this file.
1557 /// User-defined flags for the file.
1559 /// The file generation number.
1564 #[doc="A set of permissions for a file or directory is represented
1565 by a set of flags which are or'd together."]
1568 flags FilePermission: u32 {
1569 static UserRead = 0o400,
1570 static UserWrite = 0o200,
1571 static UserExecute = 0o100,
1572 static GroupRead = 0o040,
1573 static GroupWrite = 0o020,
1574 static GroupExecute = 0o010,
1575 static OtherRead = 0o004,
1576 static OtherWrite = 0o002,
1577 static OtherExecute = 0o001,
1579 static UserRWX = UserRead.bits | UserWrite.bits | UserExecute.bits,
1580 static GroupRWX = GroupRead.bits | GroupWrite.bits | GroupExecute.bits,
1581 static OtherRWX = OtherRead.bits | OtherWrite.bits | OtherExecute.bits,
1583 #[doc="Permissions for user owned files, equivalent to 0644 on
1584 unix-like systems."]
1585 static UserFile = UserRead.bits | UserWrite.bits | GroupRead.bits | OtherRead.bits,
1587 #[doc="Permissions for user owned directories, equivalent to 0755 on
1588 unix-like systems."]
1589 static UserDir = UserRWX.bits | GroupRead.bits | GroupExecute.bits |
1590 OtherRead.bits | OtherExecute.bits,
1592 #[doc="Permissions for user owned executables, equivalent to 0755
1593 on unix-like systems."]
1594 static UserExec = UserDir.bits,
1596 #[doc="All possible permissions enabled."]
1597 static AllPermissions = UserRWX.bits | GroupRWX.bits | OtherRWX.bits