]> git.lizzy.rs Git - rust.git/blob - src/libcore/macros.rs
rollup merge of #20608: nikomatsakis/assoc-types-method-dispatch
[rust.git] / src / libcore / macros.rs
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
11 #![macro_escape]
12
13 /// Entry point of task panic, for details, see std::macros
14 #[macro_export]
15 macro_rules! panic {
16     () => (
17         panic!("explicit panic")
18     );
19     ($msg:expr) => ({
20         static _MSG_FILE_LINE: (&'static str, &'static str, uint) = ($msg, file!(), line!());
21         ::core::panicking::panic(&_MSG_FILE_LINE)
22     });
23     ($fmt:expr, $($arg:tt)*) => ({
24         // The leading _'s are to avoid dead code warnings if this is
25         // used inside a dead function. Just `#[allow(dead_code)]` is
26         // insufficient, since the user may have
27         // `#[forbid(dead_code)]` and which cannot be overridden.
28         static _FILE_LINE: (&'static str, uint) = (file!(), line!());
29         ::core::panicking::panic_fmt(format_args!($fmt, $($arg)*), &_FILE_LINE)
30     });
31 }
32
33 /// Runtime assertion, for details see std::macros
34 #[macro_export]
35 macro_rules! assert {
36     ($cond:expr) => (
37         if !$cond {
38             panic!(concat!("assertion failed: ", stringify!($cond)))
39         }
40     );
41     ($cond:expr, $($arg:tt)*) => (
42         if !$cond {
43             panic!($($arg)*)
44         }
45     );
46 }
47
48 /// Runtime assertion for equality, for details see std::macros
49 #[macro_export]
50 macro_rules! assert_eq {
51     ($cond1:expr, $cond2:expr) => ({
52         let c1 = $cond1;
53         let c2 = $cond2;
54         if c1 != c2 || c2 != c1 {
55             panic!("expressions not equal, left: {}, right: {}", c1, c2);
56         }
57     })
58 }
59
60 /// Runtime assertion for equality, only without `--cfg ndebug`
61 #[macro_export]
62 macro_rules! debug_assert_eq {
63     ($($a:tt)*) => ({
64         if cfg!(not(ndebug)) {
65             assert_eq!($($a)*);
66         }
67     })
68 }
69
70 /// Runtime assertion, disableable at compile time with `--cfg ndebug`
71 #[macro_export]
72 macro_rules! debug_assert {
73     ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
74 }
75
76 /// Short circuiting evaluation on Err
77 #[macro_export]
78 macro_rules! try {
79     ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
80 }
81
82 /// Writing a formatted string into a writer
83 #[macro_export]
84 macro_rules! write {
85     ($dst:expr, $($arg:tt)*) => ((&mut *$dst).write_fmt(format_args!($($arg)*)))
86 }
87
88 /// Writing a formatted string plus a newline into a writer
89 #[macro_export]
90 macro_rules! writeln {
91     ($dst:expr, $fmt:expr $($arg:tt)*) => (
92         write!($dst, concat!($fmt, "\n") $($arg)*)
93     )
94 }
95
96 #[macro_export]
97 macro_rules! unreachable { () => (panic!("unreachable code")) }
98