知识卡片
解引用转换支持智能指针
实现Deref或DerefMut的智能指针可以在适用位置把自身引用转换为Target的引用,减少调用方反复拆包装的代码。它适合表达指针式访问语义,不能据此认为包装对象与目标拥有相同布局、所有权或业务身份。trait对象转换则还有动态分派信息,是另一种机制。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Coercion · 查看原书依据
DerefMut trait. These traits indicate that the user-defined type is acting as a smart pointer of some sort (Item 8), and in this case the compiler will coerce a reference to the smart pointer item into being a reference to an item of the type that the smart pointer contains (indicated by its Target). The second coercion of a user-defined type happens when a
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Coercion》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u034-ch01-044)。
- 结论依据:原文在“DerefMut”附近的定义、例子或代码支撑本卡核心说明:实现Deref或DerefMut的智能指针可以在适用位置把自身引用转换为Target的引用,减少调用方反复拆包装的代码。边界按已读前提解释;示例未执行。
- 原始内容:DerefMut trait. These traits indicate that the user-defined type is acting as a smart pointer of some sort (Item 8), and in this case the compiler will coerce a reference to the smart pointer item into being a reference to an item of the type that the smart pointer contains (indicated by its Target). The second coercion of a user-defined type happens when a