当我将Rust Project汇编为WASM时,会包括私人功能吗?
如果我有一个lib.rs文件,并且我正在使用mod.rs文件中的模块中的一个函数,并且该函数在mod.rs文件中使用另一个函数,但并未公开向lib.rs。因此,Lib.r.R.
如果我将项目发布到网络上会怎样?哪些文件公开?我假设整个mod.rs仍然可以访问,因为该网站仍然需要使用mod.rs的功能,还是只能从mod.rs中加载公共功能?
注意:我根本不熟悉将项目释放到实际网络。因此,它在不了解所有功能方面也起着重要的作用。我正在使用WASM(带JS)来编译某些功能。
If I have a lib.rs file and I'm using a function from a module in the mod.rs file, and that function uses another function within the mod.rs file but isn't made public to lib.rs. Thus is inaccessible to the lib.rs.
What happens if I publish my project to the web? Which files are made public? I'm assuming the whole mod.rs is still accessible since the website still needs to use function from the mod.rs, or will it only load in the public functions from mod.rs?
Note: I am not at all familiar with releasing projects to the actual web. So it also plays a big role in not seeing how it all works. I'm using WASM (with JS) to compile certain functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前尚不清楚您的意思,因为这里有两个方面:
是,如果您从公共功能访问私有功能(包括通过间接函数,嵌套呼叫,模块等),则是私有功能的代码将包含在最后的可执行文件中。这显然是正确的,因为没有私人功能的代码,公共函数根本无法工作...
pub
(或#[使用]
),并且可以从板条箱的顶层到达编译器和链接器,如果他们选择的话,可以自由删除符号。这意味着您的最终WASM模块中包含的私有功能很可能不是可见的符号,而您不应该假设它们是。私有函数的 code 可能是由于上述而包含的,但是不可能直接从外部调用私有函数。事先告诉编译器和/或链接器是否会 将函数作为可见符号,这并非微不足道。如果您在
debug
mode中进行编译,则可能包括符号可能包括,仅仅是因为编译器和/或链接器不做必要的工作以证明符号不需要包括。另外,可能需要使用链接时间优化才能做到这一点,您可能根本不使用。然而,无论如何,除非明确标记符号pub
(或#[used
;即使这样,链接器也可能决定对您不利),则不能假定公开可见的符号。例如:
在这里,IR生成三个符号:
如您所见,IR将产生两个明确标记的
内部
-mod :: private
和>的符号mod :: public
,即使mod :: public
是pub
,但它在私人模块内! - 只有public
不是内部
,并且将在最终可执行文件中公开可见。It's not exactly clear what you mean, as there are two aspects here:
Yes, if you access a private function from a public function (including via indirections, nested calls, modules, etc.), the code of the private function will be included in the final executable. This is obviously true, as the public function simply would not work without the code from the private function...
No, if a symbol is not explicitly marked
pub
(or#[used]
) and reachable from the crate's top level the compiler and linker are free to remove the symbol if they so choose. This means a private function is most likely not included in your final wasm module as a visible symbol, and you should not assume that they are. The private function's code might be included due to the above, but it is not possible to call the private function directly from the outside.It is not trivial to tell in advance if the compiler and/or linker will not include a function as a visible symbol. The symbol might be included if you are compiling in
debug
-mode, simply because the compiler and/or linker do not do the necessary work to prove that the symbol does not need to be included. Also, using link-time-optimization might be required to do that, which you may simply not use. Yet in any case, unless the symbol is explicitly markedpub
(or#[used
; and even then the linker might decide to work against you), you can't assume the symbol to be publicly visible.For example:
Here, the IR generates three symbols:
As you can see, the IR will produce two symbols that are explicitly marked
internal
-mod::private
andmod::public
, even thoughmod::public
ispub
, but it is inside a private module! - and onlypublic
is notinternal
and will become publicly visible in the final executable.