@@ -30,69 +30,224 @@ macro_rules! panic {
30
30
} ) ;
31
31
}
32
32
33
- /// Runtime assertion, for details see std::macros
33
+ /// Ensure that a boolean expression is `true` at runtime.
34
+ ///
35
+ /// This will invoke the `panic!` macro if the provided expression cannot be
36
+ /// evaluated to `true` at runtime.
37
+ ///
38
+ /// # Example
39
+ ///
40
+ /// ```
41
+ /// // the panic message for these assertions is the stringified value of the
42
+ /// // expression given.
43
+ /// assert!(true);
44
+ /// # fn some_computation() -> bool { true }
45
+ /// assert!(some_computation());
46
+ ///
47
+ /// // assert with a custom message
48
+ /// # let x = true;
49
+ /// assert!(x, "x wasn't true!");
50
+ /// # let a = 3i; let b = 27i;
51
+ /// assert!(a + b == 30, "a = {}, b = {}", a, b);
52
+ /// ```
34
53
#[ macro_export]
35
54
macro_rules! assert {
36
55
( $cond: expr) => (
37
56
if !$cond {
38
57
panic!( concat!( "assertion failed: " , stringify!( $cond) ) )
39
58
}
40
59
) ;
41
- ( $cond: expr, $( $arg: tt ) * ) => (
60
+ ( $cond: expr, $( $arg: expr ) ,+ ) => (
42
61
if !$cond {
43
- panic!( $( $arg) * )
62
+ panic!( $( $arg) ,+ )
44
63
}
45
64
) ;
46
65
}
47
66
48
- /// Runtime assertion for equality, for details see std::macros
67
+ /// Asserts that two expressions are equal to each other, testing equality in
68
+ /// both directions.
69
+ ///
70
+ /// On panic, this macro will print the values of the expressions.
71
+ ///
72
+ /// # Example
73
+ ///
74
+ /// ```
75
+ /// let a = 3i;
76
+ /// let b = 1i + 2i;
77
+ /// assert_eq!(a, b);
78
+ /// ```
49
79
#[ macro_export]
50
80
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) ;
81
+ ( $left: expr , $right: expr) => ( {
82
+ match ( & ( $left) , & ( $right) ) {
83
+ ( left_val, right_val) => {
84
+ // check both directions of equality....
85
+ if !( ( * left_val == * right_val) &&
86
+ ( * right_val == * left_val) ) {
87
+ panic!( "assertion failed: `(left == right) && (right == left)` \
88
+ (left: `{}`, right: `{}`)", * left_val, * right_val)
89
+ }
90
+ }
56
91
}
57
92
} )
58
93
}
59
94
60
- /// Runtime assertion for equality, only without `--cfg ndebug`
95
+ /// Ensure that a boolean expression is `true` at runtime.
96
+ ///
97
+ /// This will invoke the `panic!` macro if the provided expression cannot be
98
+ /// evaluated to `true` at runtime.
99
+ ///
100
+ /// Unlike `assert!`, `debug_assert!` statements can be disabled by passing
101
+ /// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for
102
+ /// checks that are too expensive to be present in a release build but may be
103
+ /// helpful during development.
104
+ ///
105
+ /// # Example
106
+ ///
107
+ /// ```
108
+ /// // the panic message for these assertions is the stringified value of the
109
+ /// // expression given.
110
+ /// debug_assert!(true);
111
+ /// # fn some_expensive_computation() -> bool { true }
112
+ /// debug_assert!(some_expensive_computation());
113
+ ///
114
+ /// // assert with a custom message
115
+ /// # let x = true;
116
+ /// debug_assert!(x, "x wasn't true!");
117
+ /// # let a = 3i; let b = 27i;
118
+ /// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
119
+ /// ```
120
+ #[ macro_export]
121
+ macro_rules! debug_assert {
122
+ ( $( $arg: tt) * ) => ( if cfg!( not( ndebug) ) { assert!( $( $arg) * ) ; } )
123
+ }
124
+
125
+ /// Asserts that two expressions are equal to each other, testing equality in
126
+ /// both directions.
127
+ ///
128
+ /// On panic, this macro will print the values of the expressions.
129
+ ///
130
+ /// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by
131
+ /// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!`
132
+ /// useful for checks that are too expensive to be present in a release build
133
+ /// but may be helpful during development.
134
+ ///
135
+ /// # Example
136
+ ///
137
+ /// ```
138
+ /// let a = 3i;
139
+ /// let b = 1i + 2i;
140
+ /// debug_assert_eq!(a, b);
141
+ /// ```
61
142
#[ macro_export]
62
143
macro_rules! debug_assert_eq {
63
- ( $( $a: tt) * ) => ( {
64
- if cfg!( not( ndebug) ) {
65
- assert_eq!( $( $a) * ) ;
66
- }
67
- } )
144
+ ( $( $arg: tt) * ) => ( if cfg!( not( ndebug) ) { assert_eq!( $( $arg) * ) ; } )
68
145
}
69
146
70
- /// Runtime assertion, disableable at compile time with `-- cfg ndebug`
147
+ # [ cfg( stage0 ) ]
71
148
#[ macro_export]
72
- macro_rules! debug_assert {
73
- ( $( $arg : tt ) * ) => ( if cfg! ( not ( ndebug ) ) { assert! ( $ ( $arg ) * ) ; } )
149
+ macro_rules! try {
150
+ ( $e : expr ) => ( match $e { Ok ( e ) => e , Err ( e ) => return Err ( e ) } )
74
151
}
75
152
76
153
/// Short circuiting evaluation on Err
154
+ ///
155
+ /// `libstd` contains a more general `try!` macro that uses `FromError`.
156
+ #[ cfg( not( stage0) ) ]
77
157
#[ macro_export]
78
158
macro_rules! try {
79
- ( $e: expr) => ( match $e { Ok ( e) => e, Err ( e) => return Err ( e) } )
159
+ ( $e: expr) => ( {
160
+ use $crate:: result:: Result :: { Ok , Err } ;
161
+
162
+ match $e {
163
+ Ok ( e) => e,
164
+ Err ( e) => return Err ( e) ,
165
+ }
166
+ } )
80
167
}
81
168
82
- /// Writing a formatted string into a writer
169
+ /// Use the `format!` syntax to write data into a buffer of type `&mut Writer`.
170
+ /// See `std::fmt` for more information.
171
+ ///
172
+ /// # Example
173
+ ///
174
+ /// ```
175
+ /// # #![allow(unused_must_use)]
176
+ ///
177
+ /// let mut w = Vec::new();
178
+ /// write!(&mut w, "test");
179
+ /// write!(&mut w, "formatted {}", "arguments");
180
+ /// ```
83
181
#[ macro_export]
84
182
macro_rules! write {
85
183
( $dst: expr, $( $arg: tt) * ) => ( ( & mut * $dst) . write_fmt( format_args!( $( $arg) * ) ) )
86
184
}
87
185
88
- /// Writing a formatted string plus a newline into a writer
186
+ /// Equivalent to the `write!` macro, except that a newline is appended after
187
+ /// the message is written.
89
188
#[ macro_export]
189
+ #[ stable]
90
190
macro_rules! writeln {
91
191
( $dst: expr, $fmt: expr $( $arg: tt) * ) => (
92
192
write!( $dst, concat!( $fmt, "\n " ) $( $arg) * )
93
193
)
94
194
}
95
195
196
+ /// A utility macro for indicating unreachable code.
197
+ ///
198
+ /// This is useful any time that the compiler can't determine that some code is unreachable. For
199
+ /// example:
200
+ ///
201
+ /// * Match arms with guard conditions.
202
+ /// * Loops that dynamically terminate.
203
+ /// * Iterators that dynamically terminate.
204
+ ///
205
+ /// # Panics
206
+ ///
207
+ /// This will always panic.
208
+ ///
209
+ /// # Examples
210
+ ///
211
+ /// Match arms:
212
+ ///
213
+ /// ```rust
214
+ /// fn foo(x: Option<int>) {
215
+ /// match x {
216
+ /// Some(n) if n >= 0 => println!("Some(Non-negative)"),
217
+ /// Some(n) if n < 0 => println!("Some(Negative)"),
218
+ /// Some(_) => unreachable!(), // compile error if commented out
219
+ /// None => println!("None")
220
+ /// }
221
+ /// }
222
+ /// ```
223
+ ///
224
+ /// Iterators:
225
+ ///
226
+ /// ```rust
227
+ /// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
228
+ /// for i in std::iter::count(0_u32, 1) {
229
+ /// if 3*i < i { panic!("u32 overflow"); }
230
+ /// if x < 3*i { return i-1; }
231
+ /// }
232
+ /// unreachable!();
233
+ /// }
234
+ /// ```
96
235
#[ macro_export]
97
- macro_rules! unreachable { ( ) => ( panic!( "unreachable code" ) ) }
236
+ macro_rules! unreachable {
237
+ ( ) => ( {
238
+ panic!( "internal error: entered unreachable code" )
239
+ } ) ;
240
+ ( $msg: expr) => ( {
241
+ unreachable!( "{}" , $msg)
242
+ } ) ;
243
+ ( $fmt: expr, $( $arg: tt) * ) => ( {
244
+ panic!( concat!( "internal error: entered unreachable code: " , $fmt) , $( $arg) * )
245
+ } ) ;
246
+ }
98
247
248
+ /// A standardised placeholder for marking unfinished code. It panics with the
249
+ /// message `"not yet implemented"` when executed.
250
+ #[ macro_export]
251
+ macro_rules! unimplemented {
252
+ ( ) => ( panic!( "not yet implemented" ) )
253
+ }
0 commit comments