Compare `protocol` with Go interfaces, Swift existentials and Rust trait objects
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 71/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
调研方向
阅读 OUTLINE.md §3,并使用 issue 中的比较,分别为 Go 接口、Swift 存在类型和 Rust trait 对象各写一句话。必要时检查所链接参考资料的准确性。完成标准是 §3 中包含基于该比较的三句指定内容。
由索引模型根据 Issue 内容生成。
描述
OUTLINE.md §3 asks for a sentence each on Go, Swift and Rust. This is the comparison behind those sentences. Thanks to Valentyn Yukhymenko for the Go references.
protocol |
Go interface | Swift any P |
Rust dyn Trait |
|
|---|---|---|---|---|
| Conformance | Structural, exact signatures | Structural, exact signatures | Declared: extension T: P |
Declared: impl Trait for T |
| Checked | Compile time | Compile time from a concrete type; run time between interfaces | Compile time; run time for as? any P |
Compile time |
| Representation | protocol_view: vtable pointer and object pointer. protocol: vtable pointer, storage with a small buffer, allocator |
itab pointer and data pointer | Three-word buffer or heap box, type metadata pointer, one witness table pointer per protocol | Data pointer and vtable pointer |
| Ownership | protocol owns and deep-copies; protocol_view borrows |
Garbage collected | Value semantics | Chosen by the pointer: Box, &, Rc |
| Copy and destroy | Owning vtable | Not needed | Value witness table, reached from the type metadata | Drop in the vtable; no copy |
| Table built | Compile time, per interface and type | Compile time from a concrete type; run time between interfaces, cached in a global hash table | Compile time, per conformance | Compile time, per trait and type |
| Type identity | const std::type_info* in the vtable |
Type descriptor in the itab, which reflect reads |
Type metadata pointer | None in the vtable |
| Composition | None (#511) | Embedded interfaces | P & Q |
Supertraits; dyn A + B is not allowed |
| Narrowing | None (#98) | To any subset interface, at run time | From P & Q to P |
To a supertrait, since Rust 1.86 |
| Erased type satisfies its own interface | No (#262) | Interface values convert without nesting | No | Yes |
| Members from a base or embedded type count | No (#513) | Yes | n/a | n/a |
| Default implementations | None (#510) | None | Protocol extensions | Bodies in the trait |
| Comparing two erased values | None (#440) | == compares dynamic types then values; panics if the type is not comparable |
Not built in | Not built in |
| Empty state | Valueless after move | nil |
None | None |
Go is the closest: implicit conformance, exact signatures, and the same two words in a view. It differs where it relies on a runtime: garbage collection instead of ownership, no small buffer, and tables built at run time. Swift is the closest on storage, with an inline buffer and per-type copy and destroy operations. Rust is closer to proxy than to protocol, since the pointer decides ownership.
Follow-ups: guardrails against accidental conformance (#518), a type descriptor for run-time metadata (#519), and comments on #513, #511, #98, #262 and #121.
References:
- Russ Cox, Go Data Structures: Interfaces
- Rob Pike, Go at Google; Go FAQ;
runtime/iface.go - Swift TypeLayout, SE-0352, SE-0364
- Rust Reference: trait objects, orphan rules
- 主要语言
- C++
- 星标
- 12
- 派生
- 4
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 21
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
jbcoe/cc-protocol 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
jbcoe/cc-protocol#507 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
jbcoe/cc-protocol#455 · 3 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
jbcoe/cc-protocol#407 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
jbcoe/cc-protocol#194 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 25/100
jbcoe/cc-protocol#519 ·
维护者通常 1 天内回复
查看 jbcoe/cc-protocol 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 1 天内回复
-
category:port-update
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
bug-unconfirmed
难度 1/5 1 小时以内 新手友好度 75/100
维护者通常 1 天内回复
-
good first issue help wanted phase:0 priority:P3 type:task
难度 2/5 1-3 小时 新手友好度 67/100
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复