Rust 的 boolean 和其他基本类型在哪里实现?
Where are Rust's boolean and other primitive types implemented?
我正在研究 Rust 中一些基本类型背后的代码,例如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。除了一个 - bool
。我在其他地方也找不到。
Rust bool
背后的代码在哪里?我知道这不是最新颖的类型,但我很惊讶我找不到它。
感谢 Francis 和 rodrigo 的回答,我注意到我为其他原语找到的代码只是它们的特征和相关宏,而不是实际实现。
Rust 书上说原语 are built-in to the language,但我对这种解释并不满意。它们是什么时候建成的?它可以追溯到 Rust 编译器首次使用 Rust 构建的时间,还是它仍然在 OCaml 中构建时发生的?是否存在相关代码?
bool
is a primitive type。基本类型和对它们的操作由编译器实现,即编译器发出专门的代码来对基本类型执行操作。
您会看到 bool
实现了许多特征。那些实现来自libcore,但它们通常是使用相应的运算符来实现的。例如,Not::not
is implemented by returning !self
。对于任何非原始类型,这将递归调用 Not::not
并导致堆栈溢出,但对于原始类型,编译器以不同方式解析运算符,并且提供这些特征实现只是为了通用代码的好处。
您可以看到 core::i32
的定义只是因为常量 i32::MIN
和 i32::MAX
。实际类型只是内置类型 i32
.
的别名
在core::f32
情况下,例如有很多有用的常量。
但是对于bool
,没有有用的值,除了true
和false
是关键字,所以没有bool
的来源。
所以这里有一些关于编译器中发生的事情的更多信息。对于初学者,正如已经提到的,布尔值发生的实际操作完全由 LLVM 处理,并直接转换为相应的 CPU 指令。虽然在某些情况下代码会由于引导而神奇地出现,但这不是其中之一。编译器专门用于处理这些类型并发出正确的 LLVM 指令。
对于编译的最早部分(例如在宏扩展期间),类型 bool
并不特殊。它只是一些带有标识符 bool
的路径。最终 around here it will get converted to a primitive type. The actual definition of the type is here.
那么现在让我们看看 !
运算符是如何工作的。正如我之前提到的,libcore 中执行 impl Not for bool
的代码永远不会被使用。 !expr
形式的代码被转换为 <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 特别排除了布尔值和整数类型,它们最终直接编译为 LLVM 指令。
这就是它的大致定义。您会在其他原语的相同文件中找到类似的代码。理论上,某处可能有某行是 enum bool { true, false }
——但最终相同的代码仍需要重写它并发出适当的 LLVM 内在函数,并且不能以这种方式表示整数。
我正在研究 Rust 中一些基本类型背后的代码,例如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。除了一个 - bool
。我在其他地方也找不到。
Rust bool
背后的代码在哪里?我知道这不是最新颖的类型,但我很惊讶我找不到它。
感谢 Francis 和 rodrigo 的回答,我注意到我为其他原语找到的代码只是它们的特征和相关宏,而不是实际实现。
Rust 书上说原语 are built-in to the language,但我对这种解释并不满意。它们是什么时候建成的?它可以追溯到 Rust 编译器首次使用 Rust 构建的时间,还是它仍然在 OCaml 中构建时发生的?是否存在相关代码?
bool
is a primitive type。基本类型和对它们的操作由编译器实现,即编译器发出专门的代码来对基本类型执行操作。
您会看到 bool
实现了许多特征。那些实现来自libcore,但它们通常是使用相应的运算符来实现的。例如,Not::not
is implemented by returning !self
。对于任何非原始类型,这将递归调用 Not::not
并导致堆栈溢出,但对于原始类型,编译器以不同方式解析运算符,并且提供这些特征实现只是为了通用代码的好处。
您可以看到 core::i32
的定义只是因为常量 i32::MIN
和 i32::MAX
。实际类型只是内置类型 i32
.
在core::f32
情况下,例如有很多有用的常量。
但是对于bool
,没有有用的值,除了true
和false
是关键字,所以没有bool
的来源。
所以这里有一些关于编译器中发生的事情的更多信息。对于初学者,正如已经提到的,布尔值发生的实际操作完全由 LLVM 处理,并直接转换为相应的 CPU 指令。虽然在某些情况下代码会由于引导而神奇地出现,但这不是其中之一。编译器专门用于处理这些类型并发出正确的 LLVM 指令。
对于编译的最早部分(例如在宏扩展期间),类型 bool
并不特殊。它只是一些带有标识符 bool
的路径。最终 around here it will get converted to a primitive type. The actual definition of the type is here.
那么现在让我们看看 !
运算符是如何工作的。正如我之前提到的,libcore 中执行 impl Not for bool
的代码永远不会被使用。 !expr
形式的代码被转换为 <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 特别排除了布尔值和整数类型,它们最终直接编译为 LLVM 指令。
这就是它的大致定义。您会在其他原语的相同文件中找到类似的代码。理论上,某处可能有某行是 enum bool { true, false }
——但最终相同的代码仍需要重写它并发出适当的 LLVM 内在函数,并且不能以这种方式表示整数。