Skip to main content

jiff_core/
macros.rs

1#![allow(unused_macros, unused_imports)]
2
3/*
4/// Unwrap an `Option<T>` in a `const` context.
5///
6/// If it fails, panics with the given message.
7macro_rules! unwrap {
8    ($val:expr, $msg:expr$(,)?) => {
9        match $val {
10            Some(val) => val,
11            None => panic!($msg),
12        }
13    };
14}
15*/
16
17/// Unwrap a `Result<T, E>` in a `const` context.
18///
19/// If it fails, panics with the given message.
20macro_rules! unwrapr {
21    ($val:expr, $msg:expr$(,)?) => {
22        match $val {
23            Ok(val) => val,
24            Err(_) => panic!($msg),
25        }
26    };
27}
28
29/// Get `T` out of `Result<T, E>` or return `Err(e)` in a `const` context.
30///
31/// No conversions are performed. This only works when used in a function
32/// that itself returns `Result<..., E>`.
33macro_rules! ctry {
34    ($val:expr) => {
35        match $val {
36            Ok(val) => val,
37            Err(err) => return Err(err),
38        }
39    };
40}
41
42/// Convert the given error to a `RangeError` and return it.
43///
44/// `val` must be one of `RangeError`, `BoundsError` or `SpecialBoundsError`.
45/// In particular, `val.into_range_error()` is called to convert to a
46/// `RangeError`.
47///
48/// This only works when used in a function
49/// that itself returns `Result<..., RangeError>`.
50macro_rules! rbail {
51    ($val:expr) => {
52        return Err($val.into_range_error())
53    };
54}
55
56/// Get `T` out of `Result<T, E>` or return `Err(e)` in a `const` context.
57///
58/// `E` must be one of `RangeError`, `BoundsError` or `SpecialBoundsError`. In
59/// particular, `E::into_range_error` is called to convert to a `RangeError`.
60///
61/// This only works when used in a function
62/// that itself returns `Result<..., RangeError>`.
63macro_rules! rtry {
64    ($val:expr) => {
65        match $val {
66            Ok(val) => val,
67            Err(err) => crate::macros::rbail!(err),
68        }
69    };
70}
71
72pub(crate) use {ctry, rbail, rtry, unwrapr};