diff options
Diffstat (limited to 'src/let_else.rs')
-rw-r--r-- | src/let_else.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/let_else.rs b/src/let_else.rs index da17a68..a1627f1 100644 --- a/src/let_else.rs +++ b/src/let_else.rs @@ -21,3 +21,25 @@ right: {:?}", } pub(crate) use assume; + +/// Binds the given expression to the given pattern, or else executes +/// `unreachable!();` with a helpful panic message and returns. +macro_rules! know { + ($pattern:pat, $value:expr) => { + // The expression might change each time it's evaluated, so we + // have to bind it so that we can reuse it in the panic message. + let _value = $value; + + let $pattern = _value else { + unreachable!( + "assertion `left matches right` failed: + left: {} +right: {:?}", + stringify!($pattern), + _value + ); + }; + }; +} + +pub(crate) use know; |