Rust 的 boolean 和其他原始类型在哪里实现? [英] Where are Rust's boolean and other primitive types implemented?

查看:66
本文介绍了Rust 的 boolean 和其他原始类型在哪里实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Rust 中一些基本类型背后的代码,例如令人愉快的简单实现 Optiontuple 后面的奇怪的宏魔法,我能够在 libcore.除了一个 - bool.我在其他任何地方也找不到.

I was going through the code behind some of the basic types in Rust, e.g. the pleasantly simple implementation of Option<T> or the weird macro magic behind tuple and I was able to find all of the types that I wanted in libcore. All except for one - bool. I couldn't find it anywhere else either.

Rust 中 bool 背后的代码在哪里?我知道这不是最新颖的类型,但我很惊讶找不到它.

Where is the code behind bool in Rust? I know this is not the most novel type out there, but I was surprised I could not find it.

感谢 Francis 和 rodrigo 的回答,我注意到我为其他原语找到的代码只是它们的特征和相关的宏,而不是实际的实现.

Thanks to the answers by Francis and rodrigo, I noticed that the code I found for other primitives was just their traits and related macros, but not actual implementations.

Rust 书指出,原语内置于语言中,但我对这个解释并不满意.它们是什么时候建成的?是否可以追溯到 Rust 编译器首次使用 Rust 构建的时间,还是仍然使用 OCaml 构建时发生的?是否存在相关代码?

The Rust book states that the primitives are built-in to the language, but I'm not satisfied with this explanation. When were they built in? Can it be traced to the time when the Rust compiler was first built with Rust or did it happen when it was still built in OCaml? Does any relevant code exist?

推荐答案

所以这里有更多关于编译器中发生的事情的信息.对于初学者,正如已经提到的,布尔值的实际操作完全由 LLVM 处理,并直接转换为相应的 CPU 指令.虽然在某些情况下,代码会因为引导而神奇地出现,但这不是其中之一.编译器专门用于处理这些类型并发出正确的 LLVM 指令.

So here's a bit more information about what goes on in the compiler. For starters, as has already been mentioned, the actual operations that occur with booleans are entirely handled by LLVM, and get directly translated to the corresponding CPU instructions. While there are some cases where code just magically appears due to bootstrapping, this is not one of them. The compiler is specifically written to handle these types and emit the correct LLVM instructions.

对于编译的最早部分(例如在宏扩展期间),bool 类型并不特殊.它只是一些带有标识符 bool 的路径.最终在这里它会被转换为一种原始类型.类型的实际定义是这里.

For the earliest parts of compilation (e.g. during macro expansion) the type bool isn't special. It's just some path with the identifier bool. Eventually around here it will get converted to a primitive type. The actual definition of the type is here.

那么现在让我们看看 ! 操作符是如何工作的.正如我之前提到的,libcore 中执行 impl Not for bool 的代码从未被使用过.!expr 形式的代码被转换为 ::not(expr) 这里.但是,您会注意到它会检查此特定表达式是否实际上是方法调用,如果不是方法调用,则将其保留为 !expr.它怎么知道?MIR 中的调用只是一个缓存查找.在类型检查过程中填充了缓存.这里是缓存插入发生的地方- 基本上是在任何时候看到 ! 时检查给定类型是否实现了 Not 特性.你会注意到这一行特别排除布尔值和整数类型,最终直接编译为 LLVM 指令.

So now let's look at how the ! operator works. As I mentioned earlier, the code in libcore that does impl Not for bool never gets used. Code in the form !expr gets transformed into <T as Not>::not(expr) here. However, you'll notice that it checks to see if this particular expression is in fact a method call or not, and just leaves it as !expr if it's not meant to be a method call. How does it know? The call in MIR is just a cache lookup. The cache got populated during the type checking pass. Here is where the cache insertion occurs -- basically checking to see if the Not trait is implemented for a given type any time it sees a !. And you'll notice that this line specifically excludes booleans and integral types, which end up compiling down to LLVM instructions directly.

这是如何定义的粗略图景.您会在其他原语的相同文件中找到类似的代码.理论上,某处可能有一行是 enum bool { true, false } —— 但最终这个相同的代码仍然需要覆盖它并发出适当的 LLVM 内在函数,而整数不能是以这种方式表示.

That's the rough picture of how it's defined. You'll find similar code in the same files for other primitives. In theory there could be some line somewhere that was enum bool { true, false } -- but ultimately this same code would still need to override it and emit the appropriate LLVM intrinsics, and integers couldn't be represented this way.

这篇关于Rust 的 boolean 和其他原始类型在哪里实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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