Back to list
huiali

rust-type-driven

by huiali

An AI expert capability layer for Rust engineering practices, centered on modular skill orchestration and collaborative execution chains. It turns Rust’s core knowledge structures into callable reasoning and decision units, enabling diagnosis, design, and optimization in complex real-world scenarios.

1🍴 1📅 Jan 25, 2026

SKILL.md


name: rust-type-driven description: "类型驱动设计专家。处理 newtype, type state, PhantomData, marker trait, builder pattern, 类型状态, 新类型模式, 编译时验证, sealed trait, ZST" globs: ["**/*.rs"]

类型驱动设计

核心问题

如何让编译器在编译期捕获更多错误?

类型设计得好,运行时错误就少。


类型设计模式

Newtype 模式

// ❌ 原始类型容易被混淆
struct UserId(u64);
struct OrderId(u64);

// ✅ 类型安全:无法混用
fn get_user(user_id: UserId) { ... }
fn get_order(order_id: OrderId) { ... }

// 编译器会阻止:
// get_order(user_id);  // 编译错误!

类型状态模式 (Type State)

// 用类型编码状态
struct Disconnected;
struct Connecting;
struct Connected;

struct Connection<State = Disconnected> {
    socket: TcpSocket,
    _state: PhantomData<State>,
}

impl Connection<Disconnected> {
    fn connect(self) -> Connection<Connecting> {
        // ...
        Connection { socket: self.socket, _state: PhantomData }
    }
}

impl Connection<Connected> {
    fn send(&mut self, data: &[u8]) {
        // 只有 Connected 状态可以发送
    }
}

PhantomData

// 用 PhantomData 标记所有权和方差
struct MyIterator<'a, T> {
    _marker: PhantomData<&'a T>,
}

// 告诉编译器:我们借用了一个 T 的生命周期

让无效状态不可表示

// ❌ 容易创建无效状态
struct User {
    name: String,
    email: Option<String>,  // 可能为空
    age: u32,
}

// ✅ email 不可能为空
struct User {
    name: String,
    email: Email,  // 类型保证有效
    age: u32,
}

struct Email(String);

impl Email {
    fn new(s: &str) -> Option<Self> {
        if s.contains('@') {
            Some(Email(s.to_string()))
        } else {
            None
        }
    }
}

Builder 模式

struct ConfigBuilder {
    host: String,
    port: u16,
    timeout: u64,
    retries: u32,
}

impl ConfigBuilder {
    fn new() -> Self {
        Self {
            host: "localhost".to_string(),
            port: 8080,
            timeout: 30,
            retries: 3,
        }
    }

    fn host(mut self, host: impl Into<String>) -> Self {
        self.host = host.into();
        self
    }

    fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    fn build(self) -> Config {
        // 可以在这里做最终验证
        Config {
            host: self.host,
            port: self.port,
        }
    }
}

Marker Trait

// 用 marker trait 标记能力
trait Sendable: Send + 'static {}

// 或用 marker 做类型约束
struct Cache<T: Cacheable> {
    data: T,
}

trait Cacheable: Send + Sync {}

Zero-Sized Types (ZST)

// 用 ZST 做标记
struct DebugOnly;
struct Always;

// 只在 debug 模式执行的代码
struct DebugLogger<Mode = Always> {
    _marker: PhantomData<Mode>,
}

impl DebugLogger<DebugOnly> {
    fn log(&self, msg: &str) {
        println!("[DEBUG] {}", msg);
    }
}

常见反模式

反模式问题改进
is_valid 标志运行时检查用类型编码状态
大量 Option可能为空重新设计类型
原始类型 everywhere类型混淆Newtype
验证在运行时延迟错误发现构造函数验证
布尔参数含义不清用枚举或 builder

验证时机

验证类型最佳时机示例
范围验证构造时Email::new() 返回 Option
状态转换类型边界Connection<Connected>
引用有效性生命周期&'a T
线程安全Send + Sync编译器检查

Score

Total Score

70/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

+10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon