A Raft State Machine in Rust - 2018-12-07 00:00:00

Implementing Raft in Rust In idiomatic OO Java, a simplistic state machine for Raft could be written something like: class Raft { enum State { Follower, Candidate, Leader } // Votes we've received from other nodes private Map<Long, Boolean> votes; // Our current state private State state = State.Follower; public void apply(VoteMessage message) { if (this.state != Candidate) { throw new InvalidStateException("Only candidates can receive votes!"); } votes.

Rust-like Enums in TypeScript - 2019-03-08 00:00:00

One of the nicest things about Rust is handling nulls with Option and error states with Result. Because Rust has good pattern matching, handling these cases becomes trivial as the complier forces you to handle all the cases of an enum in a match expression. Because TypeScript lacks pattern matching, we have to lean on the type system in a different way to get similar semantics. For Option, we start with defining the two different states and the union type that represents the Option: