Summary
零成本抽象与泛型专家。处理泛型, trait, monomorphization, static dispatch, dynamic dispatch, impl Trait, dyn Trait, 泛型, 特征, 单态化, 零成本抽象--- # 零成本抽象与泛型 ## 核心问题 **需要编译时多态还是运行时多态?** 选择正确的抽象层次直接影响性能。
smithery/huiali
零成本抽象与泛型专家。处理泛型, trait, monomorphization, static dispatch, dynamic dispatch, impl Trait, dyn Trait, 泛型, 特征, 单态化, 零成本抽象--- # 零成本抽象与泛型 ## 核心问题 **需要编译时多态还是运行时多态?** 选择正确的抽象层次直接影响性能。
零成本抽象与泛型专家。处理泛型, trait, monomorphization, static dispatch, dynamic dispatch, impl Trait, dyn Trait, 泛型, 特征, 单态化, 零成本抽象--- # 零成本抽象与泛型 ## 核心问题 **需要编译时多态还是运行时多态?** 选择正确的抽象层次直接影响性能。
Related neighbors and high-traction skills in the same topics — useful to compare before installing.
Guidance for distinctive, intentional visual design when building new UI or reshaping an existi…
866.4K installsBrowser automation CLI for AI agents. Use when the user needs to interact with websites, includ…
810.4K installsReview UI code for Web Interface Guidelines compliance. Use when asked to "review my UI", "chec…
617.3K installsBuild, deploy, evaluate, optimize, fine-tune, and manage Microsoft Foundry agents, models, and …
576.5K installsDebug Azure production issues on Azure using AppLens, Azure Monitor, resource health, and safe …
568.9K installsOther skills from smithery/huiali · top by installs.
npx skills add smithery/huiali
Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.
Files included with this skill beyond the listing page.
SKILL.md
2,718 B
SUMMARY.md
194 B
| 特性 | 泛型 (static dispatch) | trait object (dynamic dispatch) |
|---|---|---|
| 性能 | 零开销 | vtable 查找 |
| 代码大小 | 可能膨胀 | 更小 |
| 编译时间 | 更长 | 更短 |
| 灵活性 | 类型必须已知 | 运行时决定 |
| 异构集合 | 不支持 | Vec<Box<dyn Trait>> |
// 类型在编译时已知
fn process<T: Processor>(item: T) {
item.process();
}
// 返回同一类型
fn create_processor() -> impl Processor {
// 返回具体类型
}
// 多个类型参数
fn combine<A: Display, B: Display>(a: A, b: B) -> String {
format!("{} and {}", a, b)
}
// 运行时决定类型
trait Plugin {
fn run(&self);
}
struct PluginManager {
plugins: Vec<Box<dyn Plugin>>,
}
// 异构集合
let handlers: Vec<Box<dyn Handler>> = vec![
Box::new(HttpHandler),
Box::new(GrpcHandler),
];
// ❌ 不是对象安全的
trait Bad {
fn create(&self) -> Self; // 返回 Self
fn method(&self, x: Self); // 参数有 Self
}
// ✅ 对象安全
trait Good {
fn name(&self) -> &str;
}
// impl Trait:返回具体类型(静态分发)
fn create_processor() -> impl Processor {
HttpProcessor
}
// dyn Trait:返回 trait object(动态分发)
fn create_processor() -> Box<dyn Processor> {
Box::new(HttpProcessor)
}
// 泛型:每个类型生成一份代码
fn process<T: Trait>(item: T) {
item.method();
}
// 编译后:
// fn process_Http(item: Http) { ... }
// fn process_Ftp(item: Ftp) { ... }
// trait object:单一路径
fn process(item: &dyn Trait) {
item.method(); // 通过 vtable 调用
}
| 错误 | 原因 | 解决 |
|---|---|---|
| E0277 | 缺少 trait bound | 添加 T: Trait |
| E0038 | trait object 不安全 | 检查对象安全规则 |
| E0308 | 类型不匹配 | 统一类型或用泛型 |
| E0599 | 未找到实现 | 实现 trait 或检查约束 |