]> git.lizzy.rs Git - rust.git/blob - src/libcore/macros.rs
Use correct conventions for static
[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 failure, for details, see std::macros
14 #[macro_export]
15 macro_rules! fail(
16     () => (
17         fail!("{}", "explicit failure")
18     );
19     ($msg:expr) => (
20         fail!("{}", $msg)
21     );
22     ($fmt:expr, $($arg:tt)*) => ({
23         // a closure can't have return type !, so we need a full
24         // function to pass to format_args!, *and* we need the
25         // file and line numbers right here; so an inner bare fn
26         // is our only choice.
27         //
28         // LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
29         // is #[cold] and #[inline(never)] and because this is flagged as cold
30         // as returning !. We really do want this to be inlined, however,
31         // because it's just a tiny wrapper. Small wins (156K to 149K in size)
32         // were seen when forcing this to be inlined, and that number just goes
33         // up with the number of calls to fail!()
34         #[inline(always)]
35         fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
36             static FILE_LINE: (&'static str, uint) = (file!(), line!());
37             ::core::failure::begin_unwind(fmt, &FILE_LINE)
38         }
39         format_args!(run_fmt, $fmt, $($arg)*)
40     });
41 )
42
43 /// Runtime assertion, for details see std::macros
44 #[macro_export]
45 macro_rules! assert(
46     ($cond:expr) => (
47         if !$cond {
48             fail!(concat!("assertion failed: ", stringify!($cond)))
49         }
50     );
51     ($cond:expr, $($arg:tt)*) => (
52         if !$cond {
53             fail!($($arg)*)
54         }
55     );
56 )
57
58 /// Runtime assertion, only without `--cfg ndebug`
59 #[macro_export]
60 macro_rules! debug_assert(
61     ($(a:tt)*) => ({
62         if cfg!(not(ndebug)) {
63             assert!($($a)*);
64         }
65     })
66 )
67
68 /// Runtime assertion for equality, for details see std::macros
69 #[macro_export]
70 macro_rules! assert_eq(
71     ($cond1:expr, $cond2:expr) => ({
72         let c1 = $cond1;
73         let c2 = $cond2;
74         if c1 != c2 || c2 != c1 {
75             fail!("expressions not equal, left: {}, right: {}", c1, c2);
76         }
77     })
78 )
79
80 /// Runtime assertion for equality, only without `--cfg ndebug`
81 #[macro_export]
82 macro_rules! debug_assert_eq(
83     ($($a:tt)*) => ({
84         if cfg!(not(ndebug)) {
85             assert_eq!($($a)*);
86         }
87     })
88 )
89
90 /// Runtime assertion, disableable at compile time
91 #[macro_export]
92 macro_rules! debug_assert(
93     ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
94 )
95
96 /// Short circuiting evaluation on Err
97 #[macro_export]
98 macro_rules! try(
99     ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
100 )
101
102 /// Writing a formatted string into a writer
103 #[macro_export]
104 macro_rules! write(
105     ($dst:expr, $($arg:tt)*) => (format_args_method!($dst, write_fmt, $($arg)*))
106 )
107
108 /// Writing a formatted string plus a newline into a writer
109 #[macro_export]
110 macro_rules! writeln(
111     ($dst:expr, $fmt:expr $($arg:tt)*) => (
112         write!($dst, concat!($fmt, "\n") $($arg)*)
113     )
114 )
115
116 /// Write some formatted data into a stream.
117 ///
118 /// Identical to the macro in `std::macros`
119 #[macro_export]
120 macro_rules! write(
121     ($dst:expr, $($arg:tt)*) => ({
122         format_args_method!($dst, write_fmt, $($arg)*)
123     })
124 )
125
126 #[macro_export]
127 macro_rules! unreachable( () => (fail!("unreachable code")) )