Skip to content

Commit

Permalink
Add try_opt and expect macros
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed May 23, 2023
1 parent d331f07 commit 5d184aa
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
};
}

0 comments on commit 5d184aa

Please sign in to comment.