What problem does it solve?
This Skill helps developers prevent invalid runtime states by encoding invariants in the Rust type system, enabling compile-time guarantees that certain states can only exist when preconditions are met.
Core Features & Use Cases
- Type State: model distinct states with separate types to ensure valid transitions at compile time.
- Newtype: wrap primitive values to enforce domain semantics and invariants.
- PhantomData: use phantom type parameters to encode additional constraints without runtime cost.
- Marker Traits: express capabilities and allowed operations through trait bounds.
- Builder Pattern: progressively construct complex objects with type-safe steps.
- Sealed Trait / ZST: limit external implementations and leverage zero-sized markers for safety and API clarity.
Quick Start
Create a small Rust module that defines a newtype around a primitive (e.g., UserId), implements a simple Type State flow (e.g., Unregistered -> Registered), and experiments with PhantomData and marker traits to encode invariants. Then wire a minimal builder to demonstrate gradual construction.