
A Finite State Machine in Rust (part 1)
Implementing a Finite State Machine in Rust
Background (you can skip…)
Shutup! I don’t care if you’ve never heard of any of these languages or if you think this is just “old shit”! I have programmed in many, many languages and I am quite proud of that fact. See how many you know or recognize… Fortran, Algol, PL/1, Pascal, BAL, Lisp, C, C++, Objective-C, Eiffel, SmallTalk, Ada, Ruby, Python, Rust (and I know I have omitted a few more…)
Not because it was an FSM, but because I am a lazy engineer and I hate having to do the same things over and over again and because shorter and concise is much more elegant than longer and incomprehensible… but rather because I found myself repeating the same code over and again.. yes, copy pasta but harder to read, manage, understand… that I put my very limited artistic abilites to use and wrote something to be quite proud of. And, in case you are wondering the language… the first time it was in assembler, later written in C…
It was a communication protocol that I will not mention here (because you would all laugh at me…seriously) and its basic operation was quite simple - wait for input, process the input (but depending upon previous inputs), and then do something. But, it was a ‘protocol’ - that meant you had to do something before you could do something else.
It was late at night and it was getting later. I found myself writing terrible code and worst of all… alot of it. There had to be a better way.
The better way
The Finite State Machine. That was all that I was trying to implement in code, but using too many conditionals along the way to check for out of sequence input and incorrect states.
I had written jump tables (or some refer to these as dispatch tables) before (yes, assembler and in C…) As it turns out, it is realy MUCH easier to implement in assembly language - use the offset into a table as a pointer to a function and indirectly access that function.
First, how to do this in a higher level language? I would argue that C wasn’t really that much of a higher level language. It was maybe a half step above assembly with many features of higher level languages. Then too, the number of arguments for these indirect function calls varied. Would it not require some prior knowledge of the function in order to pass the correct number of arguements? (short answer, no).
So, the code was surprising short, simple, and sweet. It was easy to read, easy to manage, and easy to test. Have you ever felt very proud of something you have done? Well, this was my accomplishment ha… I know it sounds silly, but it was.
Okay, Back to the Present
So that was in C. That was for some old communication protocol that only a handful of people still use today. I wanted to create a new version of what I did (minus the communication protocol though…) and I could have done this in a few other programming languages… but as look would have it, I had this customer… and I needed to write something for which another finite state machine would be perfect… (yes, I’m skipping the details, I don’t have permission to reveal what I did) So, to satisfy the customer request, and contribute to my own contentment and happiness, I opted to do this in Rust.
In order to keep this relatively brief…haha… yeah, I know… I’m breaking this up into parts… this was apparently the background… I’ll get to more of the good stuff starting in the next ‘installment’ haha… found here: my rust fsm
Have questions or want to share your own sysadmin story? Leave a comment below!
(I will set up comments eventually ;)