m05-type-driven

Encode Rust type invariants with newtypes, PhantomData, and type state patterns.

1.4k|110|Updated Jan 17, 2026
One-click install
npx skills add https://github.com/actionbook/rust-skills --skill m05-type-driven-actionbook
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m05-type-driven
Source: https://github.com/actionbook/rust-skills/tree/main/skills/m05-type-driven
Command: npx skills add https://github.com/actionbook/rust-skills --skill m05-type-driven-actionbook

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps engineers design robust Rust APIs by encoding invariants in the type system, preventing invalid states at compile time.

Core Features & Use Cases

  • Type State patterns to model transitions at compile time.
  • Newtypes, PhantomData, and marker traits for zero-cost abstractions.
  • Builders and validated constructors to enforce invariants during creation.
  • ZST (zero-sized types) and sealed traits to prevent misuse and leakage of implementation details.
  • Real-world scenarios: API design, state machines, and safe abstractions.

Quick Start

Use this skill to implement a type-safe UserId newtype and a simple Connection state machine in Rust.

Frequently Asked Questions about m05-type-driven

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I encode invariants in the Rust type system to prevent invalid states at compile time?

Encoding Rust invariants at compile time is achieved by using type-driven design patterns like type state, newtypes, and PhantomData to structurally prevent invalid states. This skill guides you through implementing these zero-cost abstractions for robust API design.

What is the type state pattern in Rust and when should I use it?

The type state pattern in Rust models state transitions at compile time, ensuring only valid transitions are allowed. Use it when designing state machines or safe abstractions to prevent misuse by making invalid states unrepresentable.

How do I implement a type-safe builder pattern with validated constructors in Rust?

Implement a type-safe builder pattern in Rust by using PhantomData and type state to track required fields at compile time. This skill provides examples of builders and validated constructors that enforce invariants during object creation.

Do I need external Rust crates to use PhantomData and sealed traits for API design?

No, you do not need external crates to use PhantomData and sealed traits. This skill requires only stable Rust tooling and standard library constructs to implement zero-sized types and sealed traits, preventing misuse without adding dependencies.

What is the best way to prevent implementation detail leakage in Rust APIs?

The best way to prevent implementation detail leakage in Rust APIs is by using sealed traits and zero-sized types. This skill covers how to apply these patterns to restrict trait implementations and hide internal types from external consumers.

How do I create a type-safe newtype wrapper in Rust?

To create a type-safe newtype wrapper in Rust, define a struct around a primitive type to enforce domain invariants. This skill demonstrates building a type-safe UserId newtype and a simple Connection state machine as practical examples.