Stupid Rust Tricks

Enforcing deadlines with a macro

Rust has a really powerful macro system. You can use it to do great things safely… or you can have fun with it and quickly prototype coding productivity features. I chose the latter.

todo-macro is a procedural Rust macro that stops compiling after a user-specified deadline. It’s like TODO comments, but with teeth. It’s probably best explained with an example:

Using todo-macro

It’s January 1, 2020. I’m working on some Rust code that compiles, but it’s not quite ready to ship.

I want to take a break, but I know myself – I’ll probably forget about the deficiency. I could add a TODO comment, but that depends on me actively searching for TODO comments next time I open the project.

To save me from myself, I add a quick todo macro with a deadline of January 2 (in ISO 8601 format):

// Implement the timeout handling
todo!("2020-01-02")

That compiles for now, but as soon as the deadline is passed (i.e. our system clock returns Jan 3), builds start failing:

error: proc macro panicked
 --> src/main.rs:5:5
  |
5 |     todo!("2020-01-02");
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: Tsk tsk. You missed your deadline.

Think of todo-macro as a reminder that actively forces your future self to deal with a problem.

Obviously, don’t use this in real projects unless you’re really comfortable with non-deterministic builds (I’m not). But still, wouldn’t it be nice to have a (safe) feature like this in your favourite IDE?

headshot

Cities & Code

Things that don't quite fit in 280 characters.

Top Categories

View all categories

About

I'm a software engineer in Vancouver, Canada. I'm interested in databases, urban planning, computing history, and whatever else catches my fancy.

Learn More / Contact me