m05-type-driven

Encode invariants in Rust types to prevent invalid states at compile time.

1|2|Updated Oct 10, 2025
One-click install
npx skills add https://github.com/dojoengine/torii-core --skill m05-type-driven-dojoengine
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m05-type-driven
Source: https://github.com/dojoengine/torii-core/tree/main/.agents/skills/m05-type-driven
Command: npx skills add https://github.com/dojoengine/torii-core --skill m05-type-driven-dojoengine

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

In languages like Rust, a lot of safety hinges on encoding invariants in the type system. Type-driven design helps you prevent invalid states by representing state with distinct types and only exposing safe transitions.

Core Features & Use Cases

  • Type-level guarantees with newtypes, phantom types, and the type-state pattern to prevent invalid states at compile time.
  • Compile-time validation via constructors that enforce invariants and by restricting state transitions to valid paths.
  • Practical use cases include API surfaces that model lifetimes, resource states, and guarded transitions; examples cover resource handles, connection lifecycles, and builder-based initialization.

Quick Start

Show me how to implement a simple type-state pattern for a resource handle 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 prevent invalid states in Rust at compile time?

Type-driven design prevents invalid states by using newtypes, phantom types, and the type-state pattern to represent distinct states as distinct types, restricting transitions to valid paths and enforcing invariants through constructors.

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

The type-state pattern models resource lifecycles and guarded transitions as distinct types, exposing only safe state changes via constructors and APIs to provide compile-time validation and zero-cost abstraction guarantees.

How do I implement a type-state pattern for a Rust resource handle?

Implement a Rust resource handle using type-states and phantom types to model lifecycles, enforcing compile-time validation by restricting state transitions and exposing safe constructors that validate invariants before instantiation.

When should I use phantom types and newtypes for Rust API safety?

Use phantom types and newtypes for Rust API safety when modeling guarded state transitions like connection lifecycles or builder-based initialization, ensuring compile-time validation by encoding invariants directly into the type system.

Does the Rust type-state pattern work for builder-based initialization?

Yes, the type-state pattern applies to builder-based initialization by encoding invariants in types, enforcing compile-time validation of builder steps, and restricting transitions through safe constructors to guarantee valid object construction.

What are the limitations of type-driven design in Rust?

Type-driven design in Rust can increase code complexity when modeling intricate state machines, requiring careful constructor design and phantom type management to ensure compile-time validation does not overly restrict API flexibility.