liveBook · Manning

Type state

The typestate pattern encodes an object's valid states and permitted operations or transitions in its types, allowing invalid uses to be rejected by the type system.

How 2 Manning books explain it

Write Powerful Rust Macros

The type state pattern encodes an object's permitted states in its type so invalid state transitions or configurations are rejected at compile time.

How it is used
Enforces required builder fields and valid state transitions before construction or final macro output.
How deep it goes
Works through marker traits, generic state parameters, PhantomData, generated transition methods, and compile-fail testing for a builder.
Examples
Uses a red/green traffic light, a Gleipnir builder with three required fields, and a missing-field compile-fail test.
What you can do after
Build it from scratch by generating state-specific builders whose final state alone exposes `build`.

Read how Write Powerful Rust Macros explains type state →

Idiomatic Rust

A type-safe state machine is a state-machine design whose valid states and transitions are enforced by the programming language's type system.

How it is used
Uses type-level states to keep software and external state aligned and make invalid transitions unavailable.
How deep it goes
Defines states and transitions and demonstrates state-specific methods; omits database lookup and validation during session resumption.
Examples
Demonstrates light-bulb transitions and a user session with authentication, property updates, and logout.
What you can do after
Build a Rust state machine whose state-specific methods and transitions are checked by the type system.

Read how Idiomatic Rust explains type state →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.