自动取消引用和强制取消引用之间有什么关系? [英] What is the relation between auto-dereferencing and deref coercion?

查看:91
本文介绍了自动取消引用和强制取消引用之间有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过讨论,我现在有些困惑自动反引用 deref强制之间的关系。

After some discussion, I'm now a little bit confused about the relation between auto-dereferencing and deref coercion.

看来,术语自动取消引用仅在以下情况下适用取消引用的目标是方法接收者
,而看来, deref强制一词适用于函数参数及其需要的所有上下文。

It seems that the term "auto-dereferencing" applies only when the target to dereference is a method receiver, whereas it seems that the term "deref coercion" applies to function arguments and all contexts it needs to.

我认为取消引用并不总是涉及到deref强制,但是我不确定:取消引用是否总是使用某些 Deref :: deref 特征实现?

I thought that a dereference does not always involve deref coercion, but I'm not sure: does dereferencing always use some Deref::deref trait implementation?

如果是,是 T的实现者:Deref< Ta rget = U>编译器中内置的T:& U 在哪里?

最后,在编译器隐式转换&&&&的所有情况下,使用术语 autoderef听起来很自然。 x & x

Finally, it sounds natural to use the term "autoderef" in all the cases where the compiler implicitly transforms &&&&x to &x:

pub fn foo(_v: &str) -> bool {
    false
}

let x="hello world";
foo(&&&&x);

这是社区的普遍共识吗?

Is this the general consensus of the community?

推荐答案

这两种情况之间的相似之处很肤浅。

The parallels between the two cases are rather superficial.

在方法调用表达式中,编译器首先需要确定要调用的方法。该决定基于接收器的类型。编译器生成候选接收器类型的列表,其中包括通过反复取消引用接收器而获得的所有类型,还包括& T & mut T 遇到的所有类型 T 。这就是为什么您可以调用直接将& mut self 接收为 x.foo()的方法的原因必须编写(& mut x).foo()。对于候选列表中的每种类型,编译器然后会查找固有的方法以及基于可见特征的方法。有关更多详细信息,请参见语言参考

In a method call expression, the compiler first needs to determine which method to call. This decision is based on the type of the receiver. The compiler builds a list of candidate receiver types, which include all types obtained by repeatedly derefencing the receiver, but also &T and &mut T for all types T encountered. This is the reason why you can call a method receiving &mut self directly as x.foo() instead of having to write (&mut x).foo(). For each type in the candidate list, the compiler then looks up inherent methods and methods on visible traits. See the language reference for further details.

反引用强制是完全不同的。它仅发生在胁迫站点上,在该站点上编译器完全知道期望的类型。如果遇到的实际类型与期望的类型不同,则编译器可以使用任何强制(包括deref强制)将实际类型转换为期望的类型。可能的强制列表包括未定大小的强制,指针减弱和反强制。有关更多详细信息,请参见有关Nomicon中强制的章节

A deref coercion is rather different. It only occurs at a coercion site where the compiler exactly knows what type to expect. If the actual type encountered is different from the expected type, the compiler can use any coercion, including a deref coercion, to convert the actual type to the expected type. The list of possible coercions includes unsized coercions, pointer weakening and deref coercions. See the the chapter on coercions in the Nomicon for further details.

因此,这实际上是两种截然不同的机制–一种用于找到正确的方法,一种用于在已经知道确切期望使用哪种类型的情况下转换类型。第一种机制还会自动引用接收者,这在强制中是永远不会发生的。

So these are really two quite different mechanisms – one for finding the right method, and one for converting types when it is already known what type exactly to expect. The first mechanism also automatically references the receiver, which can never happen in a coercion.


我认为取消引用并不总是涉及强制取消引用,但是我不确定:取消引用是否总是使用某些 Deref :: deref 特征实现?

并非每个取消引用都是一个deref强制。如果您编写 * x ,则明确地取消引用 x 。相反,deref 强制由编译器隐式执行,并且仅在编译器知道预期类型的​​地方执行。

Not every dereferencing is a deref coercion. If you write *x, you explicitly dereference x. A deref coercion in contrast is performed implicitly by the compiler, and only in places where the compiler knows the expected type.

取消引用的语义取决于 x 指针类型,即是否为引用或原始指针。对于指针类型, * x 表示对象 x 指向的对象,而对于其他类型, * x 等效于 * Deref :: deref(& x)(或与此类似的可变逻辑)。

The semantics of dereferencing depend on whether the type of x is a pointer type, i.e. a reference or a raw pointer, or not. For pointer types, *x denotes the object x points to, while for other types *x is equivalent to *Deref::deref(&x) (or the mutable anlogue of this).


如果是,是 T的实现者:Deref< Target = U> T:& U 内置在编译器中的位置吗?

If so, is the implementor of T: Deref<Target = U> where T: &U built into the compiler?

我不太确定您的语法是什么应该意味着–它肯定不是有效的Rust语法–但我想您是在问是否将& T 的实例取消引用为 T 内置在编译器中。如上所述,编译器内置了对指针类型(包括引用)的取消引用,但是还存在& T Deref 的全面实现在标准库中。此通用实现对通用代码很有用-特质绑定 T:Deref< Target = U> 否则,将不允许 T =& U

I'm not quite sure what your syntax is supposed to mean – it's certainly not valid Rust syntax – but I guess you are asking whether derefencing an instance of &T to T is built into the compiler. As mentioned above, dereferencing of pointer types, including references, is build into the compiler, but there is also a blanket implementation of Deref for &T in the standard library. This blanket implementation is useful for generic code – the trait bound T: Deref<Target = U> otherwise wouldn't allow for T = &U.

这篇关于自动取消引用和强制取消引用之间有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆