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(unused_must_use, dead_code)];
101 use std::io::net::tcp::TcpListener;
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 // accept connections and process them
112 # fn handle_client<T>(_: T) {}
113 for stream in acceptor.incoming() {
115 handle_client(stream);
119 // close the socket server
127 I/O is an area where nearly every operation can result in unexpected
128 errors. Errors should be painfully visible when they happen, and handling them
129 should be easy to work with. It should be convenient to handle specific I/O
130 errors, and it should also be convenient to not deal with I/O errors.
132 Rust's I/O employs a combination of techniques to reduce boilerplate
133 while still providing feedback about errors. The basic strategy:
135 * All I/O operations return `IoResult<T>` which is equivalent to
136 `Result<T, IoError>`. The `Result` type is defined in the `std::result`
138 * If the `Result` type goes unused, then the compiler will by default emit a
139 warning about the unused result. This is because `Result` has the
140 `#[must_use]` attribute.
141 * Common traits are implemented for `IoResult`, e.g.
142 `impl<R: Reader> Reader for IoResult<R>`, so that error values do not have
143 to be 'unwrapped' before use.
145 These features combine in the API to allow for expressions like
146 `File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"))`
147 without having to worry about whether "diary.txt" exists or whether
148 the write succeeds. As written, if either `new` or `write_line`
149 encounters an error then the result of the entire expression will
152 If you wanted to handle the error though you might write:
155 # #[allow(unused_must_use)];
158 match File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n")) {
159 Ok(()) => (), // succeeded
160 Err(e) => println!("failed to write to my diary: {}", e),
163 # ::std::io::fs::unlink(&Path::new("diary.txt"));
166 So what actually happens if `create` encounters an error?
167 It's important to know that what `new` returns is not a `File`
168 but an `IoResult<File>`. If the file does not open, then `new` will simply
169 return `Err(..)`. Because there is an implementation of `Writer` (the trait
170 required ultimately required for types to implement `write_line`) there is no
171 need to inspect or unwrap the `IoResult<File>` and we simply call `write_line`
172 on it. If `new` returned an `Err(..)` then the followup call to `write_line`
173 will also return an error.
177 Explicit pattern matching on `IoResult`s can get quite verbose, especially
178 when performing many I/O operations. Some examples (like those above) are
179 alleviated with extra methods implemented on `IoResult`, but others have more
180 complex interdependencies among each I/O operation.
182 The `try!` macro from `std::macros` is provided as a method of early-return
183 inside `Result`-returning functions. It expands to an early-return on `Err`
184 and otherwise unwraps the contained `Ok` value.
186 If you wanted to read several `u32`s from a file and return their product:
189 use std::io::{File, IoResult};
191 fn file_product(p: &Path) -> IoResult<u32> {
192 let mut f = File::open(p);
193 let x1 = try!(f.read_le_u32());
194 let x2 = try!(f.read_le_u32());
199 match file_product(&Path::new("numbers.bin")) {
200 Ok(x) => println!("{}", x),
201 Err(e) => println!("Failed to read numbers!")
205 With `try!` in `file_product`, each `read_le_u32` need not be directly
206 concerned with error handling; instead its caller is responsible for
207 responding to errors that may occur while attempting to read the numbers.
211 #[deny(unused_must_use)];
215 use container::Container;
219 use option::{Option, Some, None};
221 use result::{Ok, Err, Result};
222 use str::{StrSlice, OwnedStr};
225 use unstable::finally::try_finally;
226 use slice::{OwnedVector, MutableVector, ImmutableVector, OwnedCloneableVector};
230 pub use self::stdio::stdin;
231 pub use self::stdio::stdout;
232 pub use self::stdio::stderr;
233 pub use self::stdio::print;
234 pub use self::stdio::println;
236 pub use self::fs::File;
237 pub use self::timer::Timer;
238 pub use self::net::ip::IpAddr;
239 pub use self::net::tcp::TcpListener;
240 pub use self::net::tcp::TcpStream;
241 pub use self::net::udp::UdpStream;
242 pub use self::pipe::PipeStream;
243 pub use self::process::{Process, ProcessConfig};
244 pub use self::tempfile::TempDir;
246 pub use self::mem::{MemReader, BufReader, MemWriter, BufWriter};
247 pub use self::buffered::{BufferedReader, BufferedWriter, BufferedStream,
249 pub use self::comm_adapters::{ChanReader, ChanWriter};
251 // this comes first to get the iotest! macro
269 /// The default buffer size for various I/O operations
270 // libuv recommends 64k buffers to maximize throughput
271 // https://groups.google.com/forum/#!topic/libuv/oQO1HJAIDdA
272 static DEFAULT_BUF_SIZE: uint = 1024 * 64;
274 /// A convenient typedef of the return value of any I/O action.
275 pub type IoResult<T> = Result<T, IoError>;
277 /// The type passed to I/O condition handlers to indicate error
281 /// Is something like this sufficient? It's kind of archaic
282 #[deriving(Eq, Clone)]
284 /// An enumeration which can be matched against for determining the flavor
287 /// A human-readable description about the error
289 /// Detailed information about this error, not always available
293 impl fmt::Show for IoError {
294 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
295 try!(fmt.buf.write_str(self.desc));
297 Some(ref s) => write!(fmt.buf, " ({})", *s),
303 /// A list specifying general categories of I/O error.
304 #[deriving(Eq, Clone, Show)]
305 #[allow(missing_doc)]
306 pub enum IoErrorKind {
307 /// Any I/O error not part of this list.
309 /// The operation could not complete because end of file was reached.
311 /// The file was not found.
313 /// The file permissions disallowed access to this file.
315 /// A network connection failed for some reason not specified in this list.
317 /// The network operation failed because the network connection was cloesd.
319 /// The connection was refused by the remote server.
321 /// The connection was reset by the remote server.
323 /// The connection was aborted (terminated) by the remote server.
325 /// The network operation failed because it was not connected yet.
327 /// The operation failed because a pipe was closed.
329 /// A file already existed with that name.
331 /// No file exists at that location.
333 /// The path did not specify the type of file that this operation required. For example,
334 /// attempting to copy a directory with the `fs::copy()` operation will fail with this error.
335 MismatchedFileTypeForOperation,
336 /// The operation temporarily failed (for example, because a signal was received), and retrying
339 /// No I/O functionality is available for this task.
341 /// A parameter was incorrect in a way that caused an I/O error not part of this list.
345 /// A trait for objects which are byte-oriented streams. Readers are defined by
346 /// one method, `read`. This function will block until data is available,
347 /// filling in the provided buffer with any data read.
349 /// Readers are intended to be composable with one another. Many objects
350 /// throughout the I/O and related libraries take and provide types which
351 /// implement the `Reader` trait.
354 // Only method which need to get implemented for this trait
356 /// Read bytes, up to the length of `buf` and place them in `buf`.
357 /// Returns the number of bytes read. The number of bytes read my
358 /// be less than the number requested, even 0. Returns `Err` on EOF.
362 /// If an error occurs during this I/O operation, then it is returned as
363 /// `Err(IoError)`. Note that end-of-file is considered an error, and can be
364 /// inspected for in the error's `kind` field. Also note that reading 0
365 /// bytes is not considered an error in all circumstances
366 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>;
368 // Convenient helper methods based on the above methods
370 /// Reads a single byte. Returns `Err` on EOF.
371 fn read_byte(&mut self) -> IoResult<u8> {
374 match self.read(buf) {
376 Ok(1) => return Ok(buf[0]),
377 Ok(_) => unreachable!(),
378 Err(e) => return Err(e)
383 /// Fills the provided slice with bytes from this reader
385 /// This will continue to call `read` until the slice has been completely
386 /// filled with bytes.
390 /// If an error occurs at any point, that error is returned, and no further
392 fn fill(&mut self, buf: &mut [u8]) -> IoResult<()> {
394 while read < buf.len() {
395 read += try!(self.read(buf.mut_slice_from(read)));
400 /// Reads exactly `len` bytes and appends them to a vector.
402 /// May push fewer than the requested number of bytes on error
403 /// or EOF. If `Ok(())` is returned, then all of the requested bytes were
404 /// pushed on to the vector, otherwise the amount `len` bytes couldn't be
405 /// read (an error was encountered), and the error is returned.
406 fn push_exact(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()> {
412 let start_len = buf.len();
413 let mut s = State { buf: buf, total_read: 0 };
415 s.buf.reserve_additional(len);
416 unsafe { s.buf.set_len(start_len + len); }
421 while s.total_read < len {
422 let len = s.buf.len();
423 let slice = s.buf.mut_slice(start_len + s.total_read, len);
424 match self.read(slice) {
426 s.total_read += nread;
428 Err(e) => return Err(e)
433 |s| unsafe { s.buf.set_len(start_len + s.total_read) })
436 /// Reads exactly `len` bytes and gives you back a new vector of length
441 /// Fails with the same conditions as `read`. Additionally returns error
442 /// on EOF. Note that if an error is returned, then some number of bytes may
443 /// have already been consumed from the underlying reader, and they are lost
444 /// (not returned as part of the error). If this is unacceptable, then it is
445 /// recommended to use the `push_exact` or `read` methods.
446 fn read_exact(&mut self, len: uint) -> IoResult<~[u8]> {
447 let mut buf = slice::with_capacity(len);
448 match self.push_exact(&mut buf, len) {
454 /// Reads all remaining bytes from the stream.
458 /// Returns any non-EOF error immediately. Previously read bytes are
459 /// discarded when an error is returned.
461 /// When EOF is encountered, all bytes read up to that point are returned.
462 fn read_to_end(&mut self) -> IoResult<~[u8]> {
463 let mut buf = slice::with_capacity(DEFAULT_BUF_SIZE);
465 match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) {
467 Err(ref e) if e.kind == EndOfFile => break,
468 Err(e) => return Err(e)
474 /// Reads all of the remaining bytes of this stream, interpreting them as a
475 /// UTF-8 encoded stream. The corresponding string is returned.
479 /// This function returns all of the same errors as `read_to_end` with an
480 /// additional error if the reader's contents are not a valid sequence of
482 fn read_to_str(&mut self) -> IoResult<~str> {
483 self.read_to_end().and_then(|s| {
484 match str::from_utf8_owned(s) {
486 None => Err(standard_error(InvalidInput)),
491 /// Create an iterator that reads a single byte on
492 /// each iteration, until EOF.
496 /// Any error other than `EndOfFile` that is produced by the underlying Reader
497 /// is returned by the iterator and should be handled by the caller.
498 fn bytes<'r>(&'r mut self) -> extensions::Bytes<'r, Self> {
499 extensions::Bytes::new(self)
502 // Byte conversion helpers
504 /// Reads `n` little-endian unsigned integer bytes.
506 /// `n` must be between 1 and 8, inclusive.
507 fn read_le_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
508 assert!(nbytes > 0 && nbytes <= 8);
514 val += (try!(self.read_u8()) as u64) << pos;
521 /// Reads `n` little-endian signed integer bytes.
523 /// `n` must be between 1 and 8, inclusive.
524 fn read_le_int_n(&mut self, nbytes: uint) -> IoResult<i64> {
525 self.read_le_uint_n(nbytes).map(|i| extend_sign(i, nbytes))
528 /// Reads `n` big-endian unsigned integer bytes.
530 /// `n` must be between 1 and 8, inclusive.
531 fn read_be_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
532 assert!(nbytes > 0 && nbytes <= 8);
538 val += (try!(self.read_u8()) as u64) << i * 8;
543 /// Reads `n` big-endian signed integer bytes.
545 /// `n` must be between 1 and 8, inclusive.
546 fn read_be_int_n(&mut self, nbytes: uint) -> IoResult<i64> {
547 self.read_be_uint_n(nbytes).map(|i| extend_sign(i, nbytes))
550 /// Reads a little-endian unsigned integer.
552 /// The number of bytes returned is system-dependant.
553 fn read_le_uint(&mut self) -> IoResult<uint> {
554 self.read_le_uint_n(uint::BYTES).map(|i| i as uint)
557 /// Reads a little-endian integer.
559 /// The number of bytes returned is system-dependant.
560 fn read_le_int(&mut self) -> IoResult<int> {
561 self.read_le_int_n(int::BYTES).map(|i| i as int)
564 /// Reads a big-endian unsigned integer.
566 /// The number of bytes returned is system-dependant.
567 fn read_be_uint(&mut self) -> IoResult<uint> {
568 self.read_be_uint_n(uint::BYTES).map(|i| i as uint)
571 /// Reads a big-endian integer.
573 /// The number of bytes returned is system-dependant.
574 fn read_be_int(&mut self) -> IoResult<int> {
575 self.read_be_int_n(int::BYTES).map(|i| i as int)
578 /// Reads a big-endian `u64`.
580 /// `u64`s are 8 bytes long.
581 fn read_be_u64(&mut self) -> IoResult<u64> {
582 self.read_be_uint_n(8)
585 /// Reads a big-endian `u32`.
587 /// `u32`s are 4 bytes long.
588 fn read_be_u32(&mut self) -> IoResult<u32> {
589 self.read_be_uint_n(4).map(|i| i as u32)
592 /// Reads a big-endian `u16`.
594 /// `u16`s are 2 bytes long.
595 fn read_be_u16(&mut self) -> IoResult<u16> {
596 self.read_be_uint_n(2).map(|i| i as u16)
599 /// Reads a big-endian `i64`.
601 /// `i64`s are 8 bytes long.
602 fn read_be_i64(&mut self) -> IoResult<i64> {
603 self.read_be_int_n(8)
606 /// Reads a big-endian `i32`.
608 /// `i32`s are 4 bytes long.
609 fn read_be_i32(&mut self) -> IoResult<i32> {
610 self.read_be_int_n(4).map(|i| i as i32)
613 /// Reads a big-endian `i16`.
615 /// `i16`s are 2 bytes long.
616 fn read_be_i16(&mut self) -> IoResult<i16> {
617 self.read_be_int_n(2).map(|i| i as i16)
620 /// Reads a big-endian `f64`.
622 /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
623 fn read_be_f64(&mut self) -> IoResult<f64> {
624 self.read_be_u64().map(|i| unsafe {
625 cast::transmute::<u64, f64>(i)
629 /// Reads a big-endian `f32`.
631 /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
632 fn read_be_f32(&mut self) -> IoResult<f32> {
633 self.read_be_u32().map(|i| unsafe {
634 cast::transmute::<u32, f32>(i)
638 /// Reads a little-endian `u64`.
640 /// `u64`s are 8 bytes long.
641 fn read_le_u64(&mut self) -> IoResult<u64> {
642 self.read_le_uint_n(8)
645 /// Reads a little-endian `u32`.
647 /// `u32`s are 4 bytes long.
648 fn read_le_u32(&mut self) -> IoResult<u32> {
649 self.read_le_uint_n(4).map(|i| i as u32)
652 /// Reads a little-endian `u16`.
654 /// `u16`s are 2 bytes long.
655 fn read_le_u16(&mut self) -> IoResult<u16> {
656 self.read_le_uint_n(2).map(|i| i as u16)
659 /// Reads a little-endian `i64`.
661 /// `i64`s are 8 bytes long.
662 fn read_le_i64(&mut self) -> IoResult<i64> {
663 self.read_le_int_n(8)
666 /// Reads a little-endian `i32`.
668 /// `i32`s are 4 bytes long.
669 fn read_le_i32(&mut self) -> IoResult<i32> {
670 self.read_le_int_n(4).map(|i| i as i32)
673 /// Reads a little-endian `i16`.
675 /// `i16`s are 2 bytes long.
676 fn read_le_i16(&mut self) -> IoResult<i16> {
677 self.read_le_int_n(2).map(|i| i as i16)
680 /// Reads a little-endian `f64`.
682 /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
683 fn read_le_f64(&mut self) -> IoResult<f64> {
684 self.read_le_u64().map(|i| unsafe {
685 cast::transmute::<u64, f64>(i)
689 /// Reads a little-endian `f32`.
691 /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
692 fn read_le_f32(&mut self) -> IoResult<f32> {
693 self.read_le_u32().map(|i| unsafe {
694 cast::transmute::<u32, f32>(i)
700 /// `u8`s are 1 byte.
701 fn read_u8(&mut self) -> IoResult<u8> {
707 /// `i8`s are 1 byte.
708 fn read_i8(&mut self) -> IoResult<i8> {
709 self.read_byte().map(|i| i as i8)
712 /// Creates a wrapper around a mutable reference to the reader.
714 /// This is useful to allow applying adaptors while still
715 /// retaining ownership of the original value.
716 fn by_ref<'a>(&'a mut self) -> RefReader<'a, Self> {
717 RefReader { inner: self }
721 impl Reader for ~Reader {
722 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
725 impl<'a> Reader for &'a mut Reader {
726 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
729 /// A `RefReader` is a struct implementing `Reader` which contains a reference
730 /// to another reader. This is often useful when composing streams.
736 /// # fn process_input<R: Reader>(r: R) {}
739 /// use std::io::util::LimitReader;
741 /// let mut stream = io::stdin();
743 /// // Only allow the function to process at most one kilobyte of input
745 /// let stream = LimitReader::new(stream.by_ref(), 1024);
746 /// process_input(stream);
749 /// // 'stream' is still available for use here
753 pub struct RefReader<'a, R> {
754 /// The underlying reader which this is referencing
758 impl<'a, R: Reader> Reader for RefReader<'a, R> {
759 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.inner.read(buf) }
762 fn extend_sign(val: u64, nbytes: uint) -> i64 {
763 let shift = (8 - nbytes) * 8;
764 (val << shift) as i64 >> shift
767 /// A trait for objects which are byte-oriented streams. Writers are defined by
768 /// one method, `write`. This function will block until the provided buffer of
769 /// bytes has been entirely written, and it will return any failurs which occur.
771 /// Another commonly overriden method is the `flush` method for writers such as
772 /// buffered writers.
774 /// Writers are intended to be composable with one another. Many objects
775 /// throughout the I/O and related libraries take and provide types which
776 /// implement the `Writer` trait.
778 /// Write the entirety of a given buffer
782 /// If an error happens during the I/O operation, the error is returned as
783 /// `Err`. Note that it is considered an error if the entire buffer could
784 /// not be written, and if an error is returned then it is unknown how much
785 /// data (if any) was actually written.
786 fn write(&mut self, buf: &[u8]) -> IoResult<()>;
788 /// Flush this output stream, ensuring that all intermediately buffered
789 /// contents reach their destination.
791 /// This is by default a no-op and implementers of the `Writer` trait should
792 /// decide whether their stream needs to be buffered or not.
793 fn flush(&mut self) -> IoResult<()> { Ok(()) }
795 /// Write a rust string into this sink.
797 /// The bytes written will be the UTF-8 encoded version of the input string.
798 /// If other encodings are desired, it is recommended to compose this stream
799 /// with another performing the conversion, or to use `write` with a
800 /// converted byte-array instead.
801 fn write_str(&mut self, s: &str) -> IoResult<()> {
802 self.write(s.as_bytes())
805 /// Writes a string into this sink, and then writes a literal newline (`\n`)
806 /// byte afterwards. Note that the writing of the newline is *not* atomic in
807 /// the sense that the call to `write` is invoked twice (once with the
808 /// string and once with a newline character).
810 /// If other encodings or line ending flavors are desired, it is recommended
811 /// that the `write` method is used specifically instead.
812 fn write_line(&mut self, s: &str) -> IoResult<()> {
813 self.write_str(s).and_then(|()| self.write(['\n' as u8]))
816 /// Write a single char, encoded as UTF-8.
817 fn write_char(&mut self, c: char) -> IoResult<()> {
818 let mut buf = [0u8, ..4];
819 let n = c.encode_utf8(buf.as_mut_slice());
820 self.write(buf.slice_to(n))
823 /// Write the result of passing n through `int::to_str_bytes`.
824 fn write_int(&mut self, n: int) -> IoResult<()> {
825 write!(self, "{:d}", n)
828 /// Write the result of passing n through `uint::to_str_bytes`.
829 fn write_uint(&mut self, n: uint) -> IoResult<()> {
830 write!(self, "{:u}", n)
833 /// Write a little-endian uint (number of bytes depends on system).
834 fn write_le_uint(&mut self, n: uint) -> IoResult<()> {
835 extensions::u64_to_le_bytes(n as u64, uint::BYTES, |v| self.write(v))
838 /// Write a little-endian int (number of bytes depends on system).
839 fn write_le_int(&mut self, n: int) -> IoResult<()> {
840 extensions::u64_to_le_bytes(n as u64, int::BYTES, |v| self.write(v))
843 /// Write a big-endian uint (number of bytes depends on system).
844 fn write_be_uint(&mut self, n: uint) -> IoResult<()> {
845 extensions::u64_to_be_bytes(n as u64, uint::BYTES, |v| self.write(v))
848 /// Write a big-endian int (number of bytes depends on system).
849 fn write_be_int(&mut self, n: int) -> IoResult<()> {
850 extensions::u64_to_be_bytes(n as u64, int::BYTES, |v| self.write(v))
853 /// Write a big-endian u64 (8 bytes).
854 fn write_be_u64(&mut self, n: u64) -> IoResult<()> {
855 extensions::u64_to_be_bytes(n, 8u, |v| self.write(v))
858 /// Write a big-endian u32 (4 bytes).
859 fn write_be_u32(&mut self, n: u32) -> IoResult<()> {
860 extensions::u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
863 /// Write a big-endian u16 (2 bytes).
864 fn write_be_u16(&mut self, n: u16) -> IoResult<()> {
865 extensions::u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
868 /// Write a big-endian i64 (8 bytes).
869 fn write_be_i64(&mut self, n: i64) -> IoResult<()> {
870 extensions::u64_to_be_bytes(n as u64, 8u, |v| self.write(v))
873 /// Write a big-endian i32 (4 bytes).
874 fn write_be_i32(&mut self, n: i32) -> IoResult<()> {
875 extensions::u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
878 /// Write a big-endian i16 (2 bytes).
879 fn write_be_i16(&mut self, n: i16) -> IoResult<()> {
880 extensions::u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
883 /// Write a big-endian IEEE754 double-precision floating-point (8 bytes).
884 fn write_be_f64(&mut self, f: f64) -> IoResult<()> {
886 self.write_be_u64(cast::transmute(f))
890 /// Write a big-endian IEEE754 single-precision floating-point (4 bytes).
891 fn write_be_f32(&mut self, f: f32) -> IoResult<()> {
893 self.write_be_u32(cast::transmute(f))
897 /// Write a little-endian u64 (8 bytes).
898 fn write_le_u64(&mut self, n: u64) -> IoResult<()> {
899 extensions::u64_to_le_bytes(n, 8u, |v| self.write(v))
902 /// Write a little-endian u32 (4 bytes).
903 fn write_le_u32(&mut self, n: u32) -> IoResult<()> {
904 extensions::u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
907 /// Write a little-endian u16 (2 bytes).
908 fn write_le_u16(&mut self, n: u16) -> IoResult<()> {
909 extensions::u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
912 /// Write a little-endian i64 (8 bytes).
913 fn write_le_i64(&mut self, n: i64) -> IoResult<()> {
914 extensions::u64_to_le_bytes(n as u64, 8u, |v| self.write(v))
917 /// Write a little-endian i32 (4 bytes).
918 fn write_le_i32(&mut self, n: i32) -> IoResult<()> {
919 extensions::u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
922 /// Write a little-endian i16 (2 bytes).
923 fn write_le_i16(&mut self, n: i16) -> IoResult<()> {
924 extensions::u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
927 /// Write a little-endian IEEE754 double-precision floating-point
929 fn write_le_f64(&mut self, f: f64) -> IoResult<()> {
931 self.write_le_u64(cast::transmute(f))
935 /// Write a little-endian IEEE754 single-precision floating-point
937 fn write_le_f32(&mut self, f: f32) -> IoResult<()> {
939 self.write_le_u32(cast::transmute(f))
943 /// Write a u8 (1 byte).
944 fn write_u8(&mut self, n: u8) -> IoResult<()> {
948 /// Write a i8 (1 byte).
949 fn write_i8(&mut self, n: i8) -> IoResult<()> {
950 self.write([n as u8])
953 /// Creates a wrapper around a mutable reference to the writer.
955 /// This is useful to allow applying wrappers while still
956 /// retaining ownership of the original value.
957 fn by_ref<'a>(&'a mut self) -> RefWriter<'a, Self> {
958 RefWriter { inner: self }
962 impl Writer for ~Writer {
963 fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.write(buf) }
964 fn flush(&mut self) -> IoResult<()> { self.flush() }
967 impl<'a> Writer for &'a mut Writer {
968 fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.write(buf) }
969 fn flush(&mut self) -> IoResult<()> { self.flush() }
972 /// A `RefWriter` is a struct implementing `Writer` which contains a reference
973 /// to another writer. This is often useful when composing streams.
979 /// # fn process_input<R: Reader>(r: R) {}
981 /// use std::io::util::TeeReader;
982 /// use std::io::{stdin, MemWriter};
984 /// let mut output = MemWriter::new();
987 /// // Don't give ownership of 'output' to the 'tee'. Instead we keep a
988 /// // handle to it in the outer scope
989 /// let mut tee = TeeReader::new(stdin(), output.by_ref());
990 /// process_input(tee);
993 /// println!("input processed: {}", output.unwrap());
996 pub struct RefWriter<'a, W> {
997 /// The underlying writer which this is referencing
1001 impl<'a, W: Writer> Writer for RefWriter<'a, W> {
1002 fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.inner.write(buf) }
1003 fn flush(&mut self) -> IoResult<()> { self.inner.flush() }
1007 /// A Stream is a readable and a writable object. Data written is typically
1008 /// received by the object which reads receive data from.
1009 pub trait Stream: Reader + Writer { }
1011 impl<T: Reader + Writer> Stream for T {}
1013 /// An iterator that reads a line on each iteration,
1014 /// until `.read_line()` encounters `EndOfFile`.
1016 /// # Notes about the Iteration Protocol
1018 /// The `Lines` may yield `None` and thus terminate
1019 /// an iteration, but continue to yield elements if iteration
1020 /// is attempted again.
1024 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1025 /// is returned by the iterator and should be handled by the caller.
1026 pub struct Lines<'r, T> {
1027 priv buffer: &'r mut T,
1030 impl<'r, T: Buffer> Iterator<IoResult<~str>> for Lines<'r, T> {
1031 fn next(&mut self) -> Option<IoResult<~str>> {
1032 match self.buffer.read_line() {
1033 Ok(x) => Some(Ok(x)),
1034 Err(IoError { kind: EndOfFile, ..}) => None,
1035 Err(y) => Some(Err(y))
1040 /// An iterator that reads a utf8-encoded character on each iteration,
1041 /// until `.read_char()` encounters `EndOfFile`.
1043 /// # Notes about the Iteration Protocol
1045 /// The `Chars` may yield `None` and thus terminate
1046 /// an iteration, but continue to yield elements if iteration
1047 /// is attempted again.
1051 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1052 /// is returned by the iterator and should be handled by the caller.
1053 pub struct Chars<'r, T> {
1054 priv buffer: &'r mut T
1057 impl<'r, T: Buffer> Iterator<IoResult<char>> for Chars<'r, T> {
1058 fn next(&mut self) -> Option<IoResult<char>> {
1059 match self.buffer.read_char() {
1060 Ok(x) => Some(Ok(x)),
1061 Err(IoError { kind: EndOfFile, ..}) => None,
1062 Err(y) => Some(Err(y))
1067 /// A Buffer is a type of reader which has some form of internal buffering to
1068 /// allow certain kinds of reading operations to be more optimized than others.
1069 /// This type extends the `Reader` trait with a few methods that are not
1070 /// possible to reasonably implement with purely a read interface.
1071 pub trait Buffer: Reader {
1072 /// Fills the internal buffer of this object, returning the buffer contents.
1073 /// Note that none of the contents will be "read" in the sense that later
1074 /// calling `read` may return the same contents.
1076 /// The `consume` function must be called with the number of bytes that are
1077 /// consumed from this buffer returned to ensure that the bytes are never
1082 /// This function will return an I/O error if the underlying reader was
1083 /// read, but returned an error. Note that it is not an error to return a
1084 /// 0-length buffer.
1085 fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]>;
1087 /// Tells this buffer that `amt` bytes have been consumed from the buffer,
1088 /// so they should no longer be returned in calls to `fill` or `read`.
1089 fn consume(&mut self, amt: uint);
1091 /// Reads the next line of input, interpreted as a sequence of UTF-8
1092 /// encoded unicode codepoints. If a newline is encountered, then the
1093 /// newline is contained in the returned string.
1100 /// let mut reader = io::stdin();
1101 /// let input = reader.read_line().ok().unwrap_or(~"nothing");
1106 /// This function has the same error semantics as `read_until`:
1108 /// * All non-EOF errors will be returned immediately
1109 /// * If an error is returned previously consumed bytes are lost
1110 /// * EOF is only returned if no bytes have been read
1111 /// * Reach EOF may mean that the delimiter is not present in the return
1114 /// Additionally, this function can fail if the line of input read is not a
1115 /// valid UTF-8 sequence of bytes.
1116 fn read_line(&mut self) -> IoResult<~str> {
1117 self.read_until('\n' as u8).and_then(|line|
1118 match str::from_utf8_owned(line) {
1120 None => Err(standard_error(InvalidInput)),
1125 /// Create an iterator that reads a line on each iteration until EOF.
1129 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1130 /// is returned by the iterator and should be handled by the caller.
1131 fn lines<'r>(&'r mut self) -> Lines<'r, Self> {
1132 Lines { buffer: self }
1135 /// Reads a sequence of bytes leading up to a specified delimiter. Once the
1136 /// specified byte is encountered, reading ceases and the bytes up to and
1137 /// including the delimiter are returned.
1141 /// If any I/O error is encountered other than EOF, the error is immediately
1142 /// returned. Note that this may discard bytes which have already been read,
1143 /// and those bytes will *not* be returned. It is recommended to use other
1144 /// methods if this case is worrying.
1146 /// If EOF is encountered, then this function will return EOF if 0 bytes
1147 /// have been read, otherwise the pending byte buffer is returned. This
1148 /// is the reason that the byte buffer returned may not always contain the
1150 fn read_until(&mut self, byte: u8) -> IoResult<~[u8]> {
1156 let available = match self.fill_buf() {
1158 Err(ref e) if res.len() > 0 && e.kind == EndOfFile => {
1162 Err(e) => return Err(e)
1164 match available.iter().position(|&b| b == byte) {
1166 res.push_all(available.slice_to(i + 1));
1171 res.push_all(available);
1172 used = available.len();
1182 /// Reads the next utf8-encoded character from the underlying stream.
1186 /// If an I/O error occurs, or EOF, then this function will return `Err`.
1187 /// This function will also return error if the stream does not contain a
1188 /// valid utf-8 encoded codepoint as the next few bytes in the stream.
1189 fn read_char(&mut self) -> IoResult<char> {
1190 let first_byte = try!(self.read_byte());
1191 let width = str::utf8_char_width(first_byte);
1192 if width == 1 { return Ok(first_byte as char) }
1193 if width == 0 { return Err(standard_error(InvalidInput)) } // not utf8
1194 let mut buf = [first_byte, 0, 0, 0];
1197 while start < width {
1198 match try!(self.read(buf.mut_slice(start, width))) {
1199 n if n == width - start => break,
1200 n if n < width - start => { start += n; }
1201 _ => return Err(standard_error(InvalidInput)),
1205 match str::from_utf8(buf.slice_to(width)) {
1206 Some(s) => Ok(s.char_at(0)),
1207 None => Err(standard_error(InvalidInput))
1211 /// Create an iterator that reads a utf8-encoded character on each iteration
1216 /// Any error other than `EndOfFile` that is produced by the underlying Reader
1217 /// is returned by the iterator and should be handled by the caller.
1218 fn chars<'r>(&'r mut self) -> Chars<'r, Self> {
1219 Chars { buffer: self }
1223 /// When seeking, the resulting cursor is offset from a base by the offset given
1224 /// to the `seek` function. The base used is specified by this enumeration.
1225 pub enum SeekStyle {
1226 /// Seek from the beginning of the stream
1228 /// Seek from the end of the stream
1230 /// Seek from the current position
1234 /// An object implementing `Seek` internally has some form of cursor which can
1235 /// be moved within a stream of bytes. The stream typically has a fixed size,
1236 /// allowing seeking relative to either end.
1238 /// Return position of file cursor in the stream
1239 fn tell(&self) -> IoResult<u64>;
1241 /// Seek to an offset in a stream
1243 /// A successful seek clears the EOF indicator. Seeking beyond EOF is
1244 /// allowed, but seeking before position 0 is not allowed.
1248 /// * Seeking to a negative offset is considered an error
1249 /// * Seeking past the end of the stream does not modify the underlying
1250 /// stream, but the next write may cause the previous data to be filled in
1251 /// with a bit pattern.
1252 fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>;
1255 /// A listener is a value that can consume itself to start listening for
1258 /// Doing so produces some sort of Acceptor.
1259 pub trait Listener<T, A: Acceptor<T>> {
1260 /// Spin up the listener and start queuing incoming connections
1264 /// Returns `Err` if this listener could not be bound to listen for
1265 /// connections. In all cases, this listener is consumed.
1266 fn listen(self) -> IoResult<A>;
1269 /// An acceptor is a value that presents incoming connections
1270 pub trait Acceptor<T> {
1271 /// Wait for and accept an incoming connection
1275 /// Returns `Err` if an I/O error is encountered.
1276 fn accept(&mut self) -> IoResult<T>;
1278 /// Create an iterator over incoming connection attempts.
1280 /// Note that I/O errors will be yielded by the iterator itself.
1281 fn incoming<'r>(&'r mut self) -> IncomingConnections<'r, Self> {
1282 IncomingConnections { inc: self }
1286 /// An infinite iterator over incoming connection attempts.
1287 /// Calling `next` will block the task until a connection is attempted.
1289 /// Since connection attempts can continue forever, this iterator always returns
1290 /// `Some`. The `Some` contains the `IoResult` representing whether the
1291 /// connection attempt was successful. A successful connection will be wrapped
1292 /// in `Ok`. A failed connection is represented as an `Err`.
1293 pub struct IncomingConnections<'a, A> {
1294 priv inc: &'a mut A,
1297 impl<'a, T, A: Acceptor<T>> Iterator<IoResult<T>> for IncomingConnections<'a, A> {
1298 fn next(&mut self) -> Option<IoResult<T>> {
1299 Some(self.inc.accept())
1303 /// Creates a standard error for a commonly used flavor of error. The `detail`
1304 /// field of the returned error will always be `None`.
1311 /// let eof = io::standard_error(io::EndOfFile);
1312 /// let einval = io::standard_error(io::InvalidInput);
1314 pub fn standard_error(kind: IoErrorKind) -> IoError {
1315 let desc = match kind {
1316 EndOfFile => "end of file",
1317 IoUnavailable => "I/O is unavailable",
1318 InvalidInput => "invalid input",
1328 /// A mode specifies how a file should be opened or created. These modes are
1329 /// passed to `File::open_mode` and are used to control where the file is
1330 /// positioned when it is initially opened.
1332 /// Opens a file positioned at the beginning.
1334 /// Opens a file positioned at EOF.
1336 /// Opens a file, truncating it if it already exists.
1340 /// Access permissions with which the file should be opened. `File`s
1341 /// opened with `Read` will return an error if written to.
1342 pub enum FileAccess {
1343 /// Read-only access, requests to write will result in an error
1345 /// Write-only access, requests to read will result in an error
1347 /// Read-write access, no requests are denied by default
1351 /// Different kinds of files which can be identified by a call to stat
1352 #[deriving(Eq, Show, Hash)]
1354 /// This is a normal file, corresponding to `S_IFREG`
1357 /// This file is a directory, corresponding to `S_IFDIR`
1360 /// This file is a named pipe, corresponding to `S_IFIFO`
1363 /// This file is a block device, corresponding to `S_IFBLK`
1366 /// This file is a symbolic link to another file, corresponding to `S_IFLNK`
1369 /// The type of this file is not recognized as one of the other categories
1373 /// A structure used to describe metadata information about a file. This
1374 /// structure is created through the `stat` method on a `Path`.
1381 /// let info = match Path::new("foo.txt").stat() {
1382 /// Ok(stat) => stat,
1383 /// Err(e) => fail!("couldn't read foo.txt: {}", e),
1386 /// println!("path: {}", info.path.display());
1387 /// println!("byte size: {}", info.size);
1391 pub struct FileStat {
1392 /// The path that this stat structure is describing
1394 /// The size of the file, in bytes
1396 /// The kind of file this path points to (directory, file, pipe, etc.)
1398 /// The file permissions currently on the file
1399 perm: FilePermission,
1401 // FIXME(#10301): These time fields are pretty useless without an actual
1402 // time representation, what are the milliseconds relative
1405 /// The time that the file was created at, in platform-dependent
1408 /// The time that this file was last modified, in platform-dependent
1411 /// The time that this file was last accessed, in platform-dependent
1415 /// Information returned by stat() which is not guaranteed to be
1416 /// platform-independent. This information may be useful on some platforms,
1417 /// but it may have different meanings or no meaning at all on other
1420 /// Usage of this field is discouraged, but if access is desired then the
1421 /// fields are located here.
1423 unstable: UnstableFileStat,
1426 /// This structure represents all of the possible information which can be
1427 /// returned from a `stat` syscall which is not contained in the `FileStat`
1428 /// structure. This information is not necessarily platform independent, and may
1429 /// have different meanings or no meaning at all on some platforms.
1431 #[allow(missing_doc)]
1433 pub struct UnstableFileStat {
1434 /// The ID of the device containing the file.
1436 /// The file serial number.
1440 /// The number of hard links to this file.
1442 /// The user ID of the file.
1444 /// The group ID of the file.
1446 /// The optimal block size for I/O.
1448 /// The blocks allocated for this file.
1450 /// User-defined flags for the file.
1452 /// The file generation number.
1456 /// A set of permissions for a file or directory is represented by a set of
1457 /// flags which are or'd together.
1458 pub type FilePermission = u32;
1460 // Each permission bit
1461 pub static UserRead: FilePermission = 0x100;
1462 pub static UserWrite: FilePermission = 0x080;
1463 pub static UserExecute: FilePermission = 0x040;
1464 pub static GroupRead: FilePermission = 0x020;
1465 pub static GroupWrite: FilePermission = 0x010;
1466 pub static GroupExecute: FilePermission = 0x008;
1467 pub static OtherRead: FilePermission = 0x004;
1468 pub static OtherWrite: FilePermission = 0x002;
1469 pub static OtherExecute: FilePermission = 0x001;
1471 // Common combinations of these bits
1472 pub static UserRWX: FilePermission = UserRead | UserWrite | UserExecute;
1473 pub static GroupRWX: FilePermission = GroupRead | GroupWrite | GroupExecute;
1474 pub static OtherRWX: FilePermission = OtherRead | OtherWrite | OtherExecute;
1476 /// A set of permissions for user owned files, this is equivalent to 0644 on
1477 /// unix-like systems.
1478 pub static UserFile: FilePermission = UserRead | UserWrite | GroupRead | OtherRead;
1479 /// A set of permissions for user owned directories, this is equivalent to 0755
1480 /// on unix-like systems.
1481 pub static UserDir: FilePermission = UserRWX | GroupRead | GroupExecute |
1482 OtherRead | OtherExecute;
1483 /// A set of permissions for user owned executables, this is equivalent to 0755
1484 /// on unix-like systems.
1485 pub static UserExec: FilePermission = UserDir;
1487 /// A mask for all possible permission bits
1488 pub static AllPermissions: FilePermission = 0x1ff;