本文介绍了泛型函数和类型如何存储在rlib中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,模板不能生成动态库,我们只能通过头文件使用它们。



在C#中,可以解释泛型函数和类型通过.NET中的中间语言。



Rust没有虚拟机,泛型可以存储在 rlib 文件。它如何设法实现这一目标?什么是rlib文件的格式?

解决方案

rlib 是一个常规静态库(内置 ar 格式),其中包含额外的元数据。该元数据包含用#[inline] 标记的所有泛型和函数的完整的,序列化的抽象语法树(AST)。



有点像是有一个C ++编译器将库的头文件推入编译的二进制文件,然后在链接到该库时再次读出它们。


In C++, the templates can not be generated into dynamic libraries, we can only use them by header files.

In C#, generic functions and types can be interpreted by intermediate language in .NET.

Rust has no virtual machine, and the generics can be stored in the rlib files. How does it managed to achieve this? What is the format of rlib files?

解决方案

An rlib is a regular static library (built in the ar format) that contains additional metadata. That metadata contains, among other things, the complete, serialised abstract syntax tree (AST) for all generics and functions marked with #[inline].

It's a bit like if there was a C++ compiler that shoved a library's header files into the compiled binary, and then read them out again when linking against that library.

这篇关于泛型函数和类型如何存储在rlib中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:00