Crabtime: Zig’s Comptime in Rust

crates.io

466 points by klaussilveira a month ago


weinzierl - a month ago

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.

nindalf - a month ago

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...

stared - a month ago

I love these kinds of acknowledgements, as they not only show gratitude, but also give a glimpse into the collaborative, creative process:

> We would like to extend our heartfelt gratitude to the following individuals for their valuable contributions to this project:

> timonv – For discovering and suggesting the brilliant name for this crate. Read more about it here (https://www.reddit.com/r/rust/comments/1j42fgi/comment/mg6pw...).

> Polanas – For their invaluable assistance with testing, design, and insightful feedback that greatly improved the project.

> Your support and contributions have played a vital role in making this crate better—thank you!

mplanchard - a month ago

At first I was like wait this looks just like eval_macro, which I discovered a couple of weeks ago. Looks like it is just renamed! The new name is great, congrats on the improved branding :)

vlovich123 - a month ago

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

norman784 - a month ago

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.

jpgvm - a month ago

This is cursed in the most wonderful way, kudos.

dymk - a month ago

This looks cool, but how it impacts project compile times? They talk about how caching works for multiple invocations of the same macro with different arguments. It would be nice to have some approximate numbers for how long it takes to create, compile, and execute one of its generated projects.

codedokode - a month ago

The problem with macros in Rust is that they have full access to your computer. This is literally an invitation for exploitation. I think we will see the attacks based on this vulnerability once Rust becomes more popular.

- a month ago
[deleted]
the__alchemist - a month ago

Deos anyone have an example beyond the one on that page? I'm having a hard time understanding.

So, I'm interested in some metaprogramming right now. I'm setting up Vec3 SIMD types, and it requires a lot of repetition to manage the various variants: f32::Vec3x8, f64::Vec3x16 etc that are all similar internally. This could be handled using traditional macros, procedural macros, or something called "code gen", which I think is string manipulation of code. Could I use crabtime to do this instead? Should I?

KolmogorovComp - a month ago

A better link https://docs.rs/crabtime/1.1.1/crabtime/

jgalt212 - a month ago

Does anyone else find macros make it hard to grep a code base? This does seem like something semantic grep could solve, but I'm unaware of any semantic grep macros use cases.

- a month ago
[deleted]
cchance - a month ago

I don't know if i'm too dumb i never understood what comptime gives, i get what macros are for but how does something like crabtime improve things ?

Does this basically allow us to write normal rust code instead of procmacros, with even fewer constraints?

jedisct1 - a month ago

It's nothing like Zig's comptime.

bachmidt1007 - a month ago

[flagged]

bachmidt1007 - a month ago

[flagged]

cyber1 - a month ago

No, this is not Zig comptime at all. Zig's comptime work is on another level, and it's amazing.

tdhz77 - a month ago

Moments like this realize I don’t understand programming fundamentals. Imposter syndrome sits in and I realize that my lack of formal education is costing me.