(I’ve updated this pattern, since a lot has changed since 2020. The
recommendations here should be ready for the Rust 2024 edition, and are closer
to correct in a post-pointer-provenance world.)
Here’s another useful Rust pattern. Like the Typestate Pattern
before it, I wrote this because I haven’t seen the sort of obsessively nerdy
writeup that I wanted to read. And, as with the Typestate Pattern, I didn’t
invent this — I’m merely documenting and generalizing it.
Since it looks like some folks have been actually reading my blog, I’ve made a
pass over the site, looking for accessibility problems. I have increased visual
contrast and made links within articles slightly more obvious. The comments in
code samples are still under the WCAG recommended constrast level, but they’re
generated by a third party syntax highlighting library, so fixing them is more
involved.
Please let me know if you have any difficulty using the site!
I’ve been studying WebAssembly recently, which has included porting some of my
m4vga graphics demos. I started with the Rust and WebAssembly
Tutorial, which has you use fancy tools like wasm-pack,
wasm-bindgen, webpack, and npm to produce a Rust-powered webpage.
And that’s great! But I want to know how things actually work, and those tools
put a lot of code between me and the machine.
In this post, I’ll show how to create a simple web graphics demo using none of
those tools — just hand-written Rust, JavaScript, and HTML. There will be
no libraries between our code and the platform. It’s the web equivalent of bare
metal programming!
The resulting WebAssembly module will be less than 300 bytes. That’s about
the same size as the previous paragraph.
The typestate pattern is an API design pattern that encodes information about
an object’s run-time state in its compile-time type. In particular, an API
using the typestate pattern will have:
Operations on an object (such as methods or functions) that are only available
when the object is in certain states,
A way of encoding these states at the type level, such that attempts to use
the operations in the wrong state fail to compile,
State transition operations (methods or functions) that change the
type-level state of objects in addition to, or instead of, changing run-time
dynamic state, such that the operations in the previous state are no longer
possible.
This is useful because:
It moves certain types of errors from run-time to compile-time, giving
programmers faster feedback.
It interacts nicely with IDEs, which can avoid suggesting operations that are
illegal in a certain state.
It can eliminate run-time checks, making code faster/smaller.
This pattern is so easy in Rust that it’s almost obvious, to the point that
you may have already written code that uses it, perhaps without realizing it.
Interestingly, it’s very difficult to implement in most other programming
languages — most of them fail to satisfy items number 2 and/or 3 above.
I haven’t seen a detailed examination of the nuances of this pattern, so here’s
my contribution.
If this isn’t your first time visiting my blog, you may recall that I’ve spent
the past several years building an elaborate microcontroller graphics
demo using C++.
Over the past few months, I’ve been rewriting it — in Rust.
This is an interesting test case for Rust, because we’re very much in C/C++’s
home court here: the demo runs on the bare metal, without an operating system,
and is very sensitive to both CPU timing and memory usage.
The results so far? The Rust implementation is simpler, shorter (in lines of
code), faster, and smaller (in bytes of Flash) than my heavily-optimized C++
version — and because it’s almost entirely safe code, several types of
bugs that I fought regularly, such as race conditions and dangling pointers, are
now caught by the compiler.
It’s fantastic. Read on for my notes on the process.