macro_rules! map_err {
($res:expr, |$param:pat_param| $expr:expr $(,)?) => { ... };
($opt:expr, | $($anything:tt)* ) => { ... };
($res:expr, $function:expr $(,)?) => { ... };
}
Expand description
A const equivalent of Result::map_err
ยงExample
use konst::result;
// Necessary for type inference reasons.
type Res = Result<u32, u32>;
const ARR: &[Res] = &[
// You can use a closure-like syntax to run code when the Result argument is Ok.
// `return` inside the "closure" returns from the function where this macro is called.
result::map_err!(Res::Ok(3), |_| loop{}),
result::map_err!(Res::Err(8), |x| x + 5),
// You can also pass functions
result::map_err!(Res::Ok(16), add_34),
result::map_err!(Res::Err(55), add_34),
];
assert_eq!(ARR, &[Ok(3), Err(13), Ok(16), Err(89)]);
const fn add_34(n: u32) -> u32 {
n + 34
}