Type-Driven Correctness in Rust
Rust 中的类型驱动正确性
Speaker Intro
讲者简介
- Principal Firmware Architect in Microsoft SCHIE (Silicon and Cloud Hardware Infrastructure Engineering) team
微软 SCHIE 团队首席固件架构师。 - Industry veteran with expertise in security, systems programming (firmware, operating systems, hypervisors), CPU and platform architecture, and C++ systems
长期从事安全、系统编程、固件、操作系统、虚拟机监控器、CPU 与平台架构,以及 C++ 系统开发。 - Started programming in Rust in 2017 (@AWS EC2), and have been in love with the language ever since
自 2017 年在 AWS EC2 开始使用 Rust,此后持续深耕这门语言。
A practical guide to using Rust’s type system to make entire classes of bugs impossible to compile. While the companion Rust Patterns book covers the mechanics (traits, associated types, type-state), this guide shows how to apply those mechanics to real-world domains — hardware diagnostics, cryptography, protocol validation, and embedded systems.
这是一本强调“如何把一整类错误变成根本无法通过编译”的实战指南。姊妹教材 Rust Patterns 负责讲清 trait、关联类型、类型状态这些机制本身,而这本书要讲的是:如何把这些机制真正落到硬件诊断、密码学、协议验证和嵌入式系统这些真实领域里。
Every pattern here follows one principle: push invariants from runtime checks into the type system so the compiler enforces them.
这里所有模式都围绕同一个原则:把原本依赖运行时检查的不变量,前移到类型系统里,让编译器来强制执行。
How to Use This Book
如何使用本书
Difficulty Legend
难度说明
| Symbol | Level | Audience |
|---|---|---|
| 🟢 | Introductory 入门 | Comfortable with ownership + traits 已经熟悉所有权与 trait。 |
| 🟡 | Intermediate 中级 | Familiar with generics + associated types 已经熟悉泛型与关联类型。 |
| 🔴 | Advanced 高级 | Ready for type-state, phantom types, and session types 已经准备好进入类型状态、幻类型与会话类型。 |
Pacing Guide
学习路径建议
| Goal | Path | Time |
|---|---|---|
| Quick overview 快速总览 | ch01, ch13 (reference card) 第 1 章、第 13 章参考卡 | 30 min 30 分钟 |
| IPMI / BMC developer IPMI / BMC 开发者 | ch02, ch05, ch07, ch10, ch17 第 2、5、7、10、17 章 | 2.5 hrs 2.5 小时 |
| GPU / PCIe developer GPU / PCIe 开发者 | ch02, ch06, ch09, ch10, ch15 第 2、6、9、10、15 章 | 2.5 hrs 2.5 小时 |
| Redfish implementer Redfish 实现者 | ch02, ch05, ch07, ch08, ch17, ch18 第 2、5、7、8、17、18 章 | 3 hrs 3 小时 |
| Framework / infrastructure 框架 / 基础设施工程师 | ch04, ch08, ch11, ch14, ch18 第 4、8、11、14、18 章 | 2.5 hrs 2.5 小时 |
| New to correct-by-construction 第一次接触 correct-by-construction | ch01 → ch10 in order, then ch12 exercises 先顺序读完第 1–10 章,再做第 12 章练习 | 4 hrs 4 小时 |
| Full deep dive 完整深入学习 | All chapters sequentially 按顺序读完全书 | 7 hrs 7 小时 |
Annotated Table of Contents
带说明的目录
| Ch | Title | Difficulty | Key Idea |
|---|---|---|---|
| 1 | The Philosophy — Why Types Beat Tests 理念:为什么类型胜过测试 | 🟢 | Three levels of correctness; types as compiler-checked guarantees 正确性的三个层级,以及“类型就是编译器检查过的保证”这一视角。 |
| 2 | Typed Command Interfaces 类型化命令接口 | 🟡 | Associated types bind request → response 用关联类型把请求和响应绑定起来。 |
| 3 | Single-Use Types 单次使用类型 | 🟡 | Move semantics as linear types for crypto 把移动语义当作密码学里的线性类型来使用。 |
| 4 | Capability Tokens 能力令牌 | 🟡 | Zero-sized proof-of-authority tokens 零大小的授权证明令牌。 |
| 5 | Protocol State Machines 协议状态机 | 🔴 | Type-state for IPMI sessions + PCIe LTSSM 把类型状态应用到 IPMI 会话和 PCIe LTSSM。 |
| 6 | Dimensional Analysis 量纲分析 | 🟢 | Newtype wrappers prevent unit mix-ups 用 newtype 包装器防止单位混淆。 |
| 7 | Validated Boundaries 已验证边界 | 🟡 | Parse once at the edge, carry proof in types 在边界处解析一次,并把验证结果携带进类型里。 |
| 8 | Capability Mixins 能力混入 | 🟡 | Ingredient traits + blanket impls 用 ingredient trait 加 blanket impl 组合能力。 |
| 9 | Phantom Types 幻类型 | 🟡 | PhantomData for register width, DMA direction 用 PhantomData 表达寄存器宽度、DMA 方向等信息。 |
| 10 | Putting It All Together 全部整合 | 🟡 | All 7 patterns in one diagnostic platform 把 7 种模式整合进一个诊断平台。 |
| 11 | Fourteen Tricks from the Trenches 一线实践中的十四个技巧 | 🟡 | Sentinel→Option, sealed traits, builders, etc. 包括 Sentinel → Option、sealed trait、builder 等技巧。 |
| 12 | Exercises 练习 | 🟡 | Six capstone problems with solutions 六个带答案的综合题。 |
| 13 | Reference Card 参考卡片 | — | Pattern catalogue + decision flowchart 模式目录加决策流程图。 |
| 14 | Testing Type-Level Guarantees 测试类型层保证 | 🟡 | trybuild, proptest, cargo-show-asm 涵盖 trybuild、proptest 和 cargo-show-asm。 |
| 15 | Const Fn Const Fn | 🟠 | Compile-time proofs for memory maps, registers, bitfields 为内存映射、寄存器和位段提供编译期证明。 |
| 16 | Send & Sync Send 与 Sync | 🟠 | Compile-time concurrency proofs 提供编译期并发正确性证明。 |
| 17 | Redfish Client Walkthrough Redfish 客户端实战讲解 | 🟡 | Eight patterns composed into a type-safe Redfish client 把八种模式组合进一个类型安全的 Redfish 客户端。 |
| 18 | Redfish Server Walkthrough Redfish 服务端实战讲解 | 🟡 | Builder type-state, source tokens, health rollup, mixins 涵盖 builder 类型状态、source token、health rollup 和 mixin。 |
Prerequisites
前置知识
| Concept | Where to learn it |
|---|---|
| Ownership and borrowing 所有权与借用 | Rust Patterns, ch01 可参考 Rust Patterns 第 1 章。 |
| Traits and associated types Trait 与关联类型 | Rust Patterns, ch02 可参考 Rust Patterns 第 2 章。 |
| Newtypes and type-state Newtype 与类型状态 | Rust Patterns, ch03 可参考 Rust Patterns 第 3 章。 |
| PhantomData PhantomData | Rust Patterns, ch04 可参考 Rust Patterns 第 4 章。 |
| Generics and trait bounds 泛型与 trait 约束 | Rust Patterns, ch01 可参考 Rust Patterns 第 1 章。 |
The Correct-by-Construction Spectrum
Correct-by-Construction 光谱
← Less Safe More Safe →
Runtime checks Unit tests Property tests Correct by Construction
───────────── ────────── ────────────── ──────────────────────
if temp > 100 { #[test] proptest! { struct Celsius(f64);
panic!("too fn test_temp() { |t in 0..200| { // Can't confuse with Rpm
hot"); assert!( assert!(...) // at the type level
} check(42)); }
} }
Invalid program?
Invalid program? Invalid program? Invalid program? Won't compile.
Crashes in prod. Fails in CI. Fails in CI Never exists.
(probabilistic).
This guide operates at the rightmost position — where bugs don’t exist because the type system cannot express them.
这本书关注的就是最右边那一端:错误之所以不存在,不是因为测出来了,而是因为类型系统根本不允许它被表达出来。