Macro konst::result::map

source ·
macro_rules! map {
    ($res:expr, |$param:pat_param| $expr:expr $(,)?) => { ... };
    ($opt:expr, | $($anything:tt)* ) => { ... };
    ($res:expr, $function:expr $(,)?) => { ... };
}
Expand description

A const equivalent of Result::map

§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!(Res::Ok(3), |x| x + 2),
    result::map!(Res::Err(8), |_| loop{}),

    // You can also pass functions
    result::map!(Res::Ok(16), add_34),
    result::map!(Res::Err(55), add_34),
];

assert_eq!(ARR, &[Ok(5), Err(8), Ok(50), Err(55)]);

const fn add_34(n: u32) -> u32 {
    n + 34
}