|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | +#[macro_escape]; |
| 11 | + |
| 12 | +#[macro_export] |
| 13 | +macro_rules! log( |
| 14 | + ($lvl:expr, $($arg:tt)+) => ({ |
| 15 | + let lvl = $lvl; |
| 16 | + if lvl <= __log_level() { |
| 17 | + format_args!(|args| { |
| 18 | + ::std::logging::log(lvl, args) |
| 19 | + }, $($arg)+) |
| 20 | + } |
| 21 | + }) |
| 22 | +) |
| 23 | +#[macro_export] |
| 24 | +macro_rules! error( ($($arg:tt)*) => (log!(1u32, $($arg)*)) ) |
| 25 | +#[macro_export] |
| 26 | +macro_rules! warn ( ($($arg:tt)*) => (log!(2u32, $($arg)*)) ) |
| 27 | +#[macro_export] |
| 28 | +macro_rules! info ( ($($arg:tt)*) => (log!(3u32, $($arg)*)) ) |
| 29 | +#[macro_export] |
| 30 | +macro_rules! debug( ($($arg:tt)*) => ( |
| 31 | + if cfg!(not(ndebug)) { log!(4u32, $($arg)*) } |
| 32 | +)) |
| 33 | + |
| 34 | +#[macro_export] |
| 35 | +macro_rules! log_enabled( |
| 36 | + ($lvl:expr) => ( { |
| 37 | + let lvl = $lvl; |
| 38 | + lvl <= __log_level() && (lvl != 4 || cfg!(not(ndebug))) |
| 39 | + } ) |
| 40 | +) |
| 41 | + |
| 42 | +#[macro_export] |
| 43 | +macro_rules! fail( |
| 44 | + () => ( |
| 45 | + fail!("explicit failure") |
| 46 | + ); |
| 47 | + ($msg:expr) => ( |
| 48 | + ::std::rt::begin_unwind($msg, file!(), line!()) |
| 49 | + ); |
| 50 | + ($fmt:expr, $($arg:tt)*) => ( |
| 51 | + ::std::rt::begin_unwind(format!($fmt, $($arg)*), file!(), line!()) |
| 52 | + ) |
| 53 | +) |
| 54 | + |
| 55 | +#[macro_export] |
| 56 | +macro_rules! assert( |
| 57 | + ($cond:expr) => { |
| 58 | + if !$cond { |
| 59 | + fail!("assertion failed: {:s}", stringify!($cond)) |
| 60 | + } |
| 61 | + }; |
| 62 | + ($cond:expr, $msg:expr) => { |
| 63 | + if !$cond { |
| 64 | + fail!($msg) |
| 65 | + } |
| 66 | + }; |
| 67 | + ($cond:expr, $( $arg:expr ),+) => { |
| 68 | + if !$cond { |
| 69 | + fail!( $($arg),+ ) |
| 70 | + } |
| 71 | + } |
| 72 | +) |
| 73 | + |
| 74 | +#[macro_export] |
| 75 | +macro_rules! assert_eq ( |
| 76 | + ($given:expr , $expected:expr) => ( |
| 77 | + { |
| 78 | + let given_val = &($given); |
| 79 | + let expected_val = &($expected); |
| 80 | + // check both directions of equality.... |
| 81 | + if !((*given_val == *expected_val) && |
| 82 | + (*expected_val == *given_val)) { |
| 83 | + fail!("assertion failed: `(left == right) && (right == left)` \ |
| 84 | + (left: `{:?}`, right: `{:?}`)", *given_val, *expected_val) |
| 85 | + } |
| 86 | + } |
| 87 | + ) |
| 88 | +) |
| 89 | + |
| 90 | +/// A utility macro for indicating unreachable code. It will fail if |
| 91 | +/// executed. This is occasionally useful to put after loops that never |
| 92 | +/// terminate normally, but instead directly return from a function. |
| 93 | +/// |
| 94 | +/// # Example |
| 95 | +/// |
| 96 | +/// ```rust |
| 97 | +/// fn choose_weighted_item(v: &[Item]) -> Item { |
| 98 | +/// assert!(!v.is_empty()); |
| 99 | +/// let mut so_far = 0u; |
| 100 | +/// for item in v.iter() { |
| 101 | +/// so_far += item.weight; |
| 102 | +/// if so_far > 100 { |
| 103 | +/// return item; |
| 104 | +/// } |
| 105 | +/// } |
| 106 | +/// // The above loop always returns, so we must hint to the |
| 107 | +/// // type checker that it isn't possible to get down here |
| 108 | +/// unreachable!(); |
| 109 | +/// } |
| 110 | +/// ``` |
| 111 | +#[macro_export] |
| 112 | +macro_rules! unreachable (() => ( |
| 113 | + fail!("internal error: entered unreachable code"); |
| 114 | +)) |
| 115 | + |
| 116 | +#[macro_export] |
| 117 | +macro_rules! condition ( |
| 118 | + |
| 119 | + { pub $c:ident: $input:ty -> $out:ty; } => { |
| 120 | + |
| 121 | + pub mod $c { |
| 122 | + #[allow(unused_imports)]; |
| 123 | + #[allow(non_uppercase_statics)]; |
| 124 | + #[allow(missing_doc)]; |
| 125 | + |
| 126 | + use super::*; |
| 127 | + |
| 128 | + local_data_key!(key: @::std::condition::Handler<$input, $out>) |
| 129 | + |
| 130 | + pub static cond : |
| 131 | + ::std::condition::Condition<$input,$out> = |
| 132 | + ::std::condition::Condition { |
| 133 | + name: stringify!($c), |
| 134 | + key: key |
| 135 | + }; |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + { $c:ident: $input:ty -> $out:ty; } => { |
| 140 | + |
| 141 | + mod $c { |
| 142 | + #[allow(unused_imports)]; |
| 143 | + #[allow(non_uppercase_statics)]; |
| 144 | + #[allow(dead_code)]; |
| 145 | + |
| 146 | + use super::*; |
| 147 | + |
| 148 | + local_data_key!(key: @::std::condition::Handler<$input, $out>) |
| 149 | + |
| 150 | + pub static cond : |
| 151 | + ::std::condition::Condition<$input,$out> = |
| 152 | + ::std::condition::Condition { |
| 153 | + name: stringify!($c), |
| 154 | + key: key |
| 155 | + }; |
| 156 | + } |
| 157 | + } |
| 158 | +) |
| 159 | + |
| 160 | +#[macro_export] |
| 161 | +macro_rules! format(($($arg:tt)*) => ( |
| 162 | + format_args!(::std::fmt::format, $($arg)*) |
| 163 | +)) |
| 164 | +#[macro_export] |
| 165 | +macro_rules! write(($dst:expr, $($arg:tt)*) => ( |
| 166 | + format_args!(|args| { ::std::fmt::write($dst, args) }, $($arg)*) |
| 167 | +)) |
| 168 | +#[macro_export] |
| 169 | +macro_rules! writeln(($dst:expr, $($arg:tt)*) => ( |
| 170 | + format_args!(|args| { ::std::fmt::writeln($dst, args) }, $($arg)*) |
| 171 | +)) |
| 172 | +#[macro_export] |
| 173 | +macro_rules! print ( |
| 174 | + ($($arg:tt)*) => (format_args!(::std::io::stdio::print_args, $($arg)*)) |
| 175 | +) |
| 176 | +#[macro_export] |
| 177 | +macro_rules! println ( |
| 178 | + ($($arg:tt)*) => (format_args!(::std::io::stdio::println_args, $($arg)*)) |
| 179 | +) |
| 180 | + |
| 181 | +#[macro_export] |
| 182 | +macro_rules! local_data_key ( |
| 183 | + ($name:ident: $ty:ty) => ( |
| 184 | + static $name: ::std::local_data::Key<$ty> = &::std::local_data::Key; |
| 185 | + ); |
| 186 | + (pub $name:ident: $ty:ty) => ( |
| 187 | + pub static $name: ::std::local_data::Key<$ty> = &::std::local_data::Key; |
| 188 | + ) |
| 189 | +) |
0 commit comments