macro_rules! ensure {
    ($cond:expr, context($($arg:tt)*) $(, $($t:tt)*)?) => { ... };
    ($cond:expr $(, $($t:tt)*)?) => { ... };
    (@internal $cond:expr, $ret:expr, tag: [$($tag:ty),* $(,)?] $(, $($t:tt)*)?) => { ... };
    (@internal $cond:expr, $ret:expr, attach: [$($a:expr),* $(,)?] $(, $($t:tt)*)?) => { ... };
    (@internal $cond:expr, $ret:expr, attach_lazy: [$($l:expr),* $(,)?] $(, $($t:tt)*)?) => { ... };
    (@internal $cond:expr, $ret:expr, ) => { ... };
}
Expand description

Check that a given expression evaluates to true, else return an error.

Ex)

ensure!(false);

First parameter is an expression that evaluates to bool. If the expression evaluates to false, the macro will return Err(Oof).

Second parameter is context(...), and is an optional second parameter. You can use this if you want to display your own context message, instead of the default assertion failed: EXPRESSION at LOCATION. Inside context(...), you can write as you do for println!.

Other optional parameters are for tagging, attach, and attach_lazy.

Ex)

struct MyTag;
struct OtherTag;

let x = 123usize;
let y = "some value";
let z = "lazy attachment";

ensure!(
  false,
  context("custom context with value {:?}", x),
  tag: [MyTag, OtherTag],
  attach: [&y, "attachment", Instant::now()],
  attach_lazy: [|| format!("context {}", &z)]
);