Live data from Hacker News

State Machines in Rust

blog.yoshuawuyts.com

21–30 of 119 posts

Re: State Machines in Rust

#21
post #15

Advocating for programmer ergonomics is always a good thing. And I think more people should advocate and try to design languages in such a way that the way of programming more closely resembles the actual real thing [1, 2]. As you might recall in cognitive psychology there's a specific idea that translating a problem to a more recognizable problem (or simply changing the symbols) is a good thing [3]. Having less work…

While I love Bret's work I don't think that's scalable. If you can simulate time axis, it means it can simulate only systems that can are hundreds of times smaller than system resource. E.g. can you simulate a Kubernetes swarm with thousands of different settings? It's a great learning tool, but not much else.

Bret's work is an example of the principle. Sublime its feature, for example, is scalable.

Re: State Machines in Rust

#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?

Re: State Machines in Rust

#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...

Re: State Machines in Rust

#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.

Re: State Machines in Rust

#25
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 go red -> red+yellow -> green when making the red -> green transition.

Re: State Machines in Rust

#26
post #14

Who is Yoshua Wuyts?

For starters, he's a significant contributor to the Rust community and ecosystem. As you can see, he shares knowledge and experiences, which is a rarety among the more experienced crowd. He actually wants to help others beyond "codeblazing".

Re: State Machines in Rust

#27

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…

Rust will also support it one day, as part of the const generics feature. Partial support is there behind a feature flag.

https://play.rust-lang.org/?version=nightly&mode=debug&editi...

    #![feature(const_generics)]
    
    #[derive(PartialEq, Eq)] // enums used as const generics must be Eq
    enum Color { Green, Yellow, Red }
    
    struct State;
    
    impl State { fn next(self) -> State { State } }
    impl std::fmt::Debug for State { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.write_str("green") } }
    
    impl State { fn next(self) -> State { State } }
    impl std::fmt::Debug for State { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.write_str("yellow") } }
    
    impl State { fn next(self) -> State { State } }
    impl std::fmt::Debug for State { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.write_str("red") } }
    
    impl State { fn new() -> Self { State } }
    
    fn main() {
        let state = dbg!(State::::new());
        let state = dbg!(state.next());
        let state = dbg!(state.next());
        let state = dbg!(state.next());
    }
Output:

    [src/main.rs:20] State::::new() = green
    [src/main.rs:21] state.next() = yellow
    [src/main.rs:22] state.next() = red
    [src/main.rs:23] state.next() = green

Re: State Machines in Rust

#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 finally a very well made JS library by David Khourshid that gives you lots of power leveraging statecharts: https://github.com/davidkpiano

While we're at it, here are some links to previous submissions on HN regarding statecharts with lots of useful and interesting information/experiences:

- https://news.ycombinator.com/item?id=18483704

- https://news.ycombinator.com/item?id=15835005

- https://news.ycombinator.com/item?id=21867990

- https://news.ycombinator.com/item?id=16606379

- https://news.ycombinator.com/item?id=22093176

My own interest in Statecharts comes from wanting/trying to use them for UI development on the web, think there is lots of value to be had and time to be saved by using leveraging it.

Re: State Machines in Rust

#29
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.

[deleted]

Re: State Machines in Rust

#30
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!

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...

Post reply on HN