Skip to content Skip to footer

5 Comments

  • Post Author
    weinzierl
    Posted March 22, 2025 at 6:38 am

    I love the logo, it is brilliant.

    Making Rust's macros easier is laudable. Purely from a user's perspective I find it especially annoying, that proc macros need their own crate, even if I understand the reasons for it.
    If I read Crabtime correctly it solves that problem, which is nice.

    That being said Crabtime looks more like compile time eval on steroids to me than an analogon to Zig's comptime.

    One (maybe the) distinguishing feature between comptime in Zig and Rust macros seems to me to be access to type information. In Zig you have it[1]
    in Rust you don't and that makes a big difference. It doesn't look like we will get that in Rust anytime soon and the projects that need it (e.g. cargo semver check) use dirty tricks (parsing RustDoc from the macro) to accomplish it. I did not see anything like that in Crabtime, but I might have missed it. At any rate, I'd expect compile time reflection for anything that claims to bring comptime to Rust.

    [1] I think, but I am not a Zig expert, so please correct me if I am wrong.

  • Post Author
    vlovich123
    Posted March 22, 2025 at 6:39 am

    Wow this is so neat. Has anyone had any experience with it / feedback? This looks so much nicer than existing macros.

  • Post Author
    norman784
    Posted March 22, 2025 at 9:09 am

    This looks nice, just yesterday I was trying to make my code more concise by using some macro_rules magic, but it was a bit more than what macro_rules can handle, so I ended up just writing the whole thing. I avoid whenever I can proc macros, I wrote my fair share of macros, but I hate them, you need to add most of the time 3 new dependencies, syn, quote and proc_macros2, that adds up to the compilation times.

    This looks worth the playing with and see if they can solve my issue, one thing I avoid as much as possible is to add unnecessary dependencies, didn’t check how many dependencies this will add overall to the project.

  • Post Author
    nindalf
    Posted March 22, 2025 at 9:25 am

    I tried the library out and it worked pretty well for me.

    I had previously written a declarative macro to generate benchmark functions [1]. It worked, but I didn't enjoy the process of getting it working. Nor did I feel confident about making changes to it.

    When I rewrote it using crabtime I found the experience much better. I was mostly writing Rust code now, something I was familiar with. The code is much more readable and customisable [2]. For example, instead of having to pass in the names of the modules each time I added a new one, I simply read the files from disk at compile time.

    To compare the two see what the code looks like in within the braces of paste!{} in the first one and crabtime::output!{} in the second one. The main difference is that I can construct the strings using Rust code and drop them in with a simple {{ str }}. With paste!, I don't know exactly what I did, but I kept messing around until it worked.

    Or compare the two loops. In the first one we have `($($year:ident {$($day:ident),+ $(,)?}),+ $(,)?)` while with crabtime we have plain Rust code – `for (year, day) in years_and_days`. I find the latter more readable.

    Overall I'm quite pleased with crabtime. Earlier I'd avoid Rust metaprogramming as much as possible, but now I'd be open to writing a macro if the situation called for it.

    [1] – https://github.com/nindalf/advent/blob/13ff13/benches/benche…

    [2] – https://github.com/nindalf/advent/blob/b72b98/benches/benche…

  • Post Author
    jpgvm
    Posted March 22, 2025 at 10:15 am

    This is cursed in the most wonderful way, kudos.

Leave a comment

In the Shadows of Innovation”

© 2025 HackTech.info. All Rights Reserved.

Sign Up to Our Newsletter

Be the first to know the latest updates

Whoops, you're not connected to Mailchimp. You need to enter a valid Mailchimp API key.