Announcing `statig`: Hierarchical state machines for event-driven systems (using GAT’s)

270 points maxidel

Hi everyone! I’m excited to announce statig, a crate to help you write hierarchical state machines (also known as HSM’s or statecharts). These were introduced to me by the book Practical UML Statecharts in C/C++ (don’t let the UML scare you) and ever since then I’ve thinking about how to best implement them in Rust.

The most important advantage of HSM’s is that they allow you to add superstates that define shared behaviour of underlying states. As state machines grow in size, I find that this feature becomes invaluable in keeping complexity under control and avoiding state explosion. Next to that HSM’s include concepts such as entry/exit actions, state local storage and shared context, which help to avoid code repetition and in turn make it easier to maintain the system.

I went through multiple iterations for the design before arriving on something that (to me at least) feels clean and is easy to maintain. But of course I’m a bit biased and I’m curious to hear what other people think. So if this all sounds interesting to you be sure to check out the repo.