454882dcb7
This has been a long time coming. Conditions in rust were initially envisioned as being a good alternative to error code return pattern. The idea is that all errors are fatal-by-default, and you can opt-in to handling the error by registering an error handler. While sounding nice, conditions ended up having some unforseen shortcomings: * Actually handling an error has some very awkward syntax: let mut result = None; let mut answer = None; io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| { answer = Some(some_io_operation()); }); match result { Some(err) => { /* hit an I/O error */ } None => { let answer = answer.unwrap(); /* deal with the result of I/O */ } } This pattern can certainly use functions like io::result, but at its core actually handling conditions is fairly difficult * The "zero value" of a function is often confusing. One of the main ideas behind using conditions was to change the signature of I/O functions. Instead of read_be_u32() returning a result, it returned a u32. Errors were notified via a condition, and if you caught the condition you understood that the "zero value" returned is actually a garbage value. These zero values are often difficult to understand, however. One case of this is the read_bytes() function. The function takes an integer length of the amount of bytes to read, and returns an array of that size. The array may actually be shorter, however, if an error occurred. Another case is fs::stat(). The theoretical "zero value" is a blank stat struct, but it's a little awkward to create and return a zero'd out stat struct on a call to stat(). In general, the return value of functions that can raise error are much more natural when using a Result as opposed to an always-usable zero-value. * Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O is as simple as calling read() and write(), but using conditions imposed the restriction that a rust local task was required if you wanted to catch errors with I/O. While certainly an surmountable difficulty, this was always a bit of a thorn in the side of conditions. * Functions raising conditions are not always clear that they are raising conditions. This suffers a similar problem to exceptions where you don't actually know whether a function raises a condition or not. The documentation likely explains, but if someone retroactively adds a condition to a function there's nothing forcing upstream users to acknowledge a new point of task failure. * Libaries using I/O are not guaranteed to correctly raise on conditions when an error occurs. In developing various I/O libraries, it's much easier to just return `None` from a read rather than raising an error. The silent contract of "don't raise on EOF" was a little difficult to understand and threw a wrench into the answer of the question "when do I raise a condition?" Many of these difficulties can be overcome through documentation, examples, and general practice. In the end, all of these difficulties added together ended up being too overwhelming and improving various aspects didn't end up helping that much. A result-based I/O error handling strategy also has shortcomings, but the cognitive burden is much smaller. The tooling necessary to make this strategy as usable as conditions were is much smaller than the tooling necessary for conditions. Perhaps conditions may manifest themselves as a future entity, but for now we're going to remove them from the standard library. Closes #9795 Closes #8968
161 lines
4.5 KiB
Rust
161 lines
4.5 KiB
Rust
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
#[macro_escape];
|
|
|
|
#[macro_export]
|
|
macro_rules! log(
|
|
($lvl:expr, $($arg:tt)+) => ({
|
|
let lvl = $lvl;
|
|
if lvl <= __log_level() {
|
|
format_args!(|args| {
|
|
::std::logging::log(lvl, args)
|
|
}, $($arg)+)
|
|
}
|
|
})
|
|
)
|
|
#[macro_export]
|
|
macro_rules! error( ($($arg:tt)*) => (log!(1u32, $($arg)*)) )
|
|
#[macro_export]
|
|
macro_rules! warn ( ($($arg:tt)*) => (log!(2u32, $($arg)*)) )
|
|
#[macro_export]
|
|
macro_rules! info ( ($($arg:tt)*) => (log!(3u32, $($arg)*)) )
|
|
#[macro_export]
|
|
macro_rules! debug( ($($arg:tt)*) => (
|
|
if cfg!(not(ndebug)) { log!(4u32, $($arg)*) }
|
|
))
|
|
|
|
#[macro_export]
|
|
macro_rules! log_enabled(
|
|
($lvl:expr) => ( {
|
|
let lvl = $lvl;
|
|
lvl <= __log_level() && (lvl != 4 || cfg!(not(ndebug)))
|
|
} )
|
|
)
|
|
|
|
#[macro_export]
|
|
macro_rules! fail(
|
|
() => (
|
|
fail!("explicit failure")
|
|
);
|
|
($msg:expr) => (
|
|
::std::rt::begin_unwind($msg, file!(), line!())
|
|
);
|
|
($fmt:expr, $($arg:tt)*) => (
|
|
{
|
|
// a closure can't have return type !, so we need a full
|
|
// function to pass to format_args!, *and* we need the
|
|
// file and line numbers right here; so an inner bare fn
|
|
// is our only choice.
|
|
#[inline]
|
|
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
|
|
::std::rt::begin_unwind_fmt(fmt, file!(), line!())
|
|
}
|
|
format_args!(run_fmt, $fmt, $($arg)*)
|
|
}
|
|
)
|
|
)
|
|
|
|
#[macro_export]
|
|
macro_rules! assert(
|
|
($cond:expr) => {
|
|
if !$cond {
|
|
fail!("assertion failed: {:s}", stringify!($cond))
|
|
}
|
|
};
|
|
($cond:expr, $msg:expr) => {
|
|
if !$cond {
|
|
fail!($msg)
|
|
}
|
|
};
|
|
($cond:expr, $( $arg:expr ),+) => {
|
|
if !$cond {
|
|
fail!( $($arg),+ )
|
|
}
|
|
}
|
|
)
|
|
|
|
#[macro_export]
|
|
macro_rules! assert_eq (
|
|
($given:expr , $expected:expr) => (
|
|
{
|
|
let given_val = &($given);
|
|
let expected_val = &($expected);
|
|
// check both directions of equality....
|
|
if !((*given_val == *expected_val) &&
|
|
(*expected_val == *given_val)) {
|
|
fail!("assertion failed: `(left == right) && (right == left)` \
|
|
(left: `{:?}`, right: `{:?}`)", *given_val, *expected_val)
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
/// A utility macro for indicating unreachable code. It will fail if
|
|
/// executed. This is occasionally useful to put after loops that never
|
|
/// terminate normally, but instead directly return from a function.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```rust
|
|
/// fn choose_weighted_item(v: &[Item]) -> Item {
|
|
/// assert!(!v.is_empty());
|
|
/// let mut so_far = 0u;
|
|
/// for item in v.iter() {
|
|
/// so_far += item.weight;
|
|
/// if so_far > 100 {
|
|
/// return item;
|
|
/// }
|
|
/// }
|
|
/// // The above loop always returns, so we must hint to the
|
|
/// // type checker that it isn't possible to get down here
|
|
/// unreachable!();
|
|
/// }
|
|
/// ```
|
|
#[macro_export]
|
|
macro_rules! unreachable (() => (
|
|
fail!("internal error: entered unreachable code");
|
|
))
|
|
|
|
#[macro_export]
|
|
macro_rules! format(($($arg:tt)*) => (
|
|
format_args!(::std::fmt::format, $($arg)*)
|
|
))
|
|
#[macro_export]
|
|
macro_rules! write(($dst:expr, $($arg:tt)*) => (
|
|
format_args!(|args| { ::std::fmt::write($dst, args) }, $($arg)*)
|
|
))
|
|
#[macro_export]
|
|
macro_rules! writeln(($dst:expr, $($arg:tt)*) => (
|
|
format_args!(|args| { ::std::fmt::writeln($dst, args) }, $($arg)*)
|
|
))
|
|
#[macro_export]
|
|
macro_rules! print (
|
|
($($arg:tt)*) => (format_args!(::std::io::stdio::print_args, $($arg)*))
|
|
)
|
|
#[macro_export]
|
|
macro_rules! println (
|
|
($($arg:tt)*) => (format_args!(::std::io::stdio::println_args, $($arg)*))
|
|
)
|
|
|
|
#[macro_export]
|
|
macro_rules! local_data_key (
|
|
($name:ident: $ty:ty) => (
|
|
static $name: ::std::local_data::Key<$ty> = &::std::local_data::Key;
|
|
);
|
|
(pub $name:ident: $ty:ty) => (
|
|
pub static $name: ::std::local_data::Key<$ty> = &::std::local_data::Key;
|
|
)
|
|
)
|
|
|
|
#[macro_export]
|
|
macro_rules! if_ok (
|
|
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
|
|
)
|