about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorAshley Williams <ashley666ashley@gmail.com>2016-07-27 10:47:19 -0700
committerAshley Williams <ashley666ashley@gmail.com>2016-09-21 14:38:26 +0200
commit3d8d55787b2c0f5b1aba01b08da13bf0d612818c (patch)
treea87b495fd37b6bcff42898432ca45bf54d7cd90b /src/libcore
parentc772948b687488a087356cb91432425662e034b9 (diff)
downloadrust-3d8d55787b2c0f5b1aba01b08da13bf0d612818c.tar.gz
rust-3d8d55787b2c0f5b1aba01b08da13bf0d612818c.zip
add assert_ne and debug_assert_ne macros
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/macros.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs
index f29a49dd5fe..99c24e4c48d 100644
--- a/src/libcore/macros.rs
+++ b/src/libcore/macros.rs
@@ -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.
 ///