Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
SorteKanin ( SorteKanin@feddit.dk )  to Rust@programming.dev · 9 months ago

Introducing crabtime, a novel way to write Rust macros

ferrisoft.com

external-link
message-square
6
link
fedilink
21
external-link

Introducing crabtime, a novel way to write Rust macros

ferrisoft.com

SorteKanin ( SorteKanin@feddit.dk )  to Rust@programming.dev · 9 months ago
message-square
6
link
fedilink
Ferrisoft
ferrisoft.com
external-link
alert-triangle
You must log in or # to comment.
  • jenesaisquoi ( jenesaisquoi@feddit.org ) 
    link
    fedilink
    English
    arrow-up
    2
    ·
    9 months ago

    Doesn’t support derive macros, unfortunately.

  • Vorpal ( Vorpal@programming.dev ) 
    link
    fedilink
    arrow-up
    2
    ·
    9 months ago

    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.

    • SorteKanin ( SorteKanin@feddit.dk ) OP
      link
      fedilink
      arrow-up
      1
      ·
      9 months ago

      Might not be new but it’s not been posted here before :)

  • MoSal ( MoSal@programming.dev ) 
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    9 months ago

    (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,)*);
        };
        () => {};
    }
    
    • SorteKanin ( SorteKanin@feddit.dk ) OP
      link
      fedilink
      arrow-up
      2
      ·
      9 months ago

      Not nearly as readable though

      • MoSal ( MoSal@programming.dev ) 
        link
        fedilink
        arrow-up
        4
        ·
        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.

Rust@programming.dev

rust@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !rust@programming.dev

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 8 users / day
  • 46 users / week
  • 177 users / month
  • 757 users / 6 months
  • 118 local subscribers
  • 8.34K subscribers
  • 1.28K Posts
  • 3.78K Comments
  • Modlog
  • mods:
  • snowe ( snowe@programming.dev ) 
  • Ategon ( Ategon@programming.dev ) 
  • EdTheLegendary ( EdTheLegendary@programming.dev ) 
  • torcherist ( torcherist@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code