• MoSal@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago
    #![feature(macro_metavar_expr_concat)]
    #![feature(macro_metavar_expr)]
    
    macro_rules! gen_enums {
        ([$($name:ident)+]) => {
            gen_enums!(${count($name)}: [$($name)+]);
        };
        ($name:ident, $($tail:ident,)+) => {
            gen_enums!([$name $($tail)+]);
            gen_enums!($($tail,)+);
        };
        ($name:ident,) => {
            gen_enums!([$name]);
        };
        ($n:literal: [$($name:ident)+]) => {
            enum ${concat(Position, $n)} { $($name,)+ }
        };
    }
    
    gen_enums!(W, Z, Y, X,);
    
      • MoSal@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        10 months ago

        It was just a quick solution showing how to do it with one macro and zero dependencies, utilizing the power of meta variables. It probably can be made nicer.

        Side Note: It’s not directly relevant/needed here, but this also shows that maybe a theoretical $reverse{} meta variable can be useful with repeated patterns, to in this case generate the variants in the expected order.