]> git.lizzy.rs Git - rust.git/blob - src/libstd/io/mod.rs
auto merge of #13967 : richo/rust/features/ICE-fails, r=alexcrichton
[rust.git] / src / libstd / io / mod.rs
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 // FIXME: cover these topics:
12 //        path, reader, writer, stream, raii (close not needed),
13 //        stdio, print!, println!, file access, process spawning,
14 //        error handling
15
16
17 /*! I/O, including files, networking, timers, and processes
18
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.
23
24 # Examples
25
26 Some examples of obvious things you might want to do
27
28 * Read lines from stdin
29
30     ```rust
31     use std::io;
32
33     for line in io::stdin().lines() {
34         print!("{}", line.unwrap());
35     }
36     ```
37
38 * Read a complete file
39
40     ```rust
41     use std::io::File;
42
43     let contents = File::open(&Path::new("message.txt")).read_to_end();
44     ```
45
46 * Write a line to a file
47
48     ```rust
49     # #![allow(unused_must_use)]
50     use std::io::File;
51
52     let mut file = File::create(&Path::new("message.txt"));
53     file.write(bytes!("hello, file!\n"));
54     # drop(file);
55     # ::std::io::fs::unlink(&Path::new("message.txt"));
56     ```
57
58 * Iterate over the lines of a file
59
60     ```rust,no_run
61     use std::io::BufferedReader;
62     use std::io::File;
63
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());
68     }
69     ```
70
71 * Pull the lines of a file into a vector of strings
72
73     ```rust,no_run
74     use std::io::BufferedReader;
75     use std::io::File;
76
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();
80     ```
81
82 * Make a simple TCP client connection and request
83
84     ```rust,should_fail
85     # #![allow(unused_must_use)]
86     use std::io::net::ip::SocketAddr;
87     use std::io::net::tcp::TcpStream;
88
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();
93     ```
94
95 * Make a simple TCP server
96
97     ```rust
98     # fn main() { }
99     # fn foo() {
100     # #![allow(dead_code)]
101     use std::io::{TcpListener, TcpStream};
102     use std::io::net::ip::{Ipv4Addr, SocketAddr};
103     use std::io::{Acceptor, Listener};
104
105     let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 80 };
106     let listener = TcpListener::bind(addr);
107
108     // bind the listener to the specified address
109     let mut acceptor = listener.listen();
110
111     fn handle_client(mut stream: TcpStream) {
112         // ...
113     # &mut stream; // silence unused mutability/variable warning
114     }
115     // accept connections and process them, spawning a new tasks for each one
116     for stream in acceptor.incoming() {
117         match stream {
118             Err(e) => { /* connection failed */ }
119             Ok(stream) => spawn(proc() {
120                 // connection succeeded
121                 handle_client(stream)
122             })
123         }
124     }
125
126     // close the socket server
127     drop(acceptor);
128     # }
129     ```
130
131
132 # Error Handling
133
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.
138
139 Rust's I/O employs a combination of techniques to reduce boilerplate
140 while still providing feedback about errors. The basic strategy:
141
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`
144   module.
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.
151
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
157 be an error.
158
159 If you wanted to handle the error though you might write:
160
161 ```rust
162 # #![allow(unused_must_use)]
163 use std::io::File;
164
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),
168 }
169
170 # ::std::io::fs::unlink(&Path::new("diary.txt"));
171 ```
172
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.
181
182 ## `try!`
183
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.
188
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.
192
193 If you wanted to read several `u32`s from a file and return their product:
194
195 ```rust
196 use std::io::{File, IoResult};
197
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());
202
203     Ok(x1 * x2)
204 }
205
206 match file_product(&Path::new("numbers.bin")) {
207     Ok(x) => println!("{}", x),
208     Err(e) => println!("Failed to read numbers!")
209 }
210 ```
211
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.
215
216 */
217
218 #![deny(unused_must_use)]
219
220 use cast;
221 use char::Char;
222 use container::Container;
223 use fmt;
224 use int;
225 use iter::Iterator;
226 use libc;
227 use ops::{BitOr, BitAnd, Sub};
228 use os;
229 use option::{Option, Some, None};
230 use owned::Box;
231 use path::Path;
232 use result::{Ok, Err, Result};
233 use str::StrSlice;
234 use str;
235 use uint;
236 use unstable::finally::try_finally;
237 use slice::{Vector, MutableVector, ImmutableVector};
238 use vec::Vec;
239
240 // Reexports
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;
246
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;
256
257 pub use self::mem::{MemReader, BufReader, MemWriter, BufWriter};
258 pub use self::buffered::{BufferedReader, BufferedWriter, BufferedStream,
259                          LineBufferedWriter};
260 pub use self::comm_adapters::{ChanReader, ChanWriter};
261
262 // this comes first to get the iotest! macro
263 pub mod test;
264
265 mod buffered;
266 mod comm_adapters;
267 mod mem;
268 mod result;
269 mod tempfile;
270 pub mod extensions;
271 pub mod fs;
272 pub mod net;
273 pub mod pipe;
274 pub mod process;
275 pub mod signal;
276 pub mod stdio;
277 pub mod timer;
278 pub mod util;
279
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;
284
285 /// A convenient typedef of the return value of any I/O action.
286 pub type IoResult<T> = Result<T, IoError>;
287
288 /// The type passed to I/O condition handlers to indicate error
289 ///
290 /// # FIXME
291 ///
292 /// Is something like this sufficient? It's kind of archaic
293 #[deriving(Eq, Clone)]
294 pub struct IoError {
295     /// An enumeration which can be matched against for determining the flavor
296     /// of error.
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>
302 }
303
304 impl IoError {
305     /// Convert an `errno` value into an `IoError`.
306     ///
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 {
311         #[cfg(windows)]
312         fn get_err(errno: i32) -> (IoErrorKind, &'static str) {
313             match errno {
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")
323                 }
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"),
329
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
333                 // win32 calls.
334                 libc::ERROR_INVALID_FUNCTION => (InvalidInput,
335                                                  "illegal operation on a directory"),
336
337                 _ => (OtherIoError, "unknown error")
338             }
339         }
340
341         #[cfg(not(windows))]
342         fn get_err(errno: i32) -> (IoErrorKind, &'static str) {
343             // FIXME: this should probably be a bit more descriptive...
344             match errno {
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"),
357
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"),
362
363                 _ => (OtherIoError, "unknown error")
364             }
365         }
366
367         let (kind, desc) = get_err(errno as i32);
368         IoError {
369             kind: kind,
370             desc: desc,
371             detail: if detail {Some(os::error_string(errno))} else {None},
372         }
373     }
374
375     /// Retrieve the last error to occur as a (detailed) IoError.
376     ///
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)
383     }
384 }
385
386 impl fmt::Show for IoError {
387     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
388         try!(fmt.buf.write_str(self.desc));
389         match self.detail {
390             Some(ref s) => write!(fmt.buf, " ({})", *s),
391             None => Ok(())
392         }
393     }
394 }
395
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.
400     OtherIoError,
401     /// The operation could not complete because end of file was reached.
402     EndOfFile,
403     /// The file was not found.
404     FileNotFound,
405     /// The file permissions disallowed access to this file.
406     PermissionDenied,
407     /// A network connection failed for some reason not specified in this list.
408     ConnectionFailed,
409     /// The network operation failed because the network connection was cloesd.
410     Closed,
411     /// The connection was refused by the remote server.
412     ConnectionRefused,
413     /// The connection was reset by the remote server.
414     ConnectionReset,
415     /// The connection was aborted (terminated) by the remote server.
416     ConnectionAborted,
417     /// The network operation failed because it was not connected yet.
418     NotConnected,
419     /// The operation failed because a pipe was closed.
420     BrokenPipe,
421     /// A file already existed with that name.
422     PathAlreadyExists,
423     /// No file exists at that location.
424     PathDoesntExist,
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
429     /// may succeed.
430     ResourceUnavailable,
431     /// No I/O functionality is available for this task.
432     IoUnavailable,
433     /// A parameter was incorrect in a way that caused an I/O error not part of this list.
434     InvalidInput,
435     /// The I/O operation's timeout expired, causing it to be canceled.
436     TimedOut,
437 }
438
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.
442 ///
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.
446 pub trait Reader {
447
448     // Only method which need to get implemented for this trait
449
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.
453     ///
454     /// # Error
455     ///
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>;
461
462     // Convenient helper methods based on the above methods
463
464     /// Reads a single byte. Returns `Err` on EOF.
465     fn read_byte(&mut self) -> IoResult<u8> {
466         let mut buf = [0];
467         loop {
468             match self.read(buf) {
469                 Ok(0) => {}
470                 Ok(1) => return Ok(buf[0]),
471                 Ok(_) => unreachable!(),
472                 Err(e) => return Err(e)
473             }
474         }
475     }
476
477     /// Fills the provided slice with bytes from this reader
478     ///
479     /// This will continue to call `read` until the slice has been completely
480     /// filled with bytes.
481     ///
482     /// # Error
483     ///
484     /// If an error occurs at any point, that error is returned, and no further
485     /// bytes are read.
486     fn fill(&mut self, buf: &mut [u8]) -> IoResult<()> {
487         let mut read = 0;
488         while read < buf.len() {
489             read += try!(self.read(buf.mut_slice_from(read)));
490         }
491         Ok(())
492     }
493
494     /// Reads exactly `len` bytes and appends them to a vector.
495     ///
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<()> {
501         struct State<'a> {
502             buf: &'a mut Vec<u8>,
503             total_read: uint
504         }
505
506         let start_len = buf.len();
507         let mut s = State { buf: buf, total_read: 0 };
508
509         s.buf.reserve_additional(len);
510         unsafe { s.buf.set_len(start_len + len); }
511
512         try_finally(
513             &mut s, (),
514             |s, _| {
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) {
519                         Ok(nread) => {
520                             s.total_read += nread;
521                         }
522                         Err(e) => return Err(e)
523                     }
524                 }
525                 Ok(())
526             },
527             |s| unsafe { s.buf.set_len(start_len + s.total_read) })
528     }
529
530     /// Reads exactly `len` bytes and gives you back a new vector of length
531     /// `len`
532     ///
533     /// # Error
534     ///
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) {
543             Ok(()) => Ok(buf),
544             Err(e) => Err(e),
545         }
546     }
547
548     /// Reads all remaining bytes from the stream.
549     ///
550     /// # Error
551     ///
552     /// Returns any non-EOF error immediately. Previously read bytes are
553     /// discarded when an error is returned.
554     ///
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);
558         loop {
559             match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) {
560                 Ok(()) => {}
561                 Err(ref e) if e.kind == EndOfFile => break,
562                 Err(e) => return Err(e)
563             }
564         }
565         return Ok(buf);
566     }
567
568     /// Reads all of the remaining bytes of this stream, interpreting them as a
569     /// UTF-8 encoded stream. The corresponding string is returned.
570     ///
571     /// # Error
572     ///
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
575     /// UTF-8 bytes.
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)),
581             }
582         })
583     }
584
585     /// Create an iterator that reads a single byte on
586     /// each iteration, until EOF.
587     ///
588     /// # Error
589     ///
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)
594     }
595
596     // Byte conversion helpers
597
598     /// Reads `n` little-endian unsigned integer bytes.
599     ///
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);
603
604         let mut val = 0u64;
605         let mut pos = 0;
606         let mut i = nbytes;
607         while i > 0 {
608             val += (try!(self.read_u8()) as u64) << pos;
609             pos += 8;
610             i -= 1;
611         }
612         Ok(val)
613     }
614
615     /// Reads `n` little-endian signed integer bytes.
616     ///
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))
620     }
621
622     /// Reads `n` big-endian unsigned integer bytes.
623     ///
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);
627
628         let mut val = 0u64;
629         let mut i = nbytes;
630         while i > 0 {
631             i -= 1;
632             val += (try!(self.read_u8()) as u64) << i * 8;
633         }
634         Ok(val)
635     }
636
637     /// Reads `n` big-endian signed integer bytes.
638     ///
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))
642     }
643
644     /// Reads a little-endian unsigned integer.
645     ///
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)
649     }
650
651     /// Reads a little-endian integer.
652     ///
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)
656     }
657
658     /// Reads a big-endian unsigned integer.
659     ///
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)
663     }
664
665     /// Reads a big-endian integer.
666     ///
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)
670     }
671
672     /// Reads a big-endian `u64`.
673     ///
674     /// `u64`s are 8 bytes long.
675     fn read_be_u64(&mut self) -> IoResult<u64> {
676         self.read_be_uint_n(8)
677     }
678
679     /// Reads a big-endian `u32`.
680     ///
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)
684     }
685
686     /// Reads a big-endian `u16`.
687     ///
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)
691     }
692
693     /// Reads a big-endian `i64`.
694     ///
695     /// `i64`s are 8 bytes long.
696     fn read_be_i64(&mut self) -> IoResult<i64> {
697         self.read_be_int_n(8)
698     }
699
700     /// Reads a big-endian `i32`.
701     ///
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)
705     }
706
707     /// Reads a big-endian `i16`.
708     ///
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)
712     }
713
714     /// Reads a big-endian `f64`.
715     ///
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)
720         })
721     }
722
723     /// Reads a big-endian `f32`.
724     ///
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)
729         })
730     }
731
732     /// Reads a little-endian `u64`.
733     ///
734     /// `u64`s are 8 bytes long.
735     fn read_le_u64(&mut self) -> IoResult<u64> {
736         self.read_le_uint_n(8)
737     }
738
739     /// Reads a little-endian `u32`.
740     ///
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)
744     }
745
746     /// Reads a little-endian `u16`.
747     ///
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)
751     }
752
753     /// Reads a little-endian `i64`.
754     ///
755     /// `i64`s are 8 bytes long.
756     fn read_le_i64(&mut self) -> IoResult<i64> {
757         self.read_le_int_n(8)
758     }
759
760     /// Reads a little-endian `i32`.
761     ///
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)
765     }
766
767     /// Reads a little-endian `i16`.
768     ///
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)
772     }
773
774     /// Reads a little-endian `f64`.
775     ///
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)
780         })
781     }
782
783     /// Reads a little-endian `f32`.
784     ///
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)
789         })
790     }
791
792     /// Read a u8.
793     ///
794     /// `u8`s are 1 byte.
795     fn read_u8(&mut self) -> IoResult<u8> {
796         self.read_byte()
797     }
798
799     /// Read an i8.
800     ///
801     /// `i8`s are 1 byte.
802     fn read_i8(&mut self) -> IoResult<i8> {
803         self.read_byte().map(|i| i as i8)
804     }
805
806     /// Creates a wrapper around a mutable reference to the reader.
807     ///
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 }
812     }
813 }
814
815 impl Reader for Box<Reader> {
816     fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
817 }
818
819 impl<'a> Reader for &'a mut Reader {
820     fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
821 }
822
823 /// A `RefReader` is a struct implementing `Reader` which contains a reference
824 /// to another reader. This is often useful when composing streams.
825 ///
826 /// # Example
827 ///
828 /// ```
829 /// # fn main() {}
830 /// # fn process_input<R: Reader>(r: R) {}
831 /// # fn foo() {
832 /// use std::io;
833 /// use std::io::util::LimitReader;
834 ///
835 /// let mut stream = io::stdin();
836 ///
837 /// // Only allow the function to process at most one kilobyte of input
838 /// {
839 ///     let stream = LimitReader::new(stream.by_ref(), 1024);
840 ///     process_input(stream);
841 /// }
842 ///
843 /// // 'stream' is still available for use here
844 ///
845 /// # }
846 /// ```
847 pub struct RefReader<'a, R> {
848     /// The underlying reader which this is referencing
849     inner: &'a mut R
850 }
851
852 impl<'a, R: Reader> Reader for RefReader<'a, R> {
853     fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.inner.read(buf) }
854 }
855
856 fn extend_sign(val: u64, nbytes: uint) -> i64 {
857     let shift = (8 - nbytes) * 8;
858     (val << shift) as i64 >> shift
859 }
860
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.
864 ///
865 /// Another commonly overriden method is the `flush` method for writers such as
866 /// buffered writers.
867 ///
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.
871 pub trait Writer {
872     /// Write the entirety of a given buffer
873     ///
874     /// # Errors
875     ///
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<()>;
881
882     /// Flush this output stream, ensuring that all intermediately buffered
883     /// contents reach their destination.
884     ///
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(()) }
888
889     /// Write a rust string into this sink.
890     ///
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())
897     }
898
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).
903     ///
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]))
908     }
909
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))
915     }
916
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)
920     }
921
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)
925     }
926
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))
930     }
931
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))
935     }
936
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))
940     }
941
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))
945     }
946
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))
950     }
951
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))
955     }
956
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))
960     }
961
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))
965     }
966
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))
970     }
971
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))
975     }
976
977     /// Write a big-endian IEEE754 double-precision floating-point (8 bytes).
978     fn write_be_f64(&mut self, f: f64) -> IoResult<()> {
979         unsafe {
980             self.write_be_u64(cast::transmute(f))
981         }
982     }
983
984     /// Write a big-endian IEEE754 single-precision floating-point (4 bytes).
985     fn write_be_f32(&mut self, f: f32) -> IoResult<()> {
986         unsafe {
987             self.write_be_u32(cast::transmute(f))
988         }
989     }
990
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))
994     }
995
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))
999     }
1000
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))
1004     }
1005
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))
1009     }
1010
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))
1014     }
1015
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))
1019     }
1020
1021     /// Write a little-endian IEEE754 double-precision floating-point
1022     /// (8 bytes).
1023     fn write_le_f64(&mut self, f: f64) -> IoResult<()> {
1024         unsafe {
1025             self.write_le_u64(cast::transmute(f))
1026         }
1027     }
1028
1029     /// Write a little-endian IEEE754 single-precision floating-point
1030     /// (4 bytes).
1031     fn write_le_f32(&mut self, f: f32) -> IoResult<()> {
1032         unsafe {
1033             self.write_le_u32(cast::transmute(f))
1034         }
1035     }
1036
1037     /// Write a u8 (1 byte).
1038     fn write_u8(&mut self, n: u8) -> IoResult<()> {
1039         self.write([n])
1040     }
1041
1042     /// Write an i8 (1 byte).
1043     fn write_i8(&mut self, n: i8) -> IoResult<()> {
1044         self.write([n as u8])
1045     }
1046
1047     /// Creates a wrapper around a mutable reference to the writer.
1048     ///
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 }
1053     }
1054 }
1055
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() }
1059 }
1060
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() }
1064 }
1065
1066 /// A `RefWriter` is a struct implementing `Writer` which contains a reference
1067 /// to another writer. This is often useful when composing streams.
1068 ///
1069 /// # Example
1070 ///
1071 /// ```
1072 /// # fn main() {}
1073 /// # fn process_input<R: Reader>(r: R) {}
1074 /// # fn foo () {
1075 /// use std::io::util::TeeReader;
1076 /// use std::io::{stdin, MemWriter};
1077 ///
1078 /// let mut output = MemWriter::new();
1079 ///
1080 /// {
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);
1085 /// }
1086 ///
1087 /// println!("input processed: {}", output.unwrap());
1088 /// # }
1089 /// ```
1090 pub struct RefWriter<'a, W> {
1091     /// The underlying writer which this is referencing
1092     inner: &'a mut W
1093 }
1094
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() }
1098 }
1099
1100
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 { }
1104
1105 impl<T: Reader + Writer> Stream for T {}
1106
1107 /// An iterator that reads a line on each iteration,
1108 /// until `.read_line()` encounters `EndOfFile`.
1109 ///
1110 /// # Notes about the Iteration Protocol
1111 ///
1112 /// The `Lines` may yield `None` and thus terminate
1113 /// an iteration, but continue to yield elements if iteration
1114 /// is attempted again.
1115 ///
1116 /// # Error
1117 ///
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> {
1121     buffer: &'r mut T,
1122 }
1123
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))
1130         }
1131     }
1132 }
1133
1134 /// An iterator that reads a utf8-encoded character on each iteration,
1135 /// until `.read_char()` encounters `EndOfFile`.
1136 ///
1137 /// # Notes about the Iteration Protocol
1138 ///
1139 /// The `Chars` may yield `None` and thus terminate
1140 /// an iteration, but continue to yield elements if iteration
1141 /// is attempted again.
1142 ///
1143 /// # Error
1144 ///
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> {
1148     buffer: &'r mut T
1149 }
1150
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))
1157         }
1158     }
1159 }
1160
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.
1169     ///
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
1172     /// returned twice.
1173     ///
1174     /// # Error
1175     ///
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]>;
1180
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);
1184
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.
1188     ///
1189     /// # Example
1190     ///
1191     /// ```rust
1192     /// use std::io;
1193     ///
1194     /// let mut reader = io::stdin();
1195     /// let input = reader.read_line().ok().unwrap_or("nothing".to_owned());
1196     /// ```
1197     ///
1198     /// # Error
1199     ///
1200     /// This function has the same error semantics as `read_until`:
1201     ///
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
1206     ///   value
1207     ///
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)),
1215             }
1216         )
1217     }
1218
1219     /// Create an iterator that reads a line on each iteration until EOF.
1220     ///
1221     /// # Error
1222     ///
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 }
1227     }
1228
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.
1232     ///
1233     /// # Error
1234     ///
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.
1239     ///
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
1243     /// delimiter.
1244     fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> {
1245         let mut res = Vec::new();
1246
1247         let mut used;
1248         loop {
1249             {
1250                 let available = match self.fill_buf() {
1251                     Ok(n) => n,
1252                     Err(ref e) if res.len() > 0 && e.kind == EndOfFile => {
1253                         used = 0;
1254                         break
1255                     }
1256                     Err(e) => return Err(e)
1257                 };
1258                 match available.iter().position(|&b| b == byte) {
1259                     Some(i) => {
1260                         res.push_all(available.slice_to(i + 1));
1261                         used = i + 1;
1262                         break
1263                     }
1264                     None => {
1265                         res.push_all(available);
1266                         used = available.len();
1267                     }
1268                 }
1269             }
1270             self.consume(used);
1271         }
1272         self.consume(used);
1273         Ok(res)
1274     }
1275
1276     /// Reads the next utf8-encoded character from the underlying stream.
1277     ///
1278     /// # Error
1279     ///
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];
1289         {
1290             let mut start = 1;
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)),
1296                 }
1297             }
1298         }
1299         match str::from_utf8(buf.slice_to(width)) {
1300             Some(s) => Ok(s.char_at(0)),
1301             None => Err(standard_error(InvalidInput))
1302         }
1303     }
1304
1305     /// Create an iterator that reads a utf8-encoded character on each iteration
1306     /// until EOF.
1307     ///
1308     /// # Error
1309     ///
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 }
1314     }
1315 }
1316
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
1321     SeekSet,
1322     /// Seek from the end of the stream
1323     SeekEnd,
1324     /// Seek from the current position
1325     SeekCur,
1326 }
1327
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.
1331 pub trait Seek {
1332     /// Return position of file cursor in the stream
1333     fn tell(&self) -> IoResult<u64>;
1334
1335     /// Seek to an offset in a stream
1336     ///
1337     /// A successful seek clears the EOF indicator. Seeking beyond EOF is
1338     /// allowed, but seeking before position 0 is not allowed.
1339     ///
1340     /// # Errors
1341     ///
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<()>;
1347 }
1348
1349 /// A listener is a value that can consume itself to start listening for
1350 /// connections.
1351 ///
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
1355     ///
1356     /// # Error
1357     ///
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>;
1361 }
1362
1363 /// An acceptor is a value that presents incoming connections
1364 pub trait Acceptor<T> {
1365     /// Wait for and accept an incoming connection
1366     ///
1367     /// # Error
1368     ///
1369     /// Returns `Err` if an I/O error is encountered.
1370     fn accept(&mut self) -> IoResult<T>;
1371
1372     /// Create an iterator over incoming connection attempts.
1373     ///
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 }
1377     }
1378 }
1379
1380 /// An infinite iterator over incoming connection attempts.
1381 /// Calling `next` will block the task until a connection is attempted.
1382 ///
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> {
1388     inc: &'a mut A,
1389 }
1390
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())
1394     }
1395 }
1396
1397 /// Creates a standard error for a commonly used flavor of error. The `detail`
1398 /// field of the returned error will always be `None`.
1399 ///
1400 /// # Example
1401 ///
1402 /// ```
1403 /// use std::io;
1404 ///
1405 /// let eof = io::standard_error(io::EndOfFile);
1406 /// let einval = io::standard_error(io::InvalidInput);
1407 /// ```
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"
1428     };
1429     IoError {
1430         kind: kind,
1431         desc: desc,
1432         detail: None,
1433     }
1434 }
1435
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.
1439 pub enum FileMode {
1440     /// Opens a file positioned at the beginning.
1441     Open,
1442     /// Opens a file positioned at EOF.
1443     Append,
1444     /// Opens a file, truncating it if it already exists.
1445     Truncate,
1446 }
1447
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
1452     Read,
1453     /// Write-only access, requests to read will result in an error
1454     Write,
1455     /// Read-write access, no requests are denied by default
1456     ReadWrite,
1457 }
1458
1459 /// Different kinds of files which can be identified by a call to stat
1460 #[deriving(Eq, Show, Hash)]
1461 pub enum FileType {
1462     /// This is a normal file, corresponding to `S_IFREG`
1463     TypeFile,
1464
1465     /// This file is a directory, corresponding to `S_IFDIR`
1466     TypeDirectory,
1467
1468     /// This file is a named pipe, corresponding to `S_IFIFO`
1469     TypeNamedPipe,
1470
1471     /// This file is a block device, corresponding to `S_IFBLK`
1472     TypeBlockSpecial,
1473
1474     /// This file is a symbolic link to another file, corresponding to `S_IFLNK`
1475     TypeSymlink,
1476
1477     /// The type of this file is not recognized as one of the other categories
1478     TypeUnknown,
1479 }
1480
1481 /// A structure used to describe metadata information about a file. This
1482 /// structure is created through the `stat` method on a `Path`.
1483 ///
1484 /// # Example
1485 ///
1486 /// ```
1487 /// # fn main() {}
1488 /// # fn foo() {
1489 /// let info = match Path::new("foo.txt").stat() {
1490 ///     Ok(stat) => stat,
1491 ///     Err(e) => fail!("couldn't read foo.txt: {}", e),
1492 /// };
1493 ///
1494 /// println!("path: {}", info.path.display());
1495 /// println!("byte size: {}", info.size);
1496 /// # }
1497 /// ```
1498 #[deriving(Hash)]
1499 pub struct FileStat {
1500     /// The path that this stat structure is describing
1501     pub path: Path,
1502     /// The size of the file, in bytes
1503     pub size: u64,
1504     /// The kind of file this path points to (directory, file, pipe, etc.)
1505     pub kind: FileType,
1506     /// The file permissions currently on the file
1507     pub perm: FilePermission,
1508
1509     // FIXME(#10301): These time fields are pretty useless without an actual
1510     //                time representation, what are the milliseconds relative
1511     //                to?
1512
1513     /// The time that the file was created at, in platform-dependent
1514     /// milliseconds
1515     pub created: u64,
1516     /// The time that this file was last modified, in platform-dependent
1517     /// milliseconds
1518     pub modified: u64,
1519     /// The time that this file was last accessed, in platform-dependent
1520     /// milliseconds
1521     pub accessed: u64,
1522
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
1526     /// platforms.
1527     ///
1528     /// Usage of this field is discouraged, but if access is desired then the
1529     /// fields are located here.
1530     #[unstable]
1531     pub unstable: UnstableFileStat,
1532 }
1533
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.
1538 #[unstable]
1539 #[deriving(Hash)]
1540 pub struct UnstableFileStat {
1541     /// The ID of the device containing the file.
1542     pub device: u64,
1543     /// The file serial number.
1544     pub inode: u64,
1545     /// The device ID.
1546     pub rdev: u64,
1547     /// The number of hard links to this file.
1548     pub nlink: u64,
1549     /// The user ID of the file.
1550     pub uid: u64,
1551     /// The group ID of the file.
1552     pub gid: u64,
1553     /// The optimal block size for I/O.
1554     pub blksize: u64,
1555     /// The blocks allocated for this file.
1556     pub blocks: u64,
1557     /// User-defined flags for the file.
1558     pub flags: u64,
1559     /// The file generation number.
1560     pub gen: u64,
1561 }
1562
1563 bitflags!(
1564     #[doc="A set of permissions for a file or directory is represented
1565 by a set of flags which are or'd together."]
1566     #[deriving(Hash)]
1567     #[deriving(Show)]
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,
1578
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,
1582
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,
1586
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,
1591
1592         #[doc="Permissions for user owned executables, equivalent to 0755
1593 on unix-like systems."]
1594         static UserExec = UserDir.bits,
1595
1596         #[doc="All possible permissions enabled."]
1597         static AllPermissions = UserRWX.bits | GroupRWX.bits | OtherRWX.bits
1598     }
1599 )