知识卡片

普通读书笔记卡 · 6074.d.1.a.2.c

动态接口需要可确定的调用形状

理解等级:中级

trait对象调用时不知道具体Self,因此不能直接调用需任意类型实例化的泛型方法,或依赖未知具体返回大小的Self方法。原书对象安全现称dyn兼容性,实际条件还包括关联常量等,不能仅记成两条穷尽规则。设计时应把动态调用面与具体类型专用操作分开。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 2. Traits Trait Object Safety · 查看原书依据

      Trait Object Safety Another restriction on trait objects is the requirement for object safety: only traits that comply with the following two rules can be used as trait objects: The trait’s methods must not be generic. The trait’s methods must not involve a type that includes Self, except for the receiver (the object on which the method is invoked). The firs

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Trait Object Safety》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u073-ch02-020)。 - 结论依据:原文在“Trait Object Safety”附近的定义、例子或代码支撑本卡核心说明:trait对象调用时不知道具体Self,因此不能直接调用需任意类型实例化的泛型方法,或依赖未知具体返回大小的Self方法。边界按已读前提解释;示例未执行。 - 原始内容:Trait Object Safety Another restriction on trait objects is the requirement for object safety: only traits that comply with the following two rules can be used as trait objects: The trait’s methods must not be generic. The trait’s methods must not involve a type that includes Self, except for the receiver (the object on which the method is invoked). The firs