From 5878b1b76bee774b6d2f2f368ce216d9084d731c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 30 Jan 2014 14:37:38 -0800 Subject: native: Remove io_error usage --- src/libnative/io/file.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/libnative') diff --git a/src/libnative/io/file.rs b/src/libnative/io/file.rs index acab7ce3a91..72379275643 100644 --- a/src/libnative/io/file.rs +++ b/src/libnative/io/file.rs @@ -111,17 +111,14 @@ impl FileDesc { } impl io::Reader for FileDesc { - fn read(&mut self, buf: &mut [u8]) -> Option { - match self.inner_read(buf) { Ok(n) => Some(n), Err(..) => None } + fn read(&mut self, buf: &mut [u8]) -> io::IoResult { + self.inner_read(buf) } } impl io::Writer for FileDesc { - fn write(&mut self, buf: &[u8]) { - match self.inner_write(buf) { - Ok(()) => {} - Err(e) => { io::io_error::cond.raise(e); } - } + fn write(&mut self, buf: &[u8]) -> io::IoResult<()> { + self.inner_write(buf) } } -- cgit 1.4.1-3-g733a5 From ae581a010363b26ef6bae60145ebd17343a343b0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 30 Jan 2014 14:28:28 -0800 Subject: native: Require all results are used and fix fallout --- src/libnative/bookkeeping.rs | 2 +- src/libnative/io/file.rs | 10 +++++----- src/libnative/io/net.rs | 4 ++-- src/libnative/io/process.rs | 20 ++++++++++---------- src/libnative/io/timer_other.rs | 5 +++-- src/libnative/lib.rs | 4 +++- src/libnative/task.rs | 3 ++- 7 files changed, 26 insertions(+), 22 deletions(-) (limited to 'src/libnative') diff --git a/src/libnative/bookkeeping.rs b/src/libnative/bookkeeping.rs index b07e4271ee4..868586b3691 100644 --- a/src/libnative/bookkeeping.rs +++ b/src/libnative/bookkeeping.rs @@ -23,7 +23,7 @@ static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT; static mut TASK_LOCK: Mutex = MUTEX_INIT; pub fn increment() { - unsafe { TASK_COUNT.fetch_add(1, atomics::SeqCst); } + let _ = unsafe { TASK_COUNT.fetch_add(1, atomics::SeqCst) }; } pub fn decrement() { diff --git a/src/libnative/io/file.rs b/src/libnative/io/file.rs index 72379275643..e6bead60d1b 100644 --- a/src/libnative/io/file.rs +++ b/src/libnative/io/file.rs @@ -422,7 +422,7 @@ impl rtio::RtioFileStream for CFile { impl Drop for CFile { fn drop(&mut self) { - unsafe { libc::fclose(self.file); } + unsafe { let _ = libc::fclose(self.file); } } } @@ -512,7 +512,7 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> { paths.push(Path::new(cstr)); entry_ptr = readdir(dir_ptr); } - closedir(dir_ptr); + assert_eq!(closedir(dir_ptr), 0); Ok(paths) } else { Err(super::last_error()) @@ -932,7 +932,7 @@ mod tests { let mut reader = FileDesc::new(input, true); let mut writer = FileDesc::new(out, true); - writer.inner_write(bytes!("test")); + writer.inner_write(bytes!("test")).unwrap(); let mut buf = [0u8, ..4]; match reader.inner_read(buf) { Ok(4) => { @@ -957,9 +957,9 @@ mod tests { assert!(!f.is_null()); let mut file = CFile::new(f); - file.write(bytes!("test")); + file.write(bytes!("test")).unwrap(); let mut buf = [0u8, ..4]; - file.seek(0, io::SeekSet); + let _ = file.seek(0, io::SeekSet).unwrap(); match file.read(buf) { Ok(4) => { assert_eq!(buf[0], 't' as u8); diff --git a/src/libnative/io/net.rs b/src/libnative/io/net.rs index 2f4bec22755..ac68b1523d7 100644 --- a/src/libnative/io/net.rs +++ b/src/libnative/io/net.rs @@ -112,8 +112,8 @@ fn setsockopt(fd: sock_t, opt: libc::c_int, val: libc::c_int, } } -#[cfg(windows)] unsafe fn close(sock: sock_t) { libc::closesocket(sock); } -#[cfg(unix)] unsafe fn close(sock: sock_t) { libc::close(sock); } +#[cfg(windows)] unsafe fn close(sock: sock_t) { let _ = libc::closesocket(sock); } +#[cfg(unix)] unsafe fn close(sock: sock_t) { let _ = libc::close(sock); } fn sockname(fd: sock_t, f: extern "system" unsafe fn(sock_t, *mut libc::sockaddr, diff --git a/src/libnative/io/process.rs b/src/libnative/io/process.rs index 13dd4298777..c2522d551b6 100644 --- a/src/libnative/io/process.rs +++ b/src/libnative/io/process.rs @@ -102,9 +102,9 @@ impl Process { cwd.as_ref(), in_fd, out_fd, err_fd); unsafe { - for pipe in in_pipe.iter() { libc::close(pipe.input); } - for pipe in out_pipe.iter() { libc::close(pipe.out); } - for pipe in err_pipe.iter() { libc::close(pipe.out); } + for pipe in in_pipe.iter() { let _ = libc::close(pipe.input); } + for pipe in out_pipe.iter() { let _ = libc::close(pipe.out); } + for pipe in err_pipe.iter() { let _ = libc::close(pipe.out); } } match res { @@ -163,8 +163,8 @@ impl rtio::RtioProcess for Process { #[cfg(not(windows))] unsafe fn killpid(pid: pid_t, signal: int) -> Result<(), io::IoError> { - libc::funcs::posix88::signal::kill(pid, signal as c_int); - Ok(()) + let r = libc::funcs::posix88::signal::kill(pid, signal as c_int); + super::mkerr_libc(r) } } } @@ -445,24 +445,24 @@ fn spawn_process_os(prog: &str, args: &[~str], rustrt::rust_unset_sigprocmask(); if in_fd == -1 { - libc::close(libc::STDIN_FILENO); + let _ = libc::close(libc::STDIN_FILENO); } else if retry(|| dup2(in_fd, 0)) == -1 { fail!("failure in dup2(in_fd, 0): {}", os::last_os_error()); } if out_fd == -1 { - libc::close(libc::STDOUT_FILENO); + let _ = libc::close(libc::STDOUT_FILENO); } else if retry(|| dup2(out_fd, 1)) == -1 { fail!("failure in dup2(out_fd, 1): {}", os::last_os_error()); } if err_fd == -1 { - libc::close(libc::STDERR_FILENO); + let _ = libc::close(libc::STDERR_FILENO); } else if retry(|| dup2(err_fd, 2)) == -1 { fail!("failure in dup3(err_fd, 2): {}", os::last_os_error()); } // close all other fds for fd in range(3, getdtablesize()).rev() { if fd != output.fd() { - close(fd as c_int); + let _ = close(fd as c_int); } } @@ -478,7 +478,7 @@ fn spawn_process_os(prog: &str, args: &[~str], } }); with_argv(prog, args, |argv| { - execvp(*argv, argv); + let _ = execvp(*argv, argv); let errno = os::errno(); let bytes = [ (errno << 24) as u8, diff --git a/src/libnative/io/timer_other.rs b/src/libnative/io/timer_other.rs index bc005f2fe8d..cda239329dc 100644 --- a/src/libnative/io/timer_other.rs +++ b/src/libnative/io/timer_other.rs @@ -187,7 +187,7 @@ fn helper(input: libc::c_int, messages: Port) { // drain the file descriptor let mut buf = [0]; - fd.inner_read(buf).unwrap(); + assert_eq!(fd.inner_read(buf).unwrap(), 1); } -1 if os::errno() == libc::EINTR as int => {} @@ -216,7 +216,8 @@ impl Timer { } pub fn sleep(ms: u64) { - unsafe { libc::usleep((ms * 1000) as libc::c_uint); } + // FIXME: this can fail because of EINTR, what do do? + let _ = unsafe { libc::usleep((ms * 1000) as libc::c_uint) }; } fn inner(&mut self) -> ~Inner { diff --git a/src/libnative/lib.rs b/src/libnative/lib.rs index f69ad8fc1aa..1e4317af397 100644 --- a/src/libnative/lib.rs +++ b/src/libnative/lib.rs @@ -21,6 +21,7 @@ #[doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk.png", html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://static.rust-lang.org/doc/master")]; +#[deny(unused_result, unused_must_use)]; // NB this crate explicitly does *not* allow glob imports, please seriously // consider whether they're needed before adding that feature here (the @@ -61,9 +62,10 @@ pub fn start(argc: int, argv: **u8, main: proc()) -> int { rt::init(argc, argv); let mut exit_code = None; let mut main = Some(main); - task::new((my_stack_bottom, my_stack_top)).run(|| { + let t = task::new((my_stack_bottom, my_stack_top)).run(|| { exit_code = Some(run(main.take_unwrap())); }); + drop(t); unsafe { rt::cleanup(); } // If the exit code wasn't set, then the task block must have failed. return exit_code.unwrap_or(rt::DEFAULT_ERROR_CODE); diff --git a/src/libnative/task.rs b/src/libnative/task.rs index 37425179701..0def5cb4053 100644 --- a/src/libnative/task.rs +++ b/src/libnative/task.rs @@ -103,7 +103,8 @@ pub fn spawn_opts(opts: TaskOpts, f: proc()) { let mut f = Some(f); let mut task = task; task.put_runtime(ops as ~rt::Runtime); - task.run(|| { f.take_unwrap()() }); + let t = task.run(|| { f.take_unwrap()() }); + drop(t); bookkeeping::decrement(); }) } -- cgit 1.4.1-3-g733a5 From f9a32cdabc1680b89bd7b579dc1e3f8f18c28257 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 30 Jan 2014 16:55:20 -0800 Subject: std: Fixing all documentation * Stop referencing io_error * Start changing "Failure" sections to "Error" sections * Update all doc examples to work. --- src/libnative/io/timer_timerfd.rs | 7 +- src/libstd/fmt/mod.rs | 22 ++- src/libstd/io/buffered.rs | 13 +- src/libstd/io/fs.rs | 164 +++++++++------------- src/libstd/io/mem.rs | 12 +- src/libstd/io/mod.rs | 287 ++++++++++++++++++-------------------- src/libstd/io/net/addrinfo.rs | 8 -- src/libstd/io/net/unix.rs | 10 -- src/libstd/io/pipe.rs | 16 +-- src/libstd/io/process.rs | 2 +- src/libstd/io/result.rs | 6 +- src/libstd/io/signal.rs | 15 +- src/libstd/io/stdio.rs | 15 +- src/libstd/os.rs | 6 +- src/libsyntax/print/pprust.rs | 16 +-- 15 files changed, 274 insertions(+), 325 deletions(-) (limited to 'src/libnative') diff --git a/src/libnative/io/timer_timerfd.rs b/src/libnative/io/timer_timerfd.rs index ca20314997e..7c22e90bbff 100644 --- a/src/libnative/io/timer_timerfd.rs +++ b/src/libnative/io/timer_timerfd.rs @@ -96,7 +96,7 @@ fn helper(input: libc::c_int, messages: Port) { if fd == input { let mut buf = [0, ..1]; // drain the input file descriptor of its input - FileDesc::new(fd, false).inner_read(buf).unwrap(); + let _ = FileDesc::new(fd, false).inner_read(buf).unwrap(); incoming = true; } else { let mut bits = [0, ..8]; @@ -104,7 +104,7 @@ fn helper(input: libc::c_int, messages: Port) { // // FIXME: should this perform a send() this number of // times? - FileDesc::new(fd, false).inner_read(bits).unwrap(); + let _ = FileDesc::new(fd, false).inner_read(bits).unwrap(); let remove = { match map.find(&fd).expect("fd unregistered") { &(ref c, oneshot) => !c.try_send(()) || oneshot @@ -166,7 +166,8 @@ impl Timer { } pub fn sleep(ms: u64) { - unsafe { libc::usleep((ms * 1000) as libc::c_uint); } + // FIXME: this can fail because of EINTR, what do do? + let _ = unsafe { libc::usleep((ms * 1000) as libc::c_uint) }; } fn remove(&mut self) { diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index e0944dea9b2..0bc567b270a 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -163,9 +163,10 @@ method of the signature: ```rust # use std; +# mod fmt { pub type Result = (); } # struct T; # trait SomeName { -fn fmt(value: &T, f: &mut std::fmt::Formatter); +fn fmt(value: &T, f: &mut std::fmt::Formatter) -> fmt::Result; # } ``` @@ -174,7 +175,14 @@ emit output into the `f.buf` stream. It is up to each format trait implementation to correctly adhere to the requested formatting parameters. The values of these parameters will be listed in the fields of the `Formatter` struct. In order to help with this, the `Formatter` struct also provides some -helper methods. An example of implementing the formatting traits would look +helper methods. + +Additionally, the return value of this function is `fmt::Result` which is a +typedef to `Result<(), IoError>` (also known as `IoError<()>`). Formatting +implementations should ensure that they return errors from `write!` correctly +(propagating errors upward). + +An example of implementing the formatting traits would look like: ```rust @@ -187,7 +195,7 @@ struct Vector2D { } impl fmt::Show for Vector2D { - fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) { + fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) -> fmt::Result { // The `f.buf` value is of the type `&mut io::Writer`, which is what th // write! macro is expecting. Note that this formatting ignores the // various flags provided to format strings. @@ -198,7 +206,7 @@ impl fmt::Show for Vector2D { // Different traits allow different forms of output of a type. The meaning of // this format is to print the magnitude of a vector. impl fmt::Binary for Vector2D { - fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) { + fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) -> fmt::Result { let magnitude = (obj.x * obj.x + obj.y * obj.y) as f64; let magnitude = magnitude.sqrt(); @@ -207,7 +215,7 @@ impl fmt::Binary for Vector2D { // for details, and the function `pad` can be used to pad strings. let decimals = f.precision.unwrap_or(3); let string = f64::to_str_exact(magnitude, decimals); - f.pad_integral(string.as_bytes(), "", true); + f.pad_integral(string.as_bytes(), "", true) } } @@ -242,6 +250,7 @@ strings and instead directly write the output. Under the hood, this function is actually invoking the `write` function defined in this module. Example usage is: ```rust +# #[allow(unused_must_use)]; use std::io; let mut w = io::MemWriter::new(); @@ -655,11 +664,12 @@ uniform_fn_call_workaround! { /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::fmt; /// use std::io; /// /// let w = &mut io::stdout() as &mut io::Writer; -/// format_args!(|args| { fmt::write(w, args) }, "Hello, {}!", "world"); +/// format_args!(|args| { fmt::write(w, args); }, "Hello, {}!", "world"); /// ``` pub fn write(output: &mut io::Writer, args: &Arguments) -> Result { unsafe { write_unsafe(output, args.fmt, args.args) } diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index e3bc97b6f28..256f9d325f3 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -31,14 +31,13 @@ use vec; /// ```rust /// use std::io::{BufferedReader, File}; /// -/// # let _g = ::std::io::ignore_io_error(); /// let file = File::open(&Path::new("message.txt")); /// let mut reader = BufferedReader::new(file); /// /// let mut buf = [0, ..100]; /// match reader.read(buf) { -/// Some(nread) => println!("Read {} bytes", nread), -/// None => println!("At the end of the file!") +/// Ok(nread) => println!("Read {} bytes", nread), +/// Err(e) => println!("error reading: {}", e) /// } /// ``` pub struct BufferedReader { @@ -121,9 +120,9 @@ impl Reader for BufferedReader { /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::io::{BufferedWriter, File}; /// -/// # let _g = ::std::io::ignore_io_error(); /// let file = File::open(&Path::new("message.txt")); /// let mut writer = BufferedWriter::new(file); /// @@ -268,9 +267,9 @@ impl Reader for InternalBufferedWriter { /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::io::{BufferedStream, File}; /// -/// # let _g = ::std::io::ignore_io_error(); /// let file = File::open(&Path::new("message.txt")); /// let mut stream = BufferedStream::new(file); /// @@ -279,8 +278,8 @@ impl Reader for InternalBufferedWriter { /// /// let mut buf = [0, ..100]; /// match stream.read(buf) { -/// Some(nread) => println!("Read {} bytes", nread), -/// None => println!("At the end of the stream!") +/// Ok(nread) => println!("Read {} bytes", nread), +/// Err(e) => println!("error reading: {}", e) /// } /// ``` pub struct BufferedStream { diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index 1b669539288..ef1b1a56ec0 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -14,11 +14,11 @@ This module provides a set of functions and traits for working with regular files & directories on a filesystem. At the top-level of the module are a set of freestanding functions, associated -with various filesystem operations. They all operate on a `Path` object. +with various filesystem operations. They all operate on `Path` objects. All operations in this module, including those as part of `File` et al -block the task during execution. Most will raise `std::io::io_error` -conditions in the event of failure. +block the task during execution. In the event of failure, all functions/methods +will return an `IoResult` type with an `Err` value. Also included in this module is an implementation block on the `Path` object defined in `std::path::Path`. The impl adds useful methods about inspecting the @@ -42,7 +42,7 @@ file.write(bytes!("foobar")); let mut file = File::open(&path); file.read_to_end(); -println!("{}", path.stat().size); +println!("{}", path.stat().unwrap().size); # drop(file); fs::unlink(&path); ``` @@ -68,11 +68,12 @@ use vec::{OwnedVector, ImmutableVector}; /// Can be constructed via `File::open()`, `File::create()`, and /// `File::open_mode()`. /// -/// # Errors +/// # Error /// -/// This type will raise an io_error condition if operations are attempted against -/// it for which its underlying file descriptor was not configured at creation -/// time, via the `FileAccess` parameter to `File::open_mode()`. +/// This type will return errors as an `IoResult` if operations are +/// attempted against it for which its underlying file descriptor was not +/// configured at creation time, via the `FileAccess` parameter to +/// `File::open_mode()`. pub struct File { priv fd: ~RtioFileStream, priv path: Path, @@ -85,7 +86,7 @@ impl File { /// /// # Example /// - /// ```rust + /// ```rust,should_fail /// use std::io::{File, Open, ReadWrite}; /// /// let p = Path::new("/some/file/path.txt"); @@ -107,12 +108,12 @@ impl File { /// /// Note that, with this function, a `File` is returned regardless of the /// access-limitations indicated by `FileAccess` (e.g. calling `write` on a - /// `File` opened as `Read` will raise an `io_error` condition at runtime). + /// `File` opened as `Read` will return an error at runtime). /// - /// # Errors + /// # Error /// - /// This function will raise an `io_error` condition under a number of - /// different circumstances, to include but not limited to: + /// This function will return an error under a number of different + /// circumstances, to include but not limited to: /// /// * Opening a file that does not exist with `Read` access. /// * Attempting to open a file with a `FileAccess` that the user lacks @@ -164,7 +165,7 @@ impl File { /// let mut f = File::create(&Path::new("foo.txt")); /// f.write(bytes!("This is a sample file")); /// # drop(f); - /// # ::std::io::fs::unlnk(&Path::new("foo.txt")); + /// # ::std::io::fs::unlink(&Path::new("foo.txt")); /// ``` pub fn create(path: &Path) -> IoResult { File::open_mode(path, Truncate, Write) @@ -178,10 +179,6 @@ impl File { /// Synchronizes all modifications to this file to its permanent storage /// device. This will flush any internal buffers necessary to perform this /// operation. - /// - /// # Errors - /// - /// This function will raise on the `io_error` condition on failure. pub fn fsync(&mut self) -> IoResult<()> { self.fd.fsync() } @@ -190,10 +187,6 @@ impl File { /// file metadata to the filesystem. This is intended for use case which /// must synchronize content, but don't need the metadata on disk. The goal /// of this method is to reduce disk operations. - /// - /// # Errors - /// - /// This function will raise on the `io_error` condition on failure. pub fn datasync(&mut self) -> IoResult<()> { self.fd.datasync() } @@ -206,10 +199,6 @@ impl File { /// be shrunk. If it is greater than the current file's size, then the file /// will be extended to `size` and have all of the intermediate data filled /// in with 0s. - /// - /// # Errors - /// - /// On error, this function will raise on the `io_error` condition. pub fn truncate(&mut self, size: i64) -> IoResult<()> { self.fd.truncate(size) } @@ -239,11 +228,11 @@ impl File { /// guaranteed that a file is immediately deleted (e.g. depending on /// platform, other open file descriptors may prevent immediate removal) /// -/// # Errors +/// # Error /// -/// This function will raise an `io_error` condition if the path points to a -/// directory, the user lacks permissions to remove the file, or if some -/// other filesystem-level error occurs. +/// This function will return an error if the path points to a directory, the +/// user lacks permissions to remove the file, or if some other filesystem-level +/// error occurs. pub fn unlink(path: &Path) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_unlink(&path.to_c_str())) } @@ -259,7 +248,6 @@ pub fn unlink(path: &Path) -> IoResult<()> { /// # Example /// /// ```rust -/// use std::io; /// use std::io::fs; /// /// let p = Path::new("/some/file/path.txt"); @@ -269,11 +257,11 @@ pub fn unlink(path: &Path) -> IoResult<()> { /// } /// ``` /// -/// # Errors +/// # Error /// -/// This call will raise an `io_error` condition if the user lacks the -/// requisite permissions to perform a `stat` call on the given path or if -/// there is no entry in the filesystem at the provided path. +/// This call will return an error if the user lacks the requisite permissions +/// to perform a `stat` call on the given path or if there is no entry in the +/// filesystem at the provided path. pub fn stat(path: &Path) -> IoResult { LocalIo::maybe_raise(|io| { io.fs_stat(&path.to_c_str()) @@ -285,7 +273,7 @@ pub fn stat(path: &Path) -> IoResult { /// information about the symlink file instead of the file that it points /// to. /// -/// # Errors +/// # Error /// /// See `stat` pub fn lstat(path: &Path) -> IoResult { @@ -305,11 +293,11 @@ pub fn lstat(path: &Path) -> IoResult { /// fs::rename(&Path::new("foo"), &Path::new("bar")); /// ``` /// -/// # Errors +/// # Error /// -/// Will raise an `io_error` condition if the provided `path` doesn't exist, -/// the process lacks permissions to view the contents, or if some other -/// intermittent I/O error occurs. +/// Will return an error if the provided `path` doesn't exist, the process lacks +/// permissions to view the contents, or if some other intermittent I/O error +/// occurs. pub fn rename(from: &Path, to: &Path) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_rename(&from.to_c_str(), &to.to_c_str())) } @@ -329,10 +317,10 @@ pub fn rename(from: &Path, to: &Path) -> IoResult<()> { /// fs::copy(&Path::new("foo.txt"), &Path::new("bar.txt")); /// ``` /// -/// # Errors +/// # Error /// -/// Will raise an `io_error` condition is the following situations, but is -/// not limited to just these cases: +/// Will return an error in the following situations, but is not limited to +/// just these cases: /// /// * The `from` path is not a file /// * The `from` file does not exist @@ -373,7 +361,7 @@ pub fn copy(from: &Path, to: &Path) -> IoResult<()> { /// # Example /// /// ```rust -/// # #[allow(unused_must_use)] +/// # #[allow(unused_must_use)]; /// use std::io; /// use std::io::fs; /// @@ -383,20 +371,16 @@ pub fn copy(from: &Path, to: &Path) -> IoResult<()> { /// fs::chmod(&Path::new("file.exe"), io::UserExec); /// ``` /// -/// # Errors +/// # Error /// -/// If this function encounters an I/O error, it will raise on the `io_error` -/// condition. Some possible error situations are not having the permission to +/// If this function encounters an I/O error, it will return an `Err` value. +/// Some possible error situations are not having the permission to /// change the attributes of a file or the file not existing. pub fn chmod(path: &Path, mode: io::FilePermission) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_chmod(&path.to_c_str(), mode)) } /// Change the user and group owners of a file at the specified path. -/// -/// # Errors -/// -/// This function will raise on the `io_error` condition on failure. pub fn chown(path: &Path, uid: int, gid: int) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_chown(&path.to_c_str(), uid, gid)) } @@ -404,31 +388,22 @@ pub fn chown(path: &Path, uid: int, gid: int) -> IoResult<()> { /// Creates a new hard link on the filesystem. The `dst` path will be a /// link pointing to the `src` path. Note that systems often require these /// two paths to both be located on the same filesystem. -/// -/// # Errors -/// -/// This function will raise on the `io_error` condition on failure. pub fn link(src: &Path, dst: &Path) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_link(&src.to_c_str(), &dst.to_c_str())) } /// Creates a new symbolic link on the filesystem. The `dst` path will be a /// symlink pointing to the `src` path. -/// -/// # Errors -/// -/// This function will raise on the `io_error` condition on failure. pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_symlink(&src.to_c_str(), &dst.to_c_str())) } /// Reads a symlink, returning the file that the symlink points to. /// -/// # Errors +/// # Error /// -/// This function will raise on the `io_error` condition on failure. Failure -/// conditions include reading a file that does not exist or reading a file -/// which is not a symlink. +/// This function will return an error on failure. Failure conditions include +/// reading a file that does not exist or reading a file which is not a symlink. pub fn readlink(path: &Path) -> IoResult { LocalIo::maybe_raise(|io| io.fs_readlink(&path.to_c_str())) } @@ -446,11 +421,10 @@ pub fn readlink(path: &Path) -> IoResult { /// fs::mkdir(&p, io::UserRWX); /// ``` /// -/// # Errors +/// # Error /// -/// This call will raise an `io_error` condition if the user lacks permissions -/// to make a new directory at the provided path, or if the directory already -/// exists. +/// This call will return an error if the user lacks permissions to make a new +/// directory at the provided path, or if the directory already exists. pub fn mkdir(path: &Path, mode: FilePermission) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_mkdir(&path.to_c_str(), mode)) } @@ -467,11 +441,10 @@ pub fn mkdir(path: &Path, mode: FilePermission) -> IoResult<()> { /// fs::rmdir(&p); /// ``` /// -/// # Errors +/// # Error /// -/// This call will raise an `io_error` condition if the user lacks permissions -/// to remove the directory at the provided path, or if the directory isn't -/// empty. +/// This call will return an error if the user lacks permissions to remove the +/// directory at the provided path, or if the directory isn't empty. pub fn rmdir(path: &Path) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_rmdir(&path.to_c_str())) } @@ -481,26 +454,32 @@ pub fn rmdir(path: &Path) -> IoResult<()> { /// # Example /// /// ```rust +/// use std::io; /// use std::io::fs; /// /// // one possible implementation of fs::walk_dir only visiting files -/// fn visit_dirs(dir: &Path, cb: |&Path|) { +/// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> { /// if dir.is_dir() { -/// let contents = fs::readdir(dir).unwrap(); +/// let contents = if_ok!(fs::readdir(dir)); /// for entry in contents.iter() { -/// if entry.is_dir() { visit_dirs(entry, cb); } -/// else { cb(entry); } +/// if entry.is_dir() { +/// if_ok!(visit_dirs(entry, |p| cb(p))); +/// } else { +/// cb(entry); +/// } /// } +/// Ok(()) +/// } else { +/// Err(io::standard_error(io::InvalidInput)) /// } -/// else { fail!("nope"); } /// } /// ``` /// -/// # Errors +/// # Error /// -/// Will raise an `io_error` condition if the provided `from` doesn't exist, -/// the process lacks permissions to view the contents or if the `path` points -/// at a non-directory file +/// Will return an error if the provided `from` doesn't exist, the process lacks +/// permissions to view the contents or if the `path` points at a non-directory +/// file pub fn readdir(path: &Path) -> IoResult<~[Path]> { LocalIo::maybe_raise(|io| { io.fs_readdir(&path.to_c_str(), 0) @@ -539,11 +518,10 @@ impl Iterator for Directories { /// Recursively create a directory and all of its parent components if they /// are missing. /// -/// # Errors +/// # Error /// -/// This function will raise on the `io_error` condition if an error -/// happens, see `fs::mkdir` for more information about error conditions -/// and performance. +/// This function will return an `Err` value if an error happens, see +/// `fs::mkdir` for more information about error conditions and performance. pub fn mkdir_recursive(path: &Path, mode: FilePermission) -> IoResult<()> { // tjc: if directory exists but with different permissions, // should we return false? @@ -559,11 +537,10 @@ pub fn mkdir_recursive(path: &Path, mode: FilePermission) -> IoResult<()> { /// Removes a directory at this path, after removing all its contents. Use /// carefully! /// -/// # Errors +/// # Error /// -/// This function will raise on the `io_error` condition if an error -/// happens. See `file::unlink` and `fs::readdir` for possible error -/// conditions. +/// This function will return an `Err` value if an error happens. See +/// `file::unlink` and `fs::readdir` for possible error conditions. pub fn rmdir_recursive(path: &Path) -> IoResult<()> { let children = if_ok!(readdir(path)); for child in children.iter() { @@ -581,11 +558,6 @@ pub fn rmdir_recursive(path: &Path) -> IoResult<()> { /// The file at the path specified will have its last access time set to /// `atime` and its modification time set to `mtime`. The times specified should /// be in milliseconds. -/// -/// # Errors -/// -/// This function will raise on the `io_error` condition if an error -/// happens. // FIXME(#10301) these arguments should not be u64 pub fn change_file_times(path: &Path, atime: u64, mtime: u64) -> IoResult<()> { LocalIo::maybe_raise(|io| io.fs_utime(&path.to_c_str(), atime, mtime)) @@ -639,7 +611,7 @@ impl path::Path { /// filesystem. This will return true if the path points to either a /// directory or a file. /// - /// # Errors + /// # Error /// /// Will not raise a condition pub fn exists(&self) -> bool { @@ -651,7 +623,7 @@ impl path::Path { /// to non-existent locations or directories or other non-regular files /// (named pipes, etc). /// - /// # Errors + /// # Error /// /// Will not raise a condition pub fn is_file(&self) -> bool { @@ -666,7 +638,7 @@ impl path::Path { /// Will return false for paths to non-existent locations or if the item is /// not a directory (eg files, named pipes, links, etc) /// - /// # Errors + /// # Error /// /// Will not raise a condition pub fn is_dir(&self) -> bool { diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index 3a6aa1939a4..395ece17ede 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -25,6 +25,7 @@ use vec::{Vector, ImmutableVector, MutableVector, OwnedCloneableVector}; /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::io::MemWriter; /// /// let mut w = MemWriter::new(); @@ -113,11 +114,12 @@ impl Seek for MemWriter { /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::io::MemReader; /// /// let mut r = MemReader::new(~[0, 1, 2]); /// -/// assert_eq!(r.read_to_end(), ~[0, 1, 2]); +/// assert_eq!(r.read_to_end().unwrap(), ~[0, 1, 2]); /// ``` pub struct MemReader { priv buf: ~[u8], @@ -182,12 +184,13 @@ impl Buffer for MemReader { /// Writes to a fixed-size byte slice /// -/// If a write will not fit in the buffer, it raises the `io_error` -/// condition and does not write any data. +/// If a write will not fit in the buffer, it returns an error and does not +/// write any data. /// /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::io::BufWriter; /// /// let mut buf = [0, ..4]; @@ -252,12 +255,13 @@ impl<'a> Seek for BufWriter<'a> { /// # Example /// /// ```rust +/// # #[allow(unused_must_use)]; /// use std::io::BufReader; /// /// let mut buf = [0, 1, 2, 3]; /// let mut r = BufReader::new(buf); /// -/// assert_eq!(r.read_to_end(), ~[0, 1, 2, 3]); +/// assert_eq!(r.read_to_end().unwrap(), ~[0, 1, 2, 3]); /// ``` pub struct BufReader<'a> { priv buf: &'a [u8], diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index bd7a349a2aa..58a35e96393 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -29,7 +29,6 @@ Some examples of obvious things you might want to do use std::io::BufferedReader; use std::io::stdin; - # let _g = ::std::io::ignore_io_error(); let mut stdin = BufferedReader::new(stdin()); for line in stdin.lines() { print!("{}", line); @@ -41,7 +40,6 @@ Some examples of obvious things you might want to do ```rust use std::io::File; - # let _g = ::std::io::ignore_io_error(); let contents = File::open(&Path::new("message.txt")).read_to_end(); ``` @@ -50,7 +48,6 @@ Some examples of obvious things you might want to do ```rust use std::io::File; - # let _g = ::std::io::ignore_io_error(); let mut file = File::create(&Path::new("message.txt")); file.write(bytes!("hello, file!\n")); # drop(file); @@ -63,7 +60,6 @@ Some examples of obvious things you might want to do use std::io::BufferedReader; use std::io::File; - # let _g = ::std::io::ignore_io_error(); let path = Path::new("message.txt"); let mut file = BufferedReader::new(File::open(&path)); for line in file.lines() { @@ -77,7 +73,6 @@ Some examples of obvious things you might want to do use std::io::BufferedReader; use std::io::File; - # let _g = ::std::io::ignore_io_error(); let path = Path::new("message.txt"); let mut file = BufferedReader::new(File::open(&path)); let lines: ~[~str] = file.lines().collect(); @@ -91,7 +86,6 @@ Some examples of obvious things you might want to do use std::io::net::ip::SocketAddr; use std::io::net::tcp::TcpStream; - # let _g = ::std::io::ignore_io_error(); let addr = from_str::("127.0.0.1:8080").unwrap(); let mut socket = TcpStream::connect(addr).unwrap(); socket.write(bytes!("GET / HTTP/1.0\n\n")); @@ -168,72 +162,50 @@ asynchronous request completes. # Error Handling I/O is an area where nearly every operation can result in unexpected -errors. It should allow errors to be handled efficiently. -It needs to be convenient to use I/O when you don't care -about dealing with specific errors. +errors. Errors should be painfully visible when they happen, and handling them +should be easy to work with. It should be convenient to handle specific I/O +errors, and it should also be convenient to not deal with I/O errors. Rust's I/O employs a combination of techniques to reduce boilerplate while still providing feedback about errors. The basic strategy: -* Errors are fatal by default, resulting in task failure -* Errors raise the `io_error` condition which provides an opportunity to inspect - an IoError object containing details. -* Return values must have a sensible null or zero value which is returned - if a condition is handled successfully. This may be an `Option`, an empty - vector, or other designated error value. -* Common traits are implemented for `Option`, e.g. `impl Reader for Option`, - so that nullable values do not have to be 'unwrapped' before use. +* All I/O operations return `IoResult` which is equivalent to + `Result`. The core `Result` type is defined in the `std::result` + module. +* If the `Result` type goes unused, then the compiler will by default emit a + warning about the unused result. +* Common traits are implemented for `IoResult`, e.g. + `impl Reader for IoResult`, so that error values do not have + to be 'unwrapped' before use. These features combine in the API to allow for expressions like `File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n"))` without having to worry about whether "diary.txt" exists or whether the write succeeds. As written, if either `new` or `write_line` -encounters an error the task will fail. +encounters an error then the result of the entire expression will +be an error. If you wanted to handle the error though you might write: ```rust use std::io::File; -use std::io::{IoError, io_error}; -let mut error = None; -io_error::cond.trap(|e: IoError| { - error = Some(e); -}).inside(|| { - File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n")); -}); - -if error.is_some() { - println!("failed to write my diary"); +match File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n")) { + Ok(()) => { /* succeeded */ } + Err(e) => println!("failed to write to my diary: {}", e), } + # ::std::io::fs::unlink(&Path::new("diary.txt")); ``` -FIXME: Need better condition handling syntax - -In this case the condition handler will have the opportunity to -inspect the IoError raised by either the call to `new` or the call to -`write_line`, but then execution will continue. - -So what actually happens if `new` encounters an error? To understand -that it's important to know that what `new` returns is not a `File` -but an `Option`. If the file does not open, and the condition -is handled, then `new` will simply return `None`. Because there is an -implementation of `Writer` (the trait required ultimately required for -types to implement `write_line`) there is no need to inspect or unwrap -the `Option` and we simply call `write_line` on it. If `new` -returned a `None` then the followup call to `write_line` will also -raise an error. - -## Concerns about this strategy - -This structure will encourage a programming style that is prone -to errors similar to null pointer dereferences. -In particular code written to ignore errors and expect conditions to be unhandled -will start passing around null or zero objects when wrapped in a condition handler. - -* FIXME: How should we use condition handlers that return values? -* FIXME: Should EOF raise default conditions when EOF is not an error? +So what actually happens if `create` encounters an error? +It's important to know that what `new` returns is not a `File` +but an `IoResult`. If the file does not open, then `new` will simply +return `Err(..)`. Because there is an implementation of `Writer` (the trait +required ultimately required for types to implement `write_line`) there is no +need to inspect or unwrap the `IoResult` and we simply call `write_line` +on it. If `new` returned an `Err(..)` then the followup call to `write_line` +will also return an error. # Issues with i/o scheduler affinity, work stealing, task pinning @@ -460,40 +432,23 @@ impl ToStr for IoErrorKind { pub trait Reader { - // Only two methods which need to get implemented for this trait + // Only method which need to get implemented for this trait /// Read bytes, up to the length of `buf` and place them in `buf`. /// Returns the number of bytes read. The number of bytes read my - /// be less than the number requested, even 0. Returns `None` on EOF. - /// - /// # Failure + /// be less than the number requested, even 0. Returns `Err` on EOF. /// - /// Raises the `io_error` condition on error. If the condition - /// is handled then no guarantee is made about the number of bytes - /// read and the contents of `buf`. If the condition is handled - /// returns `None` (FIXME see below). + /// # Error /// - /// # FIXME - /// - /// * Should raise_default error on eof? - /// * If the condition is handled it should still return the bytes read, - /// in which case there's no need to return Option - but then you *have* - /// to install a handler to detect eof. - /// - /// This doesn't take a `len` argument like the old `read`. - /// Will people often need to slice their vectors to call this - /// and will that be annoying? - /// Is it actually possible for 0 bytes to be read successfully? + /// If an error occurs during this I/O operation, then it is returned as + /// `Err(IoError)`. Note that end-of-file is considered an error, and can be + /// inspected for in the error's `kind` field. Also note that reading 0 + /// bytes is not considered an error in all circumstances fn read(&mut self, buf: &mut [u8]) -> IoResult; // Convenient helper methods based on the above methods - /// Reads a single byte. Returns `None` on EOF. - /// - /// # Failure - /// - /// Raises the same conditions as the `read` method. Returns - /// `None` if the condition is handled. + /// Reads a single byte. Returns `Err` on EOF. fn read_byte(&mut self) -> IoResult { let mut buf = [0]; loop { @@ -511,13 +466,9 @@ pub trait Reader { /// Reads `len` bytes and appends them to a vector. /// /// May push fewer than the requested number of bytes on error - /// or EOF. Returns true on success, false on EOF or error. - /// - /// # Failure - /// - /// Raises the same conditions as `read`. Additionally raises `io_error` - /// on EOF. If `io_error` is handled then `push_bytes` may push less - /// than the requested number of bytes. + /// or EOF. If `Ok(())` is returned, then all of the requested bytes were + /// pushed on to the vector, otherwise the amount `len` bytes couldn't be + /// read (an error was encountered), and the error is returned. fn push_bytes(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()> { let start_len = buf.len(); let mut total_read = 0; @@ -542,29 +493,36 @@ pub trait Reader { /// Reads `len` bytes and gives you back a new vector of length `len` /// - /// # Failure + /// # Error /// - /// Raises the same conditions as `read`. Additionally raises `io_error` - /// on EOF. If `io_error` is handled then the returned vector may - /// contain less than the requested number of bytes. + /// Fails with the same conditions as `read`. Additionally returns error on + /// on EOF. Note that if an error is returned, then some number of bytes may + /// have already been consumed from the underlying reader, and they are lost + /// (not returned as part of the error). If this is unacceptable, then it is + /// recommended to use the `push_bytes` or `read` methods. fn read_bytes(&mut self, len: uint) -> IoResult<~[u8]> { let mut buf = vec::with_capacity(len); - if_ok!(self.push_bytes(&mut buf, len)); - return Ok(buf); + match self.push_bytes(&mut buf, len) { + Ok(()) => Ok(buf), + Err(e) => Err(e), + } } /// Reads all remaining bytes from the stream. /// - /// # Failure + /// # Error + /// + /// Returns any non-EOF error immediately. Previously read bytes are + /// discarded when an error is returned. /// - /// Raises the same conditions as the `read` method except for - /// `EndOfFile` which is swallowed. + /// When EOF is encountered, all bytes read up to that point are returned, + /// but if 0 bytes have been read then the EOF error is returned. fn read_to_end(&mut self) -> IoResult<~[u8]> { let mut buf = vec::with_capacity(DEFAULT_BUF_SIZE); loop { match self.push_bytes(&mut buf, DEFAULT_BUF_SIZE) { Ok(()) => {} - Err(ref e) if e.kind == EndOfFile => break, + Err(ref e) if buf.len() > 0 && e.kind == EndOfFile => break, Err(e) => return Err(e) } } @@ -574,10 +532,11 @@ pub trait Reader { /// Reads all of the remaining bytes of this stream, interpreting them as a /// UTF-8 encoded stream. The corresponding string is returned. /// - /// # Failure + /// # Error /// - /// This function will raise all the same conditions as the `read` method, - /// along with raising a condition if the input is not valid UTF-8. + /// This function returns all of the same errors as `read_to_end` with an + /// additional error if the reader's contents are not a valid sequence of + /// UTF-8 bytes. fn read_to_str(&mut self) -> IoResult<~str> { self.read_to_end().and_then(|s| { match str::from_utf8_owned(s) { @@ -590,11 +549,12 @@ pub trait Reader { /// Create an iterator that reads a single byte on /// each iteration, until EOF. /// - /// # Failure + /// # Error /// - /// Raises the same conditions as the `read` method, for - /// each call to its `.next()` method. - /// Ends the iteration if the condition is handled. + /// The iterator protocol causes all specifics about errors encountered to + /// be swallowed. All errors will be signified by returning `None` from the + /// iterator. If this is undesirable, it is recommended to use the + /// `read_byte` method. fn bytes<'r>(&'r mut self) -> extensions::Bytes<'r, Self> { extensions::Bytes::new(self) } @@ -825,11 +785,14 @@ fn extend_sign(val: u64, nbytes: uint) -> i64 { } pub trait Writer { - /// Write the given buffer + /// Write the entirety of a given buffer /// - /// # Failure + /// # Errors /// - /// Raises the `io_error` condition on error + /// If an error happens during the I/O operation, the error is returned as + /// `Err`. Note that it is considered an error if the entire buffer could + /// not be written, and if an error is returned then it is unknown how much + /// data (if any) was actually written. fn write(&mut self, buf: &[u8]) -> IoResult<()>; /// Flush this output stream, ensuring that all intermediately buffered @@ -1021,11 +984,11 @@ impl Stream for T {} /// an iteration, but continue to yield elements if iteration /// is attempted again. /// -/// # Failure +/// # Error /// -/// Raises the same conditions as the `read` method except for `EndOfFile` -/// which is swallowed. -/// Iteration yields `None` if the condition is handled. +/// This iterator will swallow all I/O errors, transforming `Err` values to +/// `None`. If errors need to be handled, it is recommended to use the +/// `read_line` method directly. pub struct Lines<'r, T> { priv buffer: &'r mut T, } @@ -1049,10 +1012,11 @@ pub trait Buffer: Reader { /// consumed from this buffer returned to ensure that the bytes are never /// returned twice. /// - /// # Failure + /// # Error /// - /// This function will raise on the `io_error` condition if a read error is - /// encountered. + /// This function will return an I/O error if the underlying reader was + /// read, but returned an error. Note that it is not an error to return a + /// 0-length buffer. fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]>; /// Tells this buffer that `amt` bytes have been consumed from the buffer, @@ -1067,50 +1031,73 @@ pub trait Buffer: Reader { /// /// ```rust /// use std::io::{BufferedReader, stdin}; - /// # let _g = ::std::io::ignore_io_error(); /// /// let mut reader = BufferedReader::new(stdin()); /// - /// let input = reader.read_line().unwrap_or(~"nothing"); + /// let input = reader.read_line().ok().unwrap_or(~"nothing"); /// ``` /// - /// # Failure + /// # Error + /// + /// This function has the same error semantics as `read_until`: + /// + /// * All non-EOF errors will be returned immediately + /// * If an error is returned previously consumed bytes are lost + /// * EOF is only returned if no bytes have been read + /// * Reach EOF may mean that the delimiter is not present in the return + /// value /// - /// This function will raise on the `io_error` condition (except for - /// `EndOfFile` which is swallowed) if a read error is encountered. - /// The task will also fail if sequence of bytes leading up to - /// the newline character are not valid UTF-8. + /// Additionally, this function can fail if the line of input read is not a + /// valid UTF-8 sequence of bytes. fn read_line(&mut self) -> IoResult<~str> { - self.read_until('\n' as u8).map(|line| str::from_utf8_owned(line).unwrap()) + self.read_until('\n' as u8).and_then(|line| + match str::from_utf8_owned(line) { + Some(s) => Ok(s), + None => Err(standard_error(InvalidInput)), + } + ) } /// Create an iterator that reads a line on each iteration until EOF. /// - /// # Failure + /// # Error /// - /// Iterator raises the same conditions as the `read` method - /// except for `EndOfFile`. + /// This iterator will transform all error values to `None`, discarding the + /// cause of the error. If this is undesirable, it is recommended to call + /// `read_line` directly. fn lines<'r>(&'r mut self) -> Lines<'r, Self> { - Lines { - buffer: self, - } + Lines { buffer: self } } /// Reads a sequence of bytes leading up to a specified delimiter. Once the /// specified byte is encountered, reading ceases and the bytes up to and /// including the delimiter are returned. /// - /// # Failure + /// # Error /// - /// This function will raise on the `io_error` condition if a read error is - /// encountered, except that `EndOfFile` is swallowed. + /// If any I/O error is encountered other than EOF, the error is immediately + /// returned. Note that this may discard bytes which have already been read, + /// and those bytes will *not* be returned. It is recommended to use other + /// methods if this case is worrying. + /// + /// If EOF is encountered, then this function will return EOF if 0 bytes + /// have been read, otherwise the pending byte buffer is returned. This + /// is the reason that the byte buffer returned may not always contain the + /// delimiter. fn read_until(&mut self, byte: u8) -> IoResult<~[u8]> { let mut res = ~[]; let mut used; loop { { - let available = if_ok!(self.fill()); + let available = match self.fill() { + Ok(n) => n, + Err(ref e) if res.len() > 0 && e.kind == EndOfFile => { + used = 0; + break + } + Err(e) => return Err(e) + }; match available.iter().position(|&b| b == byte) { Some(i) => { res.push_all(available.slice_to(i + 1)); @@ -1131,13 +1118,11 @@ pub trait Buffer: Reader { /// Reads the next utf8-encoded character from the underlying stream. /// - /// This will return `None` if the following sequence of bytes in the - /// stream are not a valid utf8-sequence, or if an I/O error is encountered. + /// # Error /// - /// # Failure - /// - /// This function will raise on the `io_error` condition if a read error is - /// encountered. + /// If an I/O error occurs, or EOF, then this function will return `Err`. + /// This function will also return error if the stream does not contain a + /// valid utf-8 encoded codepoint as the next few bytes in the stream. fn read_char(&mut self) -> IoResult { let first_byte = if_ok!(self.read_byte()); let width = str::utf8_char_width(first_byte); @@ -1186,15 +1171,17 @@ pub trait Seek { fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>; } -/// A listener is a value that can consume itself to start listening for connections. +/// A listener is a value that can consume itself to start listening for +/// connections. +/// /// Doing so produces some sort of Acceptor. pub trait Listener> { /// Spin up the listener and start queuing incoming connections /// - /// # Failure + /// # Error /// - /// Raises `io_error` condition. If the condition is handled, - /// then `listen` returns `None`. + /// Returns `Err` if this listener could not be bound to listen for + /// connections. In all cases, this listener is consumed. fn listen(self) -> IoResult; } @@ -1202,12 +1189,14 @@ pub trait Listener> { pub trait Acceptor { /// Wait for and accept an incoming connection /// - /// # Failure - /// Raise `io_error` condition. If the condition is handled, - /// then `accept` returns `None`. + /// # Error + /// + /// Returns `Err` if an I/O error is encountered. fn accept(&mut self) -> IoResult; - /// Create an iterator over incoming connection attempts + /// Create an iterator over incoming connection attempts. + /// + /// Note that I/O errors will be yielded by the iterator itself. fn incoming<'r>(&'r mut self) -> IncomingConnections<'r, Self> { IncomingConnections { inc: self } } @@ -1216,10 +1205,10 @@ pub trait Acceptor { /// An infinite iterator over incoming connection attempts. /// Calling `next` will block the task until a connection is attempted. /// -/// Since connection attempts can continue forever, this iterator always returns Some. -/// The Some contains another Option representing whether the connection attempt was succesful. -/// A successful connection will be wrapped in Some. -/// A failed connection is represented as a None and raises a condition. +/// Since connection attempts can continue forever, this iterator always returns +/// `Some`. The `Some` contains the `IoResult` representing whether the +/// connection attempt was succesful. A successful connection will be wrapped +/// in `Ok`. A failed connection is represented as an `Err`. pub struct IncomingConnections<'a, A> { priv inc: &'a mut A, } @@ -1265,7 +1254,7 @@ pub enum FileMode { } /// Access permissions with which the file should be opened. `File`s -/// opened with `Read` will raise an `io_error` condition if written to. +/// opened with `Read` will return an error if written to. pub enum FileAccess { Read, Write, diff --git a/src/libstd/io/net/addrinfo.rs b/src/libstd/io/net/addrinfo.rs index f72c6aecd64..e9ffe97f1c3 100644 --- a/src/libstd/io/net/addrinfo.rs +++ b/src/libstd/io/net/addrinfo.rs @@ -70,10 +70,6 @@ pub struct Info { /// Easy name resolution. Given a hostname, returns the list of IP addresses for /// that hostname. -/// -/// # Failure -/// -/// On failure, this will raise on the `io_error` condition. pub fn get_host_addresses(host: &str) -> IoResult<~[IpAddr]> { lookup(Some(host), None, None).map(|a| a.map(|i| i.address.ip)) } @@ -88,10 +84,6 @@ pub fn get_host_addresses(host: &str) -> IoResult<~[IpAddr]> { /// * hint - see the hint structure, and "man -s 3 getaddrinfo", for how this /// controls lookup /// -/// # Failure -/// -/// On failure, this will raise on the `io_error` condition. -/// /// FIXME: this is not public because the `Hint` structure is not ready for public /// consumption just yet. fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option) diff --git a/src/libstd/io/net/unix.rs b/src/libstd/io/net/unix.rs index 63a2ba3d095..ce95b987663 100644 --- a/src/libstd/io/net/unix.rs +++ b/src/libstd/io/net/unix.rs @@ -45,11 +45,6 @@ impl UnixStream { /// /// The returned stream will be closed when the object falls out of scope. /// - /// # Failure - /// - /// This function will raise on the `io_error` condition if the connection - /// could not be made. - /// /// # Example /// /// ```rust @@ -86,11 +81,6 @@ impl UnixListener { /// /// This listener will be closed when it falls out of scope. /// - /// # Failure - /// - /// This function will raise on the `io_error` condition if the specified - /// path could not be bound. - /// /// # Example /// /// ``` diff --git a/src/libstd/io/pipe.rs b/src/libstd/io/pipe.rs index 75791164b89..ca85707149b 100644 --- a/src/libstd/io/pipe.rs +++ b/src/libstd/io/pipe.rs @@ -32,16 +32,14 @@ impl PipeStream { /// /// # Example /// - /// use std::libc; - /// use std::io::pipe; + /// ```rust + /// # #[allow(unused_must_use)]; + /// use std::libc; + /// use std::io::pipe::PipeStream; /// - /// let mut pipe = PipeStream::open(libc::STDERR_FILENO); - /// pipe.write(bytes!("Hello, stderr!")); - /// - /// # Failure - /// - /// If the pipe cannot be created, an error will be raised on the - /// `io_error` condition. + /// let mut pipe = PipeStream::open(libc::STDERR_FILENO); + /// pipe.write(bytes!("Hello, stderr!")); + /// ``` pub fn open(fd: libc::c_int) -> IoResult { LocalIo::maybe_raise(|io| { io.pipe_open(fd).map(|obj| PipeStream { obj: obj }) diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs index 87d47868d0a..ccf3d4582de 100644 --- a/src/libstd/io/process.rs +++ b/src/libstd/io/process.rs @@ -141,7 +141,7 @@ impl Process { /// Note that this is purely a wrapper around libuv's `uv_process_kill` /// function. /// - /// If the signal delivery fails, then the `io_error` condition is raised on + /// If the signal delivery fails, the corresponding error is returned. pub fn signal(&mut self, signal: int) -> IoResult<()> { self.handle.kill(signal) } diff --git a/src/libstd/io/result.rs b/src/libstd/io/result.rs index aa33ecb19e5..8e03cffd0fb 100644 --- a/src/libstd/io/result.rs +++ b/src/libstd/io/result.rs @@ -8,11 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Implementations of I/O traits for the Option type +//! Implementations of I/O traits for the IoResult type //! //! I/O constructors return option types to allow errors to be handled. -//! These implementations allow e.g. `Option` to be used -//! as a `Reader` without unwrapping the option first. +//! These implementations allow e.g. `IoResult` to be used +//! as a `Reader` without unwrapping the result first. use clone::Clone; use result::{Ok, Err}; diff --git a/src/libstd/io/signal.rs b/src/libstd/io/signal.rs index 8096bfeddfd..92b86afe24d 100644 --- a/src/libstd/io/signal.rs +++ b/src/libstd/io/signal.rs @@ -113,11 +113,10 @@ impl Listener { /// a signal, and a later call to `recv` will return the signal that was /// received while no task was waiting on it. /// - /// # Failure + /// # Error /// /// If this function fails to register a signal handler, then an error will - /// be raised on the `io_error` condition and the function will return - /// false. + /// be returned. pub fn register(&mut self, signum: Signum) -> io::IoResult<()> { if self.handles.contains_key(&signum) { return Ok(()); // self is already listening to signum, so succeed @@ -206,13 +205,11 @@ mod test { use super::User1; let mut s = Listener::new(); let mut called = false; - io::io_error::cond.trap(|_| { - called = true; - }).inside(|| { - if s.register(User1) { + match s.register(User1) { + Ok(..) => { fail!("Unexpected successful registry of signum {:?}", User1); } - }); - assert!(called); + Err(..) => {} + } } } diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 702ecfb6031..937ad0783e9 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -18,6 +18,7 @@ about the stream or terminal that it is attached to. # Example ```rust +# #[allow(unused_must_use)]; use std::io; let mut out = io::stdout(); @@ -283,12 +284,12 @@ impl StdWriter { /// when the writer is attached to something like a terminal, this is used /// to fetch the dimensions of the terminal. /// - /// If successful, returns Some((width, height)). + /// If successful, returns `Ok((width, height))`. /// - /// # Failure + /// # Error /// - /// This function will raise on the `io_error` condition if an error - /// happens. + /// This function will return an error if the output stream is not actually + /// connected to a TTY instance, or if querying the TTY instance fails. pub fn winsize(&mut self) -> IoResult<(int, int)> { match self.inner { TTY(ref mut tty) => tty.get_winsize(), @@ -305,10 +306,10 @@ impl StdWriter { /// Controls whether this output stream is a "raw stream" or simply a normal /// stream. /// - /// # Failure + /// # Error /// - /// This function will raise on the `io_error` condition if an error - /// happens. + /// This function will return an error if the output stream is not actually + /// connected to a TTY instance, or if querying the TTY instance fails. pub fn set_raw(&mut self, raw: bool) -> IoResult<()> { match self.inner { TTY(ref mut tty) => tty.set_raw(raw), diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 2457917d2e4..8815f88d694 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -372,9 +372,9 @@ pub fn self_exe_name() -> Option { fn load_self() -> Option<~[u8]> { use std::io; - match io::result(|| io::fs::readlink(&Path::new("/proc/self/exe"))) { - Ok(Some(path)) => Some(path.as_vec().to_owned()), - Ok(None) | Err(..) => None + match io::fs::readlink(&Path::new("/proc/self/exe")) { + Ok(path) => Some(path.as_vec().to_owned()), + Err(..) => None } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 17b0b7b9c38..f0ef5014ca8 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2059,21 +2059,17 @@ pub fn print_generics(s: &mut State, } else { let idx = idx - generics.lifetimes.len(); let param = generics.ty_params.get(idx); -<<<<<<< HEAD - print_ident(s, param.ident); - print_bounds(s, ¶m.bounds, false); + if_ok!(print_ident(s, param.ident)); + if_ok!(print_bounds(s, ¶m.bounds, false)); match param.default { Some(default) => { - space(&mut s.s); - word_space(s, "="); - print_type(s, default); + if_ok!(space(&mut s.s)); + if_ok!(word_space(s, "=")); + if_ok!(print_type(s, default)); } _ => {} } -======= - if_ok!(print_ident(s, param.ident)); - print_bounds(s, ¶m.bounds, false) ->>>>>>> syntax: Remove io_error usage + Ok(()) } } -- cgit 1.4.1-3-g733a5 From c765a8e7ad314651b92ff860cda0159c79dbec6e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 1 Feb 2014 11:24:42 -0800 Subject: Fixing remaining warnings and errors throughout --- src/doc/guide-conditions.md | 35 ++--- src/etc/combine-tests.py | 1 + src/libnative/io/file.rs | 8 +- src/libnative/io/process.rs | 25 ++- src/libnative/io/timer_helper.rs | 4 +- src/libnative/io/timer_win32.rs | 12 +- src/librustc/lib.rs | 4 +- src/librustc/metadata/decoder.rs | 12 +- src/librustdoc/html/format.rs | 1 - src/librustdoc/html/render.rs | 4 +- src/libstd/fmt/mod.rs | 27 ++-- src/libstd/io/mod.rs | 10 +- src/libstd/io/net/tcp.rs | 14 +- src/libstd/io/signal.rs | 1 + src/libstd/os.rs | 1 + src/libstd/path/mod.rs | 2 +- src/libsyntax/parse/token.rs | 4 +- src/libsyntax/print/pp.rs | 67 +++++--- src/libsyntax/print/pprust.rs | 218 +++++++------------------- src/libterm/lib.rs | 6 + src/test/bench/shootout-mandelbrot.rs | 3 +- src/test/bench/shootout-reverse-complement.rs | 3 +- 22 files changed, 185 insertions(+), 277 deletions(-) (limited to 'src/libnative') diff --git a/src/doc/guide-conditions.md b/src/doc/guide-conditions.md index d97de779902..5b7494c0618 100644 --- a/src/doc/guide-conditions.md +++ b/src/doc/guide-conditions.md @@ -47,7 +47,7 @@ An example program that does this task reads like this: # #[allow(unused_imports)]; use std::io::{BufferedReader, File}; # mod BufferedReader { -# use std::io::File; +# use std::io::{File, IoResult}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -55,7 +55,7 @@ use std::io::{BufferedReader, File}; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -71,7 +71,6 @@ fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; // Path takes a generic by-value, rather than by reference -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); @@ -245,7 +244,7 @@ and trapping its exit status using `task::try`: use std::io::{BufferedReader, File}; use std::task; # mod BufferedReader { -# use std::io::File; +# use std::io::{File, IoResult}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -253,7 +252,7 @@ use std::task; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -277,7 +276,6 @@ fn main() { fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); @@ -347,7 +345,7 @@ but similarly clear as the version that used `fail!` in the logic where the erro # #[allow(unused_imports)]; use std::io::{BufferedReader, File}; # mod BufferedReader { -# use std::io::File; +# use std::io::{File, IoResult}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -355,7 +353,7 @@ use std::io::{BufferedReader, File}; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -374,7 +372,6 @@ fn main() { fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); @@ -415,7 +412,7 @@ and replaces bad input lines with the pair `(-1,-1)`: # #[allow(unused_imports)]; use std::io::{BufferedReader, File}; # mod BufferedReader { -# use std::io::File; +# use std::io::{File, IoResult}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -423,7 +420,7 @@ use std::io::{BufferedReader, File}; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -447,7 +444,6 @@ fn main() { fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); @@ -489,7 +485,7 @@ Changing the condition's return type from `(int,int)` to `Option<(int,int)>` wil # #[allow(unused_imports)]; use std::io::{BufferedReader, File}; # mod BufferedReader { -# use std::io::File; +# use std::io::{IoResult, File}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -497,7 +493,7 @@ use std::io::{BufferedReader, File}; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -522,7 +518,6 @@ fn main() { fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); @@ -573,7 +568,7 @@ This can be encoded in the handler API by introducing a helper type: `enum Malfo # #[allow(unused_imports)]; use std::io::{BufferedReader, File}; # mod BufferedReader { -# use std::io::File; +# use std::io::{File, IoResult}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -581,7 +576,7 @@ use std::io::{BufferedReader, File}; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -615,7 +610,6 @@ fn main() { fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); @@ -696,7 +690,7 @@ a second condition and a helper function will suffice: # #[allow(unused_imports)]; use std::io::{BufferedReader, File}; # mod BufferedReader { -# use std::io::File; +# use std::io::{File, IoResult}; # use std::io::MemReader; # use std::io::BufferedReader; # static s : &'static [u8] = bytes!("1 2\n\ @@ -704,7 +698,7 @@ use std::io::{BufferedReader, File}; # 789 123\n\ # 45 67\n\ # "); -# pub fn new(_inner: Option) -> BufferedReader { +# pub fn new(_inner: IoResult) -> BufferedReader { # BufferedReader::new(MemReader::new(s.to_owned())) # } # } @@ -752,7 +746,6 @@ fn parse_int(x: &str) -> int { fn read_int_pairs() -> ~[(int,int)] { let mut pairs = ~[]; -# let _g = std::io::ignore_io_error(); let path = Path::new(&"foo.txt"); let mut reader = BufferedReader::new(File::open(&path)); diff --git a/src/etc/combine-tests.py b/src/etc/combine-tests.py index e87187abbcb..457c0b683ac 100755 --- a/src/etc/combine-tests.py +++ b/src/etc/combine-tests.py @@ -69,6 +69,7 @@ extern mod run_pass_stage2; use run_pass_stage2::*; use std::io; use std::io::Writer; +#[allow(warnings)] fn main() { let mut out = io::stdout(); """ diff --git a/src/libnative/io/file.rs b/src/libnative/io/file.rs index e6bead60d1b..cc5b0770d4d 100644 --- a/src/libnative/io/file.rs +++ b/src/libnative/io/file.rs @@ -561,7 +561,7 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> { } more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE); } - FindClose(find_handle); + assert!(FindClose(find_handle) != 0); free(wfd_ptr as *mut c_void); Ok(paths) } else { @@ -683,7 +683,9 @@ pub fn readlink(p: &CString) -> IoResult { ptr::mut_null()) }) }; - if handle == ptr::mut_null() { return Err(super::last_error()) } + if handle as int == libc::INVALID_HANDLE_VALUE as int { + return Err(super::last_error()) + } let ret = fill_utf16_buf_and_decode(|buf, sz| { unsafe { libc::GetFinalPathNameByHandleW(handle, buf as *u16, sz, @@ -694,7 +696,7 @@ pub fn readlink(p: &CString) -> IoResult { Some(s) => Ok(Path::new(s)), None => Err(super::last_error()), }; - unsafe { libc::CloseHandle(handle) }; + assert!(unsafe { libc::CloseHandle(handle) } != 0); return ret; } diff --git a/src/libnative/io/process.rs b/src/libnative/io/process.rs index c2522d551b6..2a061c5f9b2 100644 --- a/src/libnative/io/process.rs +++ b/src/libnative/io/process.rs @@ -149,9 +149,8 @@ impl rtio::RtioProcess for Process { unsafe fn killpid(pid: pid_t, signal: int) -> Result<(), io::IoError> { match signal { io::process::PleaseExitSignal | io::process::MustDieSignal => { - libc::funcs::extra::kernel32::TerminateProcess( - cast::transmute(pid), 1); - Ok(()) + let ret = libc::TerminateProcess(pid as libc::HANDLE, 1); + super::mkerr_winbool(ret) } _ => Err(io::IoError { kind: io::OtherIoError, @@ -255,9 +254,9 @@ fn spawn_process_os(prog: &str, args: &[~str], }) }); - CloseHandle(si.hStdInput); - CloseHandle(si.hStdOutput); - CloseHandle(si.hStdError); + assert!(CloseHandle(si.hStdInput) != 0); + assert!(CloseHandle(si.hStdOutput) != 0); + assert!(CloseHandle(si.hStdError) != 0); match create_err { Some(err) => return Err(err), @@ -269,7 +268,7 @@ fn spawn_process_os(prog: &str, args: &[~str], // able to close it later. We don't close the process handle however // because std::we want the process id to stay valid at least until the // calling code closes the process handle. - CloseHandle(pi.hThread); + assert!(CloseHandle(pi.hThread) != 0); Ok(SpawnProcessResult { pid: pi.dwProcessId as pid_t, @@ -576,9 +575,9 @@ fn with_dirp(d: Option<&Path>, cb: |*libc::c_char| -> T) -> T { #[cfg(windows)] fn free_handle(handle: *()) { - unsafe { - libc::funcs::extra::kernel32::CloseHandle(cast::transmute(handle)); - } + assert!(unsafe { + libc::CloseHandle(cast::transmute(handle)) != 0 + }) } #[cfg(unix)] @@ -629,15 +628,15 @@ fn waitpid(pid: pid_t) -> p::ProcessExit { loop { let mut status = 0; if GetExitCodeProcess(process, &mut status) == FALSE { - CloseHandle(process); + assert!(CloseHandle(process) != 0); fail!("failure in GetExitCodeProcess: {}", os::last_os_error()); } if status != STILL_ACTIVE { - CloseHandle(process); + assert!(CloseHandle(process) != 0); return p::ExitStatus(status as int); } if WaitForSingleObject(process, INFINITE) == WAIT_FAILED { - CloseHandle(process); + assert!(CloseHandle(process) != 0); fail!("failure in WaitForSingleObject: {}", os::last_os_error()); } } diff --git a/src/libnative/io/timer_helper.rs b/src/libnative/io/timer_helper.rs index 74759b467d4..7311be46e8b 100644 --- a/src/libnative/io/timer_helper.rs +++ b/src/libnative/io/timer_helper.rs @@ -126,11 +126,11 @@ mod imp { } pub fn signal(handle: HANDLE) { - unsafe { SetEvent(handle); } + assert!(unsafe { SetEvent(handle) != 0 }); } pub fn close(handle: HANDLE) { - unsafe { CloseHandle(handle); } + assert!(unsafe { CloseHandle(handle) != 0 }); } extern "system" { diff --git a/src/libnative/io/timer_win32.rs b/src/libnative/io/timer_win32.rs index e359d99eedf..6b472d2f46d 100644 --- a/src/libnative/io/timer_win32.rs +++ b/src/libnative/io/timer_win32.rs @@ -62,8 +62,8 @@ fn helper(input: libc::HANDLE, messages: Port) { c.send(()); match objs.iter().position(|&o| o == obj) { Some(i) => { - objs.remove(i); - chans.remove(i - 1); + drop(objs.remove(i)); + drop(chans.remove(i - 1)); } None => {} } @@ -83,8 +83,8 @@ fn helper(input: libc::HANDLE, messages: Port) { } }; if remove { - objs.remove(idx as uint); - chans.remove(idx as uint - 1); + drop(objs.remove(idx as uint)); + drop(chans.remove(idx as uint - 1)); } } } @@ -133,7 +133,7 @@ impl rtio::RtioTimer for Timer { ptr::mut_null(), 0) }, 1); - unsafe { imp::WaitForSingleObject(self.obj, libc::INFINITE); } + let _ = unsafe { imp::WaitForSingleObject(self.obj, libc::INFINITE) }; } fn oneshot(&mut self, msecs: u64) -> Port<()> { @@ -173,7 +173,7 @@ impl rtio::RtioTimer for Timer { impl Drop for Timer { fn drop(&mut self) { self.remove(); - unsafe { libc::CloseHandle(self.obj); } + assert!(unsafe { libc::CloseHandle(self.obj) != 0 }); } } diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index ef046419789..c5f7d61c224 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -241,8 +241,8 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) { 1u => { let ifile = matches.free[0].as_slice(); if ifile == "-" { - let src = - str::from_utf8_owned(io::stdin().read_to_end()).unwrap(); + let contents = io::stdin().read_to_end().unwrap(); + let src = str::from_utf8_owned(contents).unwrap(); (d::StrInput(src), None) } else { (d::FileInput(Path::new(ifile)), Some(Path::new(ifile))) diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 824d0efff25..8ba98e84dfa 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -1160,12 +1160,12 @@ fn list_crate_deps(data: &[u8], out: &mut io::Writer) -> io::IoResult<()> { let r = get_crate_deps(data); for dep in r.iter() { let string = token::get_ident(dep.name.name); - write!(out, - "{} {}-{}-{}\n", - dep.cnum, - string.get(), - dep.hash, - dep.vers); + if_ok!(write!(out, + "{} {}-{}-{}\n", + dep.cnum, + string.get(), + dep.hash, + dep.vers)); } if_ok!(write!(out, "\n")); diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 687f391c07f..92d15fbcd67 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -48,7 +48,6 @@ impl PuritySpace { } impl fmt::Show for clean::Generics { -impl fmt::Default for clean::Generics { fn fmt(g: &clean::Generics, f: &mut fmt::Formatter) -> fmt::Result { if g.lifetimes.len() == 0 && g.type_params.len() == 0 { return Ok(()) } if_ok!(f.buf.write("<".as_bytes())); diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 7dc89cca745..65696528a6f 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -804,13 +804,13 @@ impl<'a> fmt::Show for Item<'a> { fn fmt(it: &Item<'a>, fmt: &mut fmt::Formatter) -> fmt::Result { match attr::find_stability(it.item.attrs.iter()) { Some(ref stability) => { - write!(fmt.buf, + if_ok!(write!(fmt.buf, "{lvl}", lvl = stability.level.to_str(), reason = match stability.text { Some(ref s) => (*s).clone(), None => InternedString::new(""), - }); + })); } None => {} } diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 0bc567b270a..06737e22007 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -477,16 +477,20 @@ will look like `"\\{"`. */ -#[cfg(not(stage0))] -use prelude::*; - use cast; use char::Char; +use container::Container; use io::MemWriter; use io; -use str; +use iter::{Iterator, range}; +use num::Signed; +use option::{Option,Some,None}; use repr; +use result::{Ok, Err}; +use str::StrSlice; +use str; use util; +use vec::ImmutableVector; use vec; // NOTE this is just because the `prelude::*` import above includes @@ -494,19 +498,6 @@ use vec; #[cfg(stage0)] pub use Default = fmt::Show; // export required for `format!()` etc. -#[cfg(stage0)] -use container::Container; -#[cfg(stage0)] -use iter::{Iterator, range}; -#[cfg(stage0)] -use option::{Option,Some,None}; -#[cfg(stage0)] -use vec::ImmutableVector; -#[cfg(stage0)] -use str::StrSlice; -#[cfg(stage0)] -use num::Signed; - pub mod parse; pub mod rt; @@ -628,7 +619,7 @@ macro_rules! uniform_fn_call_workaround { ($( $name: ident, $trait_: ident; )*) => { $( #[doc(hidden)] - pub fn $name(x: &T, fmt: &mut Formatter) { + pub fn $name(x: &T, fmt: &mut Formatter) -> Result { $trait_::fmt(x, fmt) } )* diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 58a35e96393..7690c88478f 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -46,6 +46,7 @@ Some examples of obvious things you might want to do * Write a line to a file ```rust + # #[allow(unused_must_use)]; use std::io::File; let mut file = File::create(&Path::new("message.txt")); @@ -83,6 +84,7 @@ Some examples of obvious things you might want to do `write_str` and `write_line` methods. ```rust,should_fail + # #[allow(unused_must_use)]; use std::io::net::ip::SocketAddr; use std::io::net::tcp::TcpStream; @@ -188,6 +190,7 @@ be an error. If you wanted to handle the error though you might write: ```rust +# #[allow(unused_must_use)]; use std::io::File; match File::create(&Path::new("diary.txt")).write(bytes!("Met a girl.\n")) { @@ -360,7 +363,7 @@ pub struct IoError { detail: Option<~str> } -impl fmt::Default for IoError { +impl fmt::Show for IoError { fn fmt(err: &IoError, fmt: &mut fmt::Formatter) -> fmt::Result { if_ok!(fmt.buf.write_str(err.desc)); match err.detail { @@ -515,14 +518,13 @@ pub trait Reader { /// Returns any non-EOF error immediately. Previously read bytes are /// discarded when an error is returned. /// - /// When EOF is encountered, all bytes read up to that point are returned, - /// but if 0 bytes have been read then the EOF error is returned. + /// When EOF is encountered, all bytes read up to that point are returned. fn read_to_end(&mut self) -> IoResult<~[u8]> { let mut buf = vec::with_capacity(DEFAULT_BUF_SIZE); loop { match self.push_bytes(&mut buf, DEFAULT_BUF_SIZE) { Ok(()) => {} - Err(ref e) if buf.len() > 0 && e.kind == EndOfFile => break, + Err(ref e) if e.kind == EndOfFile => break, Err(e) => return Err(e) } } diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs index f3db964b882..a0bdc193d98 100644 --- a/src/libstd/io/net/tcp.rs +++ b/src/libstd/io/net/tcp.rs @@ -192,9 +192,10 @@ mod test { match stream.read(buf) { Ok(..) => fail!(), - Err(ref e) if cfg!(windows) => assert_eq!(e.kind, NotConnected), - Err(ref e) if cfg!(unix) => assert_eq!(e.kind, EndOfFile), - Err(..) => fail!(), + Err(ref e) => { + assert!(e.kind == NotConnected || e.kind == EndOfFile, + "unknown kind: {:?}", e.kind); + } } }) @@ -217,9 +218,10 @@ mod test { match stream.read(buf) { Ok(..) => fail!(), - Err(ref e) if cfg!(windows) => assert_eq!(e.kind, NotConnected), - Err(ref e) if cfg!(unix) => assert_eq!(e.kind, EndOfFile), - Err(..) => fail!(), + Err(ref e) => { + assert!(e.kind == NotConnected || e.kind == EndOfFile, + "unknown kind: {:?}", e.kind); + } } }) diff --git a/src/libstd/io/signal.rs b/src/libstd/io/signal.rs index 92b86afe24d..75804c40c58 100644 --- a/src/libstd/io/signal.rs +++ b/src/libstd/io/signal.rs @@ -203,6 +203,7 @@ mod test { fn test_io_signal_invalid_signum() { use io; use super::User1; + use result::{Ok, Err}; let mut s = Listener::new(); let mut called = false; match s.register(User1) { diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 8815f88d694..541db01f148 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -1535,6 +1535,7 @@ mod tests { assert!(*chunk.data == 0xbe); close(fd); } + drop(chunk); fs::unlink(&path).unwrap(); } diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 190cf5745f4..4aa4a3feab1 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -533,7 +533,7 @@ pub struct Display<'a, P> { } impl<'a, P: GenericPath> fmt::Show for Display<'a, P> { - fn fmt(d: &Display

, f: &mut fmt::Formatter) -> fmt::Display { + fn fmt(d: &Display

, f: &mut fmt::Formatter) -> fmt::Result { d.with_str(|s| f.pad(s)) } } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 6d2acd3d803..f1dd844fc7c 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -588,8 +588,8 @@ impl BytesContainer for InternedString { } impl fmt::Show for InternedString { - fn fmt(obj: &InternedString, f: &mut fmt::Formatter) { - write!(f.buf, "{}", obj.string.as_slice()); + fn fmt(obj: &InternedString, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}", obj.string.as_slice()) } } diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index bb287c0f5f8..f6952261723 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -299,19 +299,34 @@ impl Printer { if !self.scan_stack_empty { self.check_stack(0); let left = self.token[self.left].clone(); - self.advance_left(left, self.size[self.left]); + if_ok!(self.advance_left(left, self.size[self.left])); } - Begin(b) => { - if self.scan_stack_empty { - self.left_total = 1; - self.right_total = 1; - self.left = 0u; - self.right = 0u; - } else { self.advance_right(); } - debug!("pp Begin({})/buffer ~[{},{}]", - b.offset, self.left, self.right); + self.indent(0); + Ok(()) + } + Begin(b) => { + if self.scan_stack_empty { + self.left_total = 1; + self.right_total = 1; + self.left = 0u; + self.right = 0u; + } else { self.advance_right(); } + debug!("pp Begin({})/buffer ~[{},{}]", + b.offset, self.left, self.right); + self.token[self.right] = t; + self.size[self.right] = -self.right_total; + self.scan_push(self.right); + Ok(()) + } + End => { + if self.scan_stack_empty { + debug!("pp End/print ~[{},{}]", self.left, self.right); + self.print(t, 0) + } else { + debug!("pp End/buffer ~[{},{}]", self.left, self.right); + self.advance_right(); self.token[self.right] = t; - self.size[self.right] = -self.right_total; + self.size[self.right] = -1; self.scan_push(self.right); Ok(()) } @@ -330,12 +345,13 @@ impl Printer { self.token[self.right] = t; self.size[self.right] = -self.right_total; self.right_total += b.blank_space; + Ok(()) } String(ref s, len) => { if self.scan_stack_empty { debug!("pp String('{}')/print ~[{},{}]", *s, self.left, self.right); - self.print(t.clone(), len); + self.print(t.clone(), len) } else { debug!("pp String('{}')/buffer ~[{},{}]", *s, self.left, self.right); @@ -343,8 +359,9 @@ impl Printer { self.token[self.right] = t.clone(); self.size[self.right] = len; self.right_total += len; - self.check_stream(); + self.check_stream() } + } } } pub fn check_stream(&mut self) -> io::IoResult<()> { @@ -360,8 +377,10 @@ impl Printer { } } let left = self.token[self.left].clone(); - self.advance_left(left, self.size[self.left]); - if self.left != self.right { self.check_stream(); } + if_ok!(self.advance_left(left, self.size[self.left])); + if self.left != self.right { + if_ok!(self.check_stream()); + } } Ok(()) } @@ -405,7 +424,7 @@ impl Printer { debug!("advnce_left ~[{},{}], sizeof({})={}", self.left, self.right, self.left, L); if L >= 0 { - self.print(x.clone(), L); + let ret = self.print(x.clone(), L); match x { Break(b) => self.left_total += b.blank_space, String(_, len) => { @@ -417,7 +436,7 @@ impl Printer { self.left += 1u; self.left %= self.buf_len; let left = self.token[self.left].clone(); - self.advance_left(left, self.size[self.left]); + if_ok!(self.advance_left(left, self.size[self.left])); } ret } else { @@ -477,7 +496,7 @@ impl Printer { } write!(self.out, "{}", s) } - pub fn print(&mut self, x: Token, L: int) { + pub fn print(&mut self, x: Token, L: int) -> io::IoResult<()> { debug!("print {} {} (remaining line space={})", tok_str(x.clone()), L, self.space); debug!("{}", buf_str(self.token.clone(), @@ -587,16 +606,16 @@ pub fn end(p: &mut Printer) -> io::IoResult<()> { p.pretty_print(End) } pub fn eof(p: &mut Printer) -> io::IoResult<()> { p.pretty_print(Eof) } -pub fn word(p: &mut Printer, wrd: &str) { - p.pretty_print(String(/* bad */ wrd.to_str(), wrd.len() as int)); +pub fn word(p: &mut Printer, wrd: &str) -> io::IoResult<()> { + p.pretty_print(String(/* bad */ wrd.to_str(), wrd.len() as int)) } -pub fn huge_word(p: &mut Printer, wrd: &str) { - p.pretty_print(String(/* bad */ wrd.to_str(), SIZE_INFINITY)); +pub fn huge_word(p: &mut Printer, wrd: &str) -> io::IoResult<()> { + p.pretty_print(String(/* bad */ wrd.to_str(), SIZE_INFINITY)) } -pub fn zero_word(p: &mut Printer, wrd: &str) { - p.pretty_print(String(/* bad */ wrd.to_str(), 0)); +pub fn zero_word(p: &mut Printer, wrd: &str) -> io::IoResult<()> { + p.pretty_print(String(/* bad */ wrd.to_str(), 0)) } pub fn spaces(p: &mut Printer, n: uint) -> io::IoResult<()> { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index f0ef5014ca8..e291583d121 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -964,11 +964,7 @@ pub fn print_attribute(s: &mut State, attr: &ast::Attribute) -> io::IoResult<()> if_ok!(maybe_print_comment(s, attr.span.lo)); if attr.node.is_sugared_doc { let comment = attr.value_str().unwrap(); -<<<<<<< HEAD - word(&mut s.s, comment.get()); -======= - if_ok!(word(&mut s.s, comment)); ->>>>>>> syntax: Remove io_error usage + if_ok!(word(&mut s.s, comment.get())); } else { if_ok!(word(&mut s.s, "#[")); if_ok!(print_meta_item(s, attr.meta())); @@ -1139,24 +1135,7 @@ pub fn print_mac(s: &mut State, m: &ast::Mac) -> io::IoResult<()> { } } -<<<<<<< HEAD -pub fn print_expr_vstore(s: &mut State, t: ast::ExprVstore) { -======= -pub fn print_vstore(s: &mut State, t: ast::Vstore) -> io::IoResult<()> { - match t { - ast::VstoreFixed(Some(i)) => word(&mut s.s, format!("{}", i)), - ast::VstoreFixed(None) => word(&mut s.s, "_"), - ast::VstoreUniq => word(&mut s.s, "~"), - ast::VstoreBox => word(&mut s.s, "@"), - ast::VstoreSlice(ref r) => { - if_ok!(word(&mut s.s, "&")); - print_opt_lifetime(s, r) - } - } -} - pub fn print_expr_vstore(s: &mut State, t: ast::ExprVstore) -> io::IoResult<()> { ->>>>>>> syntax: Remove io_error usage match t { ast::ExprVstoreUniq => word(&mut s.s, "~"), ast::ExprVstoreSlice => word(&mut s.s, "&"), @@ -1557,51 +1536,27 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) -> io::IoResult<()> { } else { if_ok!(word(&mut s.s, "asm!")); } -<<<<<<< HEAD - popen(s); - print_string(s, a.asm.get(), a.asm_str_style); - word_space(s, ":"); - for &(ref co, o) in a.outputs.iter() { - print_string(s, co.get(), ast::CookedStr); - popen(s); - print_expr(s, o); - pclose(s); - word_space(s, ","); - } - word_space(s, ":"); - for &(ref co, o) in a.inputs.iter() { - print_string(s, co.get(), ast::CookedStr); - popen(s); - print_expr(s, o); - pclose(s); - word_space(s, ","); - } - word_space(s, ":"); - print_string(s, a.clobbers.get(), ast::CookedStr); - pclose(s); -======= if_ok!(popen(s)); - if_ok!(print_string(s, a.asm, a.asm_str_style)); + if_ok!(print_string(s, a.asm.get(), a.asm_str_style)); if_ok!(word_space(s, ":")); - for &(co, o) in a.outputs.iter() { - if_ok!(print_string(s, co, ast::CookedStr)); + for &(ref co, o) in a.outputs.iter() { + if_ok!(print_string(s, co.get(), ast::CookedStr)); if_ok!(popen(s)); if_ok!(print_expr(s, o)); if_ok!(pclose(s)); if_ok!(word_space(s, ",")); } if_ok!(word_space(s, ":")); - for &(co, o) in a.inputs.iter() { - if_ok!(print_string(s, co, ast::CookedStr)); + for &(ref co, o) in a.inputs.iter() { + if_ok!(print_string(s, co.get(), ast::CookedStr)); if_ok!(popen(s)); if_ok!(print_expr(s, o)); if_ok!(pclose(s)); if_ok!(word_space(s, ",")); } if_ok!(word_space(s, ":")); - if_ok!(print_string(s, a.clobbers, ast::CookedStr)); + if_ok!(print_string(s, a.clobbers.get(), ast::CookedStr)); if_ok!(pclose(s)); ->>>>>>> syntax: Remove io_error usage } ast::ExprMac(ref m) => if_ok!(print_mac(s, m)), ast::ExprParen(e) => { @@ -1659,23 +1614,14 @@ pub fn print_decl(s: &mut State, decl: &ast::Decl) -> io::IoResult<()> { } } -<<<<<<< HEAD -pub fn print_ident(s: &mut State, ident: ast::Ident) { - let string = token::get_ident(ident.name); - word(&mut s.s, string.get()); -} - -pub fn print_name(s: &mut State, name: ast::Name) { - let string = token::get_ident(name); - word(&mut s.s, string.get()); -======= pub fn print_ident(s: &mut State, ident: ast::Ident) -> io::IoResult<()> { - word(&mut s.s, ident_to_str(&ident)) + let string = token::get_ident(ident.name); + word(&mut s.s, string.get()) } pub fn print_name(s: &mut State, name: ast::Name) -> io::IoResult<()> { - word(&mut s.s, interner_get(name)) ->>>>>>> syntax: Remove io_error usage + let string = token::get_ident(name); + word(&mut s.s, string.get()) } pub fn print_for_decl(s: &mut State, loc: &ast::Local, @@ -2088,38 +2034,23 @@ pub fn print_generics(s: &mut State, pub fn print_meta_item(s: &mut State, item: &ast::MetaItem) -> io::IoResult<()> { if_ok!(ibox(s, indent_unit)); match item.node { -<<<<<<< HEAD - ast::MetaWord(ref name) => word(&mut s.s, name.get()), - ast::MetaNameValue(ref name, ref value) => { - word_space(s, name.get()); - word_space(s, "="); - print_literal(s, value); - } - ast::MetaList(ref name, ref items) => { - word(&mut s.s, name.get()); - popen(s); - commasep(s, - Consistent, - items.as_slice(), - |p, &i| print_meta_item(p, i)); - pclose(s); -======= - ast::MetaWord(name) => { if_ok!(word(&mut s.s, name)); } - ast::MetaNameValue(name, value) => { - if_ok!(word_space(s, name)); - if_ok!(word_space(s, "=")); - if_ok!(print_literal(s, &value)); - } - ast::MetaList(name, ref items) => { - if_ok!(word(&mut s.s, name)); - if_ok!(popen(s)); - if_ok!(commasep(s, - Consistent, - items.as_slice(), - |p, &i| print_meta_item(p, i))); - if_ok!(pclose(s)); ->>>>>>> syntax: Remove io_error usage - } + ast::MetaWord(ref name) => { + if_ok!(word(&mut s.s, name.get())); + } + ast::MetaNameValue(ref name, ref value) => { + if_ok!(word_space(s, name.get())); + if_ok!(word_space(s, "=")); + if_ok!(print_literal(s, value)); + } + ast::MetaList(ref name, ref items) => { + if_ok!(word(&mut s.s, name.get())); + if_ok!(popen(s)); + if_ok!(commasep(s, + Consistent, + items.as_slice(), + |p, &i| print_meta_item(p, i))); + if_ok!(pclose(s)); + } } end(s) } @@ -2171,17 +2102,10 @@ pub fn print_view_item(s: &mut State, item: &ast::ViewItem) -> io::IoResult<()> if_ok!(head(s, "extern mod")); if_ok!(print_ident(s, id)); for &(ref p, style) in optional_path.iter() { -<<<<<<< HEAD - space(&mut s.s); - word(&mut s.s, "="); - space(&mut s.s); - print_string(s, p.get(), style); -======= if_ok!(space(&mut s.s)); if_ok!(word(&mut s.s, "=")); if_ok!(space(&mut s.s)); - if_ok!(print_string(s, *p, style)); ->>>>>>> syntax: Remove io_error usage + if_ok!(print_string(s, p.get(), style)); } } @@ -2373,88 +2297,54 @@ pub fn print_literal(s: &mut State, lit: &ast::Lit) -> io::IoResult<()> { _ => () } match lit.node { -<<<<<<< HEAD ast::LitStr(ref st, style) => print_string(s, st.get(), style), ast::LitChar(ch) => { let mut res = ~"'"; char::from_u32(ch).unwrap().escape_default(|c| res.push_char(c)); res.push_char('\''); - word(&mut s.s, res); + word(&mut s.s, res) } ast::LitInt(i, t) => { if i < 0_i64 { word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u) - + ast_util::int_ty_to_str(t)); + + ast_util::int_ty_to_str(t)) } else { word(&mut s.s, (i as u64).to_str_radix(10u) - + ast_util::int_ty_to_str(t)); -======= - ast::LitStr(st, style) => print_string(s, st, style), - ast::LitChar(ch) => { - let mut res = ~"'"; - char::from_u32(ch).unwrap().escape_default(|c| res.push_char(c)); - res.push_char('\''); - word(&mut s.s, res) - } - ast::LitInt(i, t) => { - if i < 0_i64 { - word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u) - + ast_util::int_ty_to_str(t)) - } else { - word(&mut s.s, (i as u64).to_str_radix(10u) - + ast_util::int_ty_to_str(t)) - } ->>>>>>> syntax: Remove io_error usage - } - ast::LitUint(u, t) => { - word(&mut s.s, u.to_str_radix(10u) + ast_util::uint_ty_to_str(t)) + + ast_util::int_ty_to_str(t)) } - ast::LitIntUnsuffixed(i) => { - if i < 0_i64 { - word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u)) - } else { - word(&mut s.s, (i as u64).to_str_radix(10u)) - } - } - ast::LitFloat(f, t) => { - word(&mut s.s, f.to_owned() + ast_util::float_ty_to_str(t)) - } - ast::LitFloatUnsuffixed(f) => word(&mut s.s, f), - ast::LitNil => word(&mut s.s, "()"), - ast::LitBool(val) => { - if val { word(&mut s.s, "true") } else { word(&mut s.s, "false") } - } - ast::LitBinary(arr) => { - if_ok!(ibox(s, indent_unit)); - if_ok!(word(&mut s.s, "[")); - if_ok!(commasep_cmnt(s, Inconsistent, arr, - |s, u| word(&mut s.s, format!("{}", *u)), - |_| lit.span)); - if_ok!(word(&mut s.s, "]")); - end(s) + } + ast::LitUint(u, t) => { + word(&mut s.s, + u.to_str_radix(10u) + + ast_util::uint_ty_to_str(t)) + } + ast::LitIntUnsuffixed(i) => { + if i < 0_i64 { + word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u)) + } else { + word(&mut s.s, (i as u64).to_str_radix(10u)) } -<<<<<<< HEAD } + ast::LitFloat(ref f, t) => { - word(&mut s.s, f.get() + ast_util::float_ty_to_str(t)); + word(&mut s.s, f.get() + ast_util::float_ty_to_str(t)) } ast::LitFloatUnsuffixed(ref f) => word(&mut s.s, f.get()), ast::LitNil => word(&mut s.s, "()"), ast::LitBool(val) => { - if val { word(&mut s.s, "true"); } else { word(&mut s.s, "false"); } + if val { word(&mut s.s, "true") } else { word(&mut s.s, "false") } } ast::LitBinary(ref arr) => { - ibox(s, indent_unit); - word(&mut s.s, "["); - commasep_cmnt(s, Inconsistent, *arr.borrow(), |s, u| word(&mut s.s, format!("{}", *u)), - |_| lit.span); - word(&mut s.s, "]"); - end(s); - } -======= ->>>>>>> syntax: Remove io_error usage + if_ok!(ibox(s, indent_unit)); + if_ok!(word(&mut s.s, "[")); + if_ok!(commasep_cmnt(s, Inconsistent, *arr.borrow(), + |s, u| word(&mut s.s, format!("{}", *u)), + |_| lit.span)); + if_ok!(word(&mut s.s, "]")); + end(s) + } } } diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index 98dd2b20a5f..c4481a1a07f 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -19,15 +19,21 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://static.rust-lang.org/doc/master")]; +#[feature(macro_rules)]; #[deny(non_camel_case_types)]; #[allow(missing_doc)]; use std::os; +use std::io; use terminfo::TermInfo; use terminfo::searcher::open; use terminfo::parser::compiled::{parse, msys_terminfo}; use terminfo::parm::{expand, Number, Variables}; +macro_rules! if_ok ( + ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) }) +) + pub mod terminfo; // FIXME (#2807): Windows support. diff --git a/src/test/bench/shootout-mandelbrot.rs b/src/test/bench/shootout-mandelbrot.rs index a0e9f96b31d..6f4a4c43b03 100644 --- a/src/test/bench/shootout-mandelbrot.rs +++ b/src/test/bench/shootout-mandelbrot.rs @@ -8,11 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::io; use std::io::BufferedWriter; struct DummyWriter; impl Writer for DummyWriter { - fn write(&mut self, _: &[u8]) {} + fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) } } static ITER: int = 50; diff --git a/src/test/bench/shootout-reverse-complement.rs b/src/test/bench/shootout-reverse-complement.rs index 43167458e86..771e545ece8 100644 --- a/src/test/bench/shootout-reverse-complement.rs +++ b/src/test/bench/shootout-reverse-complement.rs @@ -36,11 +36,12 @@ fn make_complements() -> [u8, ..256] { fn main() { let complements = make_complements(); - let mut data = if std::os::getenv("RUST_BENCH").is_some() { + let data = if std::os::getenv("RUST_BENCH").is_some() { File::open(&Path::new("shootout-k-nucleotide.data")).read_to_end() } else { stdin().read_to_end() }; + let mut data = data.unwrap(); for seq in data.mut_split(|c| *c == '>' as u8) { // skip header and last \n -- cgit 1.4.1-3-g733a5