all 5 comments

sorted by: hot top controversial new old
[–] 3 points 9 months ago (1 child)

Uh, this blog says "introducing", but this is hardly new. I have seen this crate around for a while, and https://crates.io/crates/crabtime/versions corroborates that.

Is this an old blog? I can't find a date on it.

  • source
  • hideshow 1 child comment
  • [–] 2 points 9 months ago* (last edited 9 months ago) (2 children)

    (slightly edited from initial version)

    #![feature(macro_metavar_expr_concat)]
    #![feature(macro_metavar_expr)]
    
    macro_rules! gen_enums {
        ([$($name:ident)+]) => {
            gen_enums!(${count($name)}: [$($name)+]);
        };
        ($n:literal: [$($name:ident)+]) => {
            enum ${concat(Position, $n)} { $($name,)+ }
        };
        ($name:ident, $($tail:ident,)*) => {
            gen_enums!([$name $($tail)*]);
            gen_enums!($($tail,)*);
        };
        () => {};
    }
    
  • source
  • hideshow 2 child comments
  • [–] [S] 2 points 9 months ago (1 child)

    Not nearly as readable though

  • source
  • parent
  • hideshow 1 child comment
  • [–] 4 points 9 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.

  • source
  • parent