Skip to content

Commit

Permalink
Clean up formatting in macros module
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Feb 7, 2014
1 parent 1e55cae commit 8192f55
Showing 1 changed file with 74 additions and 58 deletions.
132 changes: 74 additions & 58 deletions src/libstd/macros.rs
Expand Up @@ -20,23 +20,33 @@ macro_rules! log(
}
})
)

#[macro_export]
macro_rules! error( ($($arg:tt)*) => (log!(1u32, $($arg)*)) )
macro_rules! error(
($($arg:tt)*) => (log!(1u32, $($arg)*))
)

#[macro_export]
macro_rules! warn ( ($($arg:tt)*) => (log!(2u32, $($arg)*)) )
macro_rules! warn(
($($arg:tt)*) => (log!(2u32, $($arg)*))
)

#[macro_export]
macro_rules! info ( ($($arg:tt)*) => (log!(3u32, $($arg)*)) )
macro_rules! info(
($($arg:tt)*) => (log!(3u32, $($arg)*))
)

#[macro_export]
macro_rules! debug( ($($arg:tt)*) => (
if cfg!(not(ndebug)) { log!(4u32, $($arg)*) }
))
macro_rules! debug(
($($arg:tt)*) => (if cfg!(not(ndebug)) { log!(4u32, $($arg)*) })
)

#[macro_export]
macro_rules! log_enabled(
($lvl:expr) => ( {
($lvl:expr) => ({
let lvl = $lvl;
lvl <= __log_level() && (lvl != 4 || cfg!(not(ndebug)))
} )
})
)

#[macro_export]
Expand All @@ -47,54 +57,50 @@ macro_rules! fail(
($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)*)
($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) => {
($cond:expr) => (
if !$cond {
fail!("assertion failed: {:s}", stringify!($cond))
}
};
($cond:expr, $msg:expr) => {
);
($cond:expr, $msg:expr) => (
if !$cond {
fail!($msg)
}
};
($cond:expr, $( $arg:expr ),+) => {
);
($cond:expr, $($arg:expr),+) => (
if !$cond {
fail!( $($arg),+ )
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)
}
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
Expand All @@ -103,7 +109,7 @@ macro_rules! assert_eq (
///
/// # Example
///
/// ```rust
/// ~~~rust
/// fn choose_weighted_item(v: &[Item]) -> Item {
/// assert!(!v.is_empty());
/// let mut so_far = 0u;
Expand All @@ -117,11 +123,11 @@ macro_rules! assert_eq (
/// // type checker that it isn't possible to get down here
/// unreachable!();
/// }
/// ```
/// ~~~
#[macro_export]
macro_rules! unreachable (() => (
fail!("internal error: entered unreachable code");
))
macro_rules! unreachable(
() => (fail!("internal error: entered unreachable code"))
)

/// A standardised placeholder for marking unfinished code. It fails with the
/// message `"not yet implemented"` when executed.
Expand All @@ -131,37 +137,47 @@ macro_rules! unimplemented(
)

#[macro_export]
macro_rules! format(($($arg:tt)*) => (
format_args!(::std::fmt::format, $($arg)*)
))
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_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_rules! writeln(
($dst:expr, $($arg:tt)*) => (
format_args!(|args| { ::std::fmt::writeln($dst, args) }, $($arg)*)
)
)

#[macro_export]
macro_rules! print (
macro_rules! print(
($($arg:tt)*) => (format_args!(::std::io::stdio::print_args, $($arg)*))
)

#[macro_export]
macro_rules! println (
macro_rules! println(
($($arg:tt)*) => (format_args!(::std::io::stdio::println_args, $($arg)*))
)

#[macro_export]
macro_rules! local_data_key (
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 (
macro_rules! if_ok(
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
)

5 comments on commit 8192f55

@bors
Copy link
Contributor

@bors bors commented on 8192f55 Feb 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at brendanzab@8192f55

@bors
Copy link
Contributor

@bors bors commented on 8192f55 Feb 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/rust/unimplemented = 8192f55 into auto

@bors
Copy link
Contributor

@bors bors commented on 8192f55 Feb 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/rust/unimplemented = 8192f55 merged ok, testing candidate = c8759f6

@bors
Copy link
Contributor

@bors bors commented on 8192f55 Feb 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 8192f55 Feb 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c8759f6

Please sign in to comment.