From 5d184aaf30995f3f048114b72c9d32fc8a39cb92 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 10 May 2023 14:23:51 +0200 Subject: [PATCH] Add `try_opt` and `expect` macros --- src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 336e18751d..9f7293d81e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -546,3 +546,25 @@ impl fmt::Debug for OutOfRange { #[cfg(feature = "std")] impl std::error::Error for OutOfRange {} + +/// Workaround because `?` is not (yet) available in const context. +#[macro_export] +macro_rules! try_opt { + ($e:expr) => { + match $e { + Some(v) => v, + None => return None, + } + }; +} + +/// Workaround because `.expect()` is not (yet) available in const context. +#[macro_export] +macro_rules! expect { + ($e:expr, $m:literal) => { + match $e { + Some(v) => v, + None => panic!($m), + } + }; +}