m05-type-driven

Encode domain invariants in Rust types with newtypes and builder patterns.

3|Updated Mar 22, 2026
One-click install
npx skills add https://github.com/0xharryriddle/codex-field-kit --skill m05-type-driven-0xharryriddle
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m05-type-driven
Source: https://github.com/0xharryriddle/codex-field-kit/tree/main/archive/upstream/chasebuild-agent-skills/rust/skills/m05-type-driven
Command: npx skills add https://github.com/0xharryriddle/codex-field-kit --skill m05-type-driven-0xharryriddle

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Encode invariants in the type system to prevent invalid states and provide safer APIs, reducing runtime errors.

Core Features & Use Cases

  • Type State for guarding state transitions between states at compile time.
  • Newtype wrappers and PhantomData to enforce invariants without runtime costs.
  • Builder patterns and marker traits to ensure only valid constructions.
  • Compile-time validation for robust, production-grade designs.

Quick Start

Demonstrate a compile-time invariant using a newtype and a guarded builder 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 enforce invariants in Rust at compile time?

Compile-time invariants in Rust are enforced using type-driven design techniques like type-state patterns, newtypes, and marker traits to prevent invalid states. This approach guarantees correctness by shifting validation to the compiler.

What is type-state pattern in Rust and how does it work?

The type-state pattern in Rust encodes state machines directly into the type system to guard state transitions at compile time. By using PhantomData and marker traits, it ensures only valid state transitions are permitted.

How do I build a safe builder pattern in Rust that prevents invalid construction?

A safe builder pattern in Rust uses marker traits and type-state encoding to ensure only valid object constructions compile successfully. This guarantees that required fields and invariants are satisfied before instantiation.

When should I use type-driven design instead of runtime validation in Rust?

Type-driven design is preferred over runtime validation in Rust when you need to eliminate runtime errors for domain invariants and state machines. It provides safer APIs with zero runtime costs by checking validity during compilation.

Can I use PhantomData and newtypes to enforce domain invariants without runtime overhead?

PhantomData and newtype wrappers enforce domain invariants in Rust without runtime overhead by encoding rules directly into the type system. This allows the compiler to reject invalid states during compilation.

Are there limitations to using type-state patterns for complex state machines in Rust?

Type-state patterns in Rust can increase code complexity and boilerplate when modeling highly complex state machines with many transitions. It is best suited for production-grade designs where compile-time safety outweighs structural verbosity.