Auto merge of #35074 - ashleygwilliams:assert_ne, r=steveklabnik
add debug_assert_ne + assert_ne ~~✨ work in progress, please do not merge ✨~~ fixes #35073
This commit is contained in:
commit
53f9730291
@ -119,6 +119,44 @@ macro_rules! assert_eq {
|
||||
});
|
||||
}
|
||||
|
||||
/// Asserts that two expressions are not equal to each other.
|
||||
///
|
||||
/// On panic, this macro will print the values of the expressions with their
|
||||
/// debug representations.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let a = 3;
|
||||
/// let b = 2;
|
||||
/// assert_ne!(a, b);
|
||||
/// ```
|
||||
#[macro_export]
|
||||
#[stable(feature = "assert_ne", since = "1.12.0")]
|
||||
macro_rules! assert_ne {
|
||||
($left:expr , $right:expr) => ({
|
||||
match (&$left, &$right) {
|
||||
(left_val, right_val) => {
|
||||
if *left_val == *right_val {
|
||||
panic!("assertion failed: `(left != right)` \
|
||||
(left: `{:?}`, right: `{:?}`)", left_val, right_val)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
($left:expr , $right:expr, $($arg:tt)*) => ({
|
||||
match (&($left), &($right)) {
|
||||
(left_val, right_val) => {
|
||||
if *left_val == *right_val {
|
||||
panic!("assertion failed: `(left != right)` \
|
||||
(left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
|
||||
format_args!($($arg)*))
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Ensure that a boolean expression is `true` at runtime.
|
||||
///
|
||||
/// This will invoke the `panic!` macro if the provided expression cannot be
|
||||
@ -189,6 +227,31 @@ macro_rules! debug_assert_eq {
|
||||
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
|
||||
}
|
||||
|
||||
/// Asserts that two expressions are not equal to each other.
|
||||
///
|
||||
/// On panic, this macro will print the values of the expressions with their
|
||||
/// debug representations.
|
||||
///
|
||||
/// Unlike `assert_ne!`, `debug_assert_ne!` statements are only enabled in non
|
||||
/// optimized builds by default. An optimized build will omit all
|
||||
/// `debug_assert_ne!` statements unless `-C debug-assertions` is passed to the
|
||||
/// compiler. This makes `debug_assert_ne!` useful for checks that are too
|
||||
/// expensive to be present in a release build but may be helpful during
|
||||
/// development.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let a = 3;
|
||||
/// let b = 2;
|
||||
/// debug_assert_ne!(a, b);
|
||||
/// ```
|
||||
#[macro_export]
|
||||
#[stable(feature = "assert_ne", since = "1.12.0")]
|
||||
macro_rules! debug_assert_ne {
|
||||
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_ne!($($arg)*); })
|
||||
}
|
||||
|
||||
/// Helper macro for reducing boilerplate code for matching `Result` together
|
||||
/// with converting downstream errors.
|
||||
///
|
||||
|
@ -304,8 +304,8 @@ use prelude::v1::*;
|
||||
// We want to reexport a few macros from core but libcore has already been
|
||||
// imported by the compiler (via our #[no_std] attribute) In this case we just
|
||||
// add a new crate name so we can attach the reexports to it.
|
||||
#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
|
||||
unreachable, unimplemented, write, writeln, try)]
|
||||
#[macro_reexport(assert, assert_eq, assert_ne, debug_assert, debug_assert_eq,
|
||||
debug_assert_ne, unreachable, unimplemented, write, writeln, try)]
|
||||
extern crate core as __core;
|
||||
|
||||
#[macro_use]
|
||||
|
22
src/test/run-pass/assert-ne-macro-success.rs
Normal file
22
src/test/run-pass/assert-ne-macro-success.rs
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct Point { x : isize }
|
||||
|
||||
pub fn main() {
|
||||
assert_ne!(666,14);
|
||||
assert_ne!("666".to_string(),"abc".to_string());
|
||||
assert_ne!(Box::new(Point{x:666}),Box::new(Point{x:34}));
|
||||
assert_ne!(&Point{x:666},&Point{x:34});
|
||||
assert_ne!(666, 42, "no gods no masters");
|
||||
assert_ne!(666, 42, "6 {} 6", "6");
|
||||
assert_ne!(666, 42, "{x}, {y}, {z}", x = 6, y = 6, z = 6);
|
||||
}
|
13
src/test/run-pass/assert-ne-macro-unsized.rs
Normal file
13
src/test/run-pass/assert-ne-macro-unsized.rs
Normal file
@ -0,0 +1,13 @@
|
||||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub fn main() {
|
||||
assert_ne!([6, 6, 6][..], vec![1, 2, 3][..]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user