Macro const_eval_select

Source
pub(crate) macro const_eval_select {
    (
        @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
        if const
            $(#[$compiletime_attr:meta])* $compiletime:block
        else
            $(#[$runtime_attr:meta])* $runtime:block
    ) => { ... },
    (
        @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
        #[noinline]
        if const
            $(#[$compiletime_attr:meta])* $compiletime:block
        else
            $(#[$runtime_attr:meta])* $runtime:block
    ) => { ... },
    (
        @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty),* $(,)? } $( -> $ret:ty )? :
        if const
            $(#[$compiletime_attr:meta])* $compiletime:block
        else
            $(#[$runtime_attr:meta])* $runtime:block
    ) => { ... },
}
🔬This is a nightly-only experimental API. (core_intrinsics)
Expand description

A macro to make it easier to invoke const_eval_select. Use as follows:

ⓘ
const_eval_select!(
    @capture { arg1: i32 = some_expr, arg2: T = other_expr } -> U:
    if const #[attributes_for_const_arm] {
        // Compile-time code goes here.
    } else #[attributes_for_runtime_arm] {
        // Run-time code goes here.
    }
)

The @capture block declares which surrounding variables / expressions can be used inside the if const. Note that the two arms of this if really each become their own function, which is why the macro supports setting attributes for those functions. The runtime function is always markes as #[inline].

See const_eval_select() for the rules and requirements around that intrinsic.