Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit 7b7d03c

Browse files
palfreydavidbarsky
authored andcommitted
Build ensure version with a single arg (#304)
1 parent 6820ba2 commit 7b7d03c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/macros.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ macro_rules! bail {
2323
/// it does not panic.
2424
#[macro_export(local_inner_macros)]
2525
macro_rules! ensure {
26+
($cond:expr) => {
27+
if !($cond) {
28+
bail!("{}", _failure__stringify!($cond));
29+
}
30+
};
2631
($cond:expr, $e:expr) => {
2732
if !($cond) {
2833
bail!($e);
@@ -35,6 +40,14 @@ macro_rules! ensure {
3540
};
3641
}
3742

43+
#[doc(hidden)]
44+
#[macro_export]
45+
macro_rules! _failure__stringify {
46+
($($inner:tt)*) => {
47+
stringify! { $($inner)* }
48+
}
49+
}
50+
3851
/// Constructs an `Error` using the standard string interpolation syntax.
3952
///
4053
/// ```rust

tests/macro_trailing_comma.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ fn ensure() {
4343
wrap_early_return!(ensure!(false, "test {}", 4,)));
4444
}
4545

46+
#[test]
47+
fn single_arg_ensure() {
48+
assert_eq!(
49+
wrap_early_return!(ensure!(false)),
50+
Err("false".to_string()));
51+
assert_eq!(
52+
wrap_early_return!(ensure!(true == false)),
53+
Err("true == false".to_string()));
54+
assert_eq!(
55+
wrap_early_return!(ensure!(4 == 5)),
56+
Err("4 == 5".to_string()));
57+
}
58+
4659
#[test]
4760
fn format_err() {
4861
assert_eq!(

0 commit comments

Comments
 (0)