Repository: rust-lang-deprecated/failure (language: rust)

Issue:
Add single-argument version of ensure
`assert!` lets you have a single argument version that throws an error based off the expression, rather than having to generate one. It would be really useful for `ensure!` to have the same option (while keeping the existing custom error options).


Discussion / hints:

Fix patch:
diff --git a/src/macros.rs b/src/macros.rs
index decea20..c0a9dad 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -23,6 +23,11 @@ macro_rules! bail {
 /// it does not panic.
 #[macro_export(local_inner_macros)]
 macro_rules! ensure {
+    ($cond:expr) => {
+        if !($cond) {
+            bail!("{}", _failure__stringify!($cond));
+        }
+    };
     ($cond:expr, $e:expr) => {
         if !($cond) {
             bail!($e);
@@ -35,6 +40,14 @@ macro_rules! ensure {
     };
 }
 
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _failure__stringify {
+    ($($inner:tt)*) => {
+        stringify! { $($inner)* }
+    }
+}
+
 /// Constructs an `Error` using the standard string interpolation syntax.
 ///
 /// ```rust


Test patch:
diff --git a/tests/macro_trailing_comma.rs b/tests/macro_trailing_comma.rs
index 6d8b80e..012d006 100644
--- a/tests/macro_trailing_comma.rs
+++ b/tests/macro_trailing_comma.rs
@@ -43,6 +43,19 @@ fn ensure() {
         wrap_early_return!(ensure!(false, "test {}", 4,)));
 }
 
+#[test]
+fn single_arg_ensure() {
+    assert_eq!(
+        wrap_early_return!(ensure!(false)),
+        Err("false".to_string()));
+    assert_eq!(
+        wrap_early_return!(ensure!(true == false)),
+        Err("true == false".to_string()));
+    assert_eq!(
+        wrap_early_return!(ensure!(4 == 5)),
+        Err("4 == 5".to_string()));
+}
+
 #[test]
 fn format_err() {
     assert_eq!(