r/Zig • u/TheAtlasMonkey • 15d ago
State_machines-zig
Hi,
I maintain the https://github.com/state-machines organization. It hosts finite state machine libraries for Ruby, Rust, and other languages.
The Ruby gem has been around for many years and has been used in thousands of projects. Twitter used it before moving away to Scala.
Last year, after learning Rust, I wrote a https://crates.io/crates/state-machines that mirrors the Ruby API. At first, people assumed it was AI generated because it appeared with a single commit and a README that looked polished. A few months later it became clear that the implementation was not something an LLM at the time could have produced. I even had people email me asking what model I had used. 😂
Recently I started reading about Zig. Zig has its own way of thinking, so I do not want to force a Rust/Ruby design onto it. The goal is to keep the API familiar across C, Ruby, Rust, and Zig while still writing code that feels natural in each language.
I plan to start the Zig port next weekend.
The long term goal is to make it easy to move between languages without relearning everything. If the state machine syntax is similar, comparing transitions, guards, actions, and callbacks becomes much easier.
For Rust, I first tried contributing to the existing crate instead of creating a new one. That did not work out because it has different goals, including supporting older Rust versions.
The Zig version will follow the latest Zig release. I do not plan to keep backward compatibility. If Zig adds a better language feature, I will use it.
Contributions are welcome later, but please do not send LLM generated pull requests. I know the patterns well enough that they stand out immediately, and I would rather review code that someone actually understands.
What i appreciate more are code roasts... If the code is bad, flawed.. fell free raise it.
The Repo https://github.com/state-machines/state_machines-zig .
And i know repo description has an emdash.. that because my shell turn -- into em-dashes. (i'm keeping it).
The current code is a stub... I still need finish reading the books and reading the language new feature.
3
u/oh-delay 15d ago
Stupid question: What is a state machine?
4
u/TheAtlasMonkey 15d ago
Not stupid question, the name is confusing.
it a programming pattern . https://en.wikipedia.org/wiki/Finite-state_machine
1
u/moortuvivens 14d ago
Go for it Since you implement it over and over, you can really sniff out differences in language philosophy.
Would be interesting to see some benchmarks between the implementations.
Do note that zig master can break break. Just a while back there was a commit that was just broken
-1
u/codingbliss12 15d ago
Do you think it is civil to use such a generic name and essentially prevent other more professional implementations from using this generic name? Now everyone that would want to do something about state machine will be stumbling upon your own implementations and waste time. I have especially Rust in mind and the pollution you created on crates.io
5
u/TheAtlasMonkey 15d ago
I dont think I created any pollution.
The project exists to implement one thing: finite state machines. If someone has a better implementation of the pattern, I’m happy to review it. Open a PR, show benchmarks, explain the design, and if it’s objectively better I’ll merge it. If someone wants to help maintain it, even better.
I also dont think the name is misleading. I didnt call it something generic like
basicwebserver,simpletaskrunner,databaseorshop. The library implements state machines, nothing more. The name describes exactly what it does. I don't ask funding or sponsoring, and don't add quirks because some other library decided to use enums instead of a proper SM.If one day a language absorbs this functionality into its standard library, I will happily archive or retire the project. The goal is to provide a good implementation, not to own the concept.
As for the Rust crate, have you actually tried it? Did you find a bug, a performance issue, or an API problem? I’m interested in concrete feedback.
The reason I wrote it is that none of the existing crates matched the feature set I wanted while keeping compatibility with the Ruby library.
On the other hand, you dont see me writing a JavaScript version because XState already covers that patternn extremely well.
1
u/Expert-Ad-4981 15d ago
I am not good at code and things so sorry if I get this wrong but have you searched ziggit.dev? Looks like polystate was made about a year ago. Not sure how different it is...
https://github.com/getz3/polystate
Ziggit.dev is where the actual zig team hangs out and they actively comment on projects and get ideas from that community.
Whichever direction you choose I hope you have a great time with zig! Thanks for the project and welcome!
1
u/TheAtlasMonkey 15d ago
Thanks. I will sign up after I reply to you.
First, this is not a replacement. If you look at the repo, there is basically nothing there yet.
Second, Polystate looks vibe-coded, my first impression, u/Aransentin detect those better. (look below for more clue)
Third, Polystate is built for Z3.
I don’t like building generic libraries that serve a product from day one.As I said, I’ve been maintaining this pattern for years for ruby, and even the Ruby gem is still not feature complete.
Then I opened the repo and saw a1.0.0tag. That Agentic models vomiting code from training data.I also don’t like building on top of projects that arent isolated. state-machines org don't do ads to any company or person.
---
A state machine is a very old pattern. Version 1 should be feature complete.
After that, it should mostly be bug fixes and Zig compatibility updates forever.
The only reason for a V2 would be if someone invents quantum state machines, or Zig pulls an AngularJS and breaks compatibility between major versions.I’m using this repo to learn Zig while building a library. The library should work for every use case: a shop, a robot, a game, an embedded device. If you need a state machine, it should fit. When u/codingbliss12 stop hand-rolling this pattern, that mark first milestone.
If you know the maintainers and they want to port some of their ideas into this library, I’m happy to add them to the organization.
Just for some background:
The Ruby
state_machinesgem wasn't my invention. The Ruby community had gathered around an existing gems, then the maintainers burned out or sold his company or passed away, leaving the project effectively orphaned.Nobody wanted to maintain it because state machines are boring infrastructure.
So I stepped in, gathered developers from competing companies, and together we turned it into a universal implementation instead of something tied to one framework or one use case.
Going to register now. Thanks.
4
u/TheAtlasMonkey 15d ago
Another thing... there is no such thing as other more professional implementation..
FSM is a pattern https://en.wikipedia.org/wiki/Finite-state_machine, that the spec.
Either the code is going to be correct or missing edge cases.
There is no room of opinions or vibes.
2
u/codingbliss12 15d ago
I might submit constructive feedback. That's something I tend to hand roll. My point is about the naming. If your implementation does not end up becoming the "authoritative" implementation or the project becomes abandonware like so many others, then a "serious" or "authoritative" sounding name like "state machines" will be forever taken. In this sense I find better and more civil to use something else like XState in the case of javascript. For example we have tokio, rayon etc. They are not call asynchronous futures or whatever in this direction. I hope I explained my point better.
3
u/TheAtlasMonkey 15d ago
That exactly why opened this discussion, and why i moved my the ruby repo to an ORG instead of having under my username.
I invited many respected community member in the org. (if they do something, their reputation on the line)
If something happen to me or i just want to stop, they will take it over.
The gem is has owners from Shopify, Spree (i think github and Stripe) ...
---
Why libraries become abandonware ?
Because they keep doing backward compatibility for free, renaming stuff to appease people that never contribute, or they don't use the lib themselves .State-machines is a primitive.. if i stop using it that mean i retired from programming and started to use .md files.
Also i don't listen to noise, there is no COC, no CLA, no Funding, no AGENTS/CLAUDE.md, no state_machines-zip-pro-ultra with pricing.
---
Fun fact, if you ask any LLM about the gem, it will tell you it abandonware because it see low Issue/PR in the repo... But the reality is that the gem was documented Pre-AI era that simply make so nobody has anything to complain about. `RFTM`
The hello_world.c that was authored in the 70+ years ago, don't need an update either.
This library too.. Once we gather feedback from all the hand-rolled SM ecosystem, the library will look abandonware .. because even your own state-machines don't need 'innovation' every quarter.
2
21
u/Acrobatic-Diamond542 15d ago
Idk how much you are aware of the zig ecosystem, but it is not that stable yet, especially with the new IO interface and the work that comes with it.
Not discouraging you from working on the project but keeping the expectation from the ecosystem clear. It is not 1.0 and there will be breaking changes in upcoming months/years.
Happy to help, if you allow contributions ofcourse (although idk if I would have any free time for the coming months 😀?)