@@ -60,184 +60,6 @@ macro_rules! panic {
6060 } ) ;
6161}
6262
63- /// Ensure that a boolean expression is `true` at runtime.
64- ///
65- /// This will invoke the `panic!` macro if the provided expression cannot be
66- /// evaluated to `true` at runtime.
67- ///
68- /// # Example
69- ///
70- /// ```
71- /// // the panic message for these assertions is the stringified value of the
72- /// // expression given.
73- /// assert!(true);
74- /// # fn some_computation() -> bool { true }
75- /// assert!(some_computation());
76- ///
77- /// // assert with a custom message
78- /// # let x = true;
79- /// assert!(x, "x wasn't true!");
80- /// # let a = 3i; let b = 27i;
81- /// assert!(a + b == 30, "a = {}, b = {}", a, b);
82- /// ```
83- #[ macro_export]
84- #[ stable]
85- macro_rules! assert {
86- ( $cond: expr) => (
87- if !$cond {
88- panic!( concat!( "assertion failed: " , stringify!( $cond) ) )
89- }
90- ) ;
91- ( $cond: expr, $( $arg: tt) +) => (
92- if !$cond {
93- panic!( $( $arg) +)
94- }
95- ) ;
96- }
97-
98- /// Asserts that two expressions are equal to each other, testing equality in
99- /// both directions.
100- ///
101- /// On panic, this macro will print the values of the expressions.
102- ///
103- /// # Example
104- ///
105- /// ```
106- /// let a = 3i;
107- /// let b = 1i + 2i;
108- /// assert_eq!(a, b);
109- /// ```
110- #[ macro_export]
111- #[ stable]
112- macro_rules! assert_eq {
113- ( $left: expr , $right: expr) => ( {
114- match ( & ( $left) , & ( $right) ) {
115- ( left_val, right_val) => {
116- // check both directions of equality....
117- if !( ( * left_val == * right_val) &&
118- ( * right_val == * left_val) ) {
119- panic!( "assertion failed: `(left == right) && (right == left)` \
120- (left: `{:?}`, right: `{:?}`)", * left_val, * right_val)
121- }
122- }
123- }
124- } )
125- }
126-
127- /// Ensure that a boolean expression is `true` at runtime.
128- ///
129- /// This will invoke the `panic!` macro if the provided expression cannot be
130- /// evaluated to `true` at runtime.
131- ///
132- /// Unlike `assert!`, `debug_assert!` statements can be disabled by passing
133- /// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for
134- /// checks that are too expensive to be present in a release build but may be
135- /// helpful during development.
136- ///
137- /// # Example
138- ///
139- /// ```
140- /// // the panic message for these assertions is the stringified value of the
141- /// // expression given.
142- /// debug_assert!(true);
143- /// # fn some_expensive_computation() -> bool { true }
144- /// debug_assert!(some_expensive_computation());
145- ///
146- /// // assert with a custom message
147- /// # let x = true;
148- /// debug_assert!(x, "x wasn't true!");
149- /// # let a = 3i; let b = 27i;
150- /// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
151- /// ```
152- #[ macro_export]
153- #[ stable]
154- macro_rules! debug_assert {
155- ( $( $arg: tt) * ) => ( if cfg!( not( ndebug) ) { assert!( $( $arg) * ) ; } )
156- }
157-
158- /// Asserts that two expressions are equal to each other, testing equality in
159- /// both directions.
160- ///
161- /// On panic, this macro will print the values of the expressions.
162- ///
163- /// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by
164- /// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!`
165- /// useful for checks that are too expensive to be present in a release build
166- /// but may be helpful during development.
167- ///
168- /// # Example
169- ///
170- /// ```
171- /// let a = 3i;
172- /// let b = 1i + 2i;
173- /// debug_assert_eq!(a, b);
174- /// ```
175- #[ macro_export]
176- macro_rules! debug_assert_eq {
177- ( $( $arg: tt) * ) => ( if cfg!( not( ndebug) ) { assert_eq!( $( $arg) * ) ; } )
178- }
179-
180- /// A utility macro for indicating unreachable code.
181- ///
182- /// This is useful any time that the compiler can't determine that some code is unreachable. For
183- /// example:
184- ///
185- /// * Match arms with guard conditions.
186- /// * Loops that dynamically terminate.
187- /// * Iterators that dynamically terminate.
188- ///
189- /// # Panics
190- ///
191- /// This will always panic.
192- ///
193- /// # Examples
194- ///
195- /// Match arms:
196- ///
197- /// ```rust
198- /// fn foo(x: Option<int>) {
199- /// match x {
200- /// Some(n) if n >= 0 => println!("Some(Non-negative)"),
201- /// Some(n) if n < 0 => println!("Some(Negative)"),
202- /// Some(_) => unreachable!(), // compile error if commented out
203- /// None => println!("None")
204- /// }
205- /// }
206- /// ```
207- ///
208- /// Iterators:
209- ///
210- /// ```rust
211- /// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
212- /// for i in std::iter::count(0_u32, 1) {
213- /// if 3*i < i { panic!("u32 overflow"); }
214- /// if x < 3*i { return i-1; }
215- /// }
216- /// unreachable!();
217- /// }
218- /// ```
219- #[ macro_export]
220- #[ unstable = "relationship with panic is unclear" ]
221- macro_rules! unreachable {
222- ( ) => ( {
223- panic!( "internal error: entered unreachable code" )
224- } ) ;
225- ( $msg: expr) => ( {
226- unreachable!( "{}" , $msg)
227- } ) ;
228- ( $fmt: expr, $( $arg: tt) * ) => ( {
229- panic!( concat!( "internal error: entered unreachable code: " , $fmt) , $( $arg) * )
230- } ) ;
231- }
232-
233- /// A standardised placeholder for marking unfinished code. It panics with the
234- /// message `"not yet implemented"` when executed.
235- #[ macro_export]
236- #[ unstable = "relationship with panic is unclear" ]
237- macro_rules! unimplemented {
238- ( ) => ( panic!( "not yet implemented" ) )
239- }
240-
24163/// Use the syntax described in `std::fmt` to create a value of type `String`.
24264/// See `std::fmt` for more information.
24365///
0 commit comments