Skip to content

Commit

Permalink
Merge pull request #18 from greymattergames/fix/ensure-once
Browse files Browse the repository at this point in the history
Fix ensure expressions evaluated after `once!` call
  • Loading branch information
girtonman authored Nov 21, 2024
2 parents bc3f319 + 8e1cf03 commit b61445a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ fn main() {
for i in 0..5 {
ensure!(false);
ensure!(false, "ensure messages can be logged during development");
ensure!(false, "ensure will only break once per false result");
ensure!(i % 2 == 0, "ensure messages can be formatted {}", i,);
ensure_always!(i % 2 == 0);

ensure_always!(false);
ensure_always!(false, "ensure_always will happen multiple times");
ensure_always!(false, "ensure_always messages can be formatted {}", i);

fail!("fail! will happen only once");
fail!("fail! can also format output - i was: {}", i);
fail!("fail! can also format output {}", i);

fail_always!("fail_always! will happen multiple times");

try_some_option(None);
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,17 @@ macro_rules! ensure {
#[cfg(all(debug_assertions, feature = "enable"))]
macro_rules! ensure {
($expression: expr) => {
$crate::_internal::_once!($crate::ensure_always!($expression))
if !$expression {
$crate::_internal::_once!($crate::breakpoint!());
}
};
($expression: expr, $($argument: tt),+ $(,)?) => {
$crate::_internal::_once!($crate::ensure_always!($expression, $($argument),+))
if !$expression {
$crate::_internal::_once!({
$crate::_internal::_error!($($argument),+);
$crate::breakpoint!();
});
}
};
}

Expand Down

0 comments on commit b61445a

Please sign in to comment.