Skip to content
Myr

Merge pull request #129 from dtolnay/ensure

Before adoption: this commit came in with the history the project adopted, so no landing records it.

1-argument `ensure!`

authored by David Tolnay · committed by GitHub · parents 0c0e41f, 6161f7d · Browse files · 2 files+12 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+12 added−0 removed

  • tests:+9 added−0 removed
  • other:+3 added−0 removed

2 files+12 added−0 removed

modified:src/macros.rs+3 added
111 unmodified lines
112/// # }113/// ```114#[macro_export]115macro_rules! ensure {116 ($cond:expr $(,)?) => {117 $crate::ensure!($cond, concat!("Condition failed: `", stringify!($cond), "`"))118 };119 ($cond:expr, $msg:literal $(,)?) => {120 if !$cond {121 return $crate::private::Err($crate::anyhow!($msg));122 }
57 unmodified lines
modified:tests/test_macros.rs+9 added
30 unmodified lines
31 ensure!(v + v == 1, "This is not correct, v: {}", v);32 Ok(())33 };34 assert!(f().is_err());3536 let f = || {37 ensure!(v + v == 1);38 Ok(())39 };40 assert_eq!(41 f().unwrap_err().to_string(),42 "Condition failed: `v + v == 1`",43 );44}