Use Type-State pattern without the ugly code

197 points ozgunozerk

I love type-state pattern's promises:

  • compile time checks
  • better/safer auto completion suggestions by your IDE
  • no additional runtime costs

However, I agree that in order to utilize type-state pattern, the code has to become quite ugly. We are talking about less readable and maintainable code, just because of this.

Although I'm a fan, I agree usually it's not a good idea to use type-state pattern.

And THAT, my friends, bothered me...

So I wrote this: https://crates.io/crates/state-shift

TL;DR -> it lets you convert your structs and methods into type-state version, without the ugly code. So, best of both worlds!

Also the GitHub link (always appreciate a ⭐️ if you feel like it): https://github.com/ozgunozerk/state-shift/

Any feedback, contribution, issue, pr, etc. is more than welcome!