jack: (Default)
[personal profile] jack
I did some experimenting with rust macros. The idea is to make a rich assert that displays the values of any variables and expressions used in the assert condition (with some useful default omitting redundant ones).

It looks like writing this in the source:


...
rsst!( two.pow(2+3+5)+1 == 1025 ); // Passes. Execution continues.
rsst!( two.pow(2+3+5)+1 == 1023 ); // Panics. See below.


And getting this output:


$ cargo run
...
Assertion failed: two.pow(2+3+5)+1 == 1023
two.pow(2 + 3 + 5) + 1: 1025
two.pow(2 + 3 + 5): 1024
two: 2
2 + 3 + 5: 10


There's more info including functioning examples at https://github.com/CartesianDaemon/assrt

It was interesting to learn what was possible. As people intimated, rust macros were very powerful and flexible, but very hard to write.

I know this will probably never go anywhere from here, but I wanted to try what it looked like. I assume the same functionality exists, although I didn't find it immediately. I didn't try to publish the crate yet because it's not really functional, although I'll see if it works in my projects.

I am curious how much people think that's cool, how much people think that's pointless, and how much people think that's horrific. I always thought Catch.hpp which includes a similar thing in C++ using macros with horrific class magic hidden inside was really useful because it's presumably horrific to write the library, but perfectly seamless to use the library.

I followed tradition and hesitated a lot over choosing any names. I wanted something that looked like "assert" but wasn't taken. Knowing people may take objection I seriously considered "asst" or something else with "ass" centrally. For now it's "assrt" for the crate and "rsst" or "csst" for the macros.

Active Recent Entries