Live data from Hacker News

State Machines in Rust

blog.yoshuawuyts.com

31–40 of 119 posts

Re: State Machines in Rust

#31
post #4

Once upon a time I have implemented POP3 server protocol state machine in Clojure. 180 lines in total, of which 40 were FSM declaration, 140 command handling functions. Not sure I'll ever want to approach FSM in any other language, unless no other choice.

Can you perhaps point to it? Sounds interesting to read that code!

Not OP but there is tons of FSM implementations/libs in Clojure scattered around GitHub and GitLab if you search for them.

Some of them:

- https://github.com/ztellman/automat

- https://github.com/metosin/tilakone

- https://github.com/cdorrat/reduce-fsm

Re: State Machines in Rust

#32
post #30

Earlier quoted context omitted.

Can you perhaps point to it? Sounds interesting to read that code!

Yes. Can't share the whole repo (it's our internal product), but here are two files (FSM macro and FSM itself) you can explore. Let me know it that helps. https://gist.github.com/juskrey/61148c98bdde871a8d3743f54b82... https://gist.github.com/juskrey/127cf8456fc527d20ed5e244ce01...

Interesting, thanks a lot for being able to share those examples!

I think you managed to find a bug in the GitHub syntax highlighter, as https://gist.github.com/juskrey/61148c98bdde871a8d3743f54b82... has no colors after line ~108.

Re: State Machines in Rust

#33

C++20 supports enums as non-type template parameters, so I think it'd be possible to do it with enums there. Something like: enum class Color{Green, Yellow, Red}; template struct State{}; auto newState() -> State {...}; auto next(State ) -> State {...} auto next(State ) -> State {...} auto next(State ) -> State {...} int main(){ const auto state = newState(); // Green const auto state = next(state); // Yellow const a…

> C++20 supports enums as non-type template parameters, so I think it'd be possible to do it with enums there. Something like:

Even c++98 supported that already : https://gcc.godbolt.org/z/7xma_u

Re: State Machines in Rust

#34
post #24
post #4

Once upon a time I have implemented POP3 server protocol state machine in Clojure. 180 lines in total, of which 40 were FSM declaration, 140 command handling functions. Not sure I'll ever want to approach FSM in any other language, unless no other choice.

Did you iterate to a FSM solution, from many nested conditions, or use it right from the start? I'm fighting the urge to write any FSM without having the absolute certainty that it is needed. There seems no other way than by refactoring to an FSM.

I have started from already existing FSM libs (which did not fit well) and after couple of experiments understood that creating own custom FSM processor is a no-brainer with clojure/core.match

Re: State Machines in Rust

#35
post #23
post #22

Slightly OT, but: > In Germany traffic lights go green -> yellow -> red -> yellow -> green, but let's pretend they're only green -> yellow -> red -> green. is not correct if I'm not missing something major. Is there any situation where they turn yellow before turning green?

They probably mean "red-yellow", which is an intermediate "wait for it" state in many European countries, including Germany https://de.wikipedia.org/wiki/Ampel#/media/Datei:Traffic_lig...

I think I've never noticed that red-yellow in 40+ years of living in Germany. Interesting. Maybe it's a good thing I never get to drive a car these days.

Re: State Machines in Rust

#36

C++20 supports enums as non-type template parameters, so I think it'd be possible to do it with enums there. Something like: enum class Color{Green, Yellow, Red}; template struct State{}; auto newState() -> State {...}; auto next(State ) -> State {...} auto next(State ) -> State {...} auto next(State ) -> State {...} int main(){ const auto state = newState(); // Green const auto state = next(state); // Yellow const a…

> C++20 supports enums as non-type template parameters, so I think it'd be possible to do it with enums there. Something like: Even c++98 supported that already : https://gcc.godbolt.org/z/7xma_u

Good point, I suppose of course it does, since enums are just ints. But C++20 adds support for enum classes (and also variants when they can be defined as value types with a defaulted operator).

Re: State Machines in Rust

#37
post #28

For you who enjoying using state machines but wish they did even more and/or were embedded in each other (nested state machines!), check out this thing called State Charts! Here is the initial paper from David Harel: STATECHARTS: A VISUAL FORMALISM FOR COMPLEX SYSTEMS (1987) - https://www.inf.ed.ac.uk/teaching/courses/seoc/2005_2006/res... Website with lots of info and resources: https://statecharts.github.io/ And fi…

[deleted]

Re: State Machines in Rust

#39
post #28

For you who enjoying using state machines but wish they did even more and/or were embedded in each other (nested state machines!), check out this thing called State Charts! Here is the initial paper from David Harel: STATECHARTS: A VISUAL FORMALISM FOR COMPLEX SYSTEMS (1987) - https://www.inf.ed.ac.uk/teaching/courses/seoc/2005_2006/res... Website with lots of info and resources: https://statecharts.github.io/ And fi…

> (nested state machines!)

Also known as hierarchical state machines. (web searches) Oh, which are also called state charts. So yes, those!

Re: State Machines in Rust

#40

I disagree with the implementation, State should be a trait with NextState as an associated type. This makes things cumbersome when it can be a set of types, but it makes excellent use of the type system and ownership patterns of Rust. Edit: and the type state pattern http://cliffle.com/blog/rust-typestate/ As an aside, if you want to dive in with FSMs and automata theory (as well as some basic language topics) go re…

[deleted]
Post reply on HN