Repository: dtolnay/anyhow (rust)

Issue:
Consider allowing 1-argument `ensure!`
The `ensure!` macro currently takes a condition argument (bool) followed by format arguments for the error message.
https://docs.rs/anyhow/1.0.34/anyhow/macro.ensure.html

```rust
    ($cond:expr, $fmt:expr, $($arg:tt)*) => { ... };
```

It would be reasonable to also support 1-argument `ensure!` with only the condition argument.

```rust
ensure!(depth <= MAX_DEPTH);
```

If the condition is false, the resulting error would be produced from the `stringify` of the condition expression. This would match the API of the standard library's `assert!` macro. https://doc.rust-lang.org/std/macro.assert.html

Pull request description:
1-argument `ensure!`
Closes #126.

Patch:
diff --git a/src/macros.rs b/src/macros.rs
index c4b4c80..f050ce3 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -113,6 +113,9 @@ macro_rules! bail {
 /// ```
 #[macro_export]
 macro_rules! ensure {
+    ($cond:expr $(,)?) => {
+        $crate::ensure!($cond, concat!("Condition failed: `", stringify!($cond), "`"))
+    };
     ($cond:expr, $msg:literal $(,)?) => {
         if !$cond {
             return $crate::private::Err($crate::anyhow!($msg));


Test patch:
diff --git a/tests/test_macros.rs b/tests/test_macros.rs
index eb1503f..dd66973 100644
--- a/tests/test_macros.rs
+++ b/tests/test_macros.rs
@@ -32,4 +32,13 @@ fn test_ensure() {
         Ok(())
     };
     assert!(f().is_err());
+
+    let f = || {
+        ensure!(v + v == 1);
+        Ok(())
+    };
+    assert_eq!(
+        f().unwrap_err().to_string(),
+        "Condition failed: `v + v == 1`",
+    );
 }