简体   繁体   English

是否有文件证明Cargo可以下载并捆绑同一板条箱的多个版本?

[英]Is it documented that Cargo can download and bundle multiple versions of the same crate?

By forking and playing with some code, I noticed that Cargo can download and bundle multiple versions of the same crate in the same project (native-tls 0.1.5 and 0.2.1, for example). 通过分叉并使用一些代码,我注意到Cargo可以在同一项目中下载并捆绑同一板条箱的多个版本(例如,native-tls 0.1.5和0.2.1)。 I have wasted so much time by looking at the documentation of the wrong version. 通过查看错误版本的文档,我浪费了很多时间。

I have looked for some information about this behaviour but I was not able to find anything. 我一直在寻找有关此行为的一些信息,但找不到任何东西。 Is this documented somewhere? 这是在某处记录的吗?

Is there an easy way to determine/detect the version used by the code you're working on (current edited file)? 有没有一种简单的方法来确定/检测您正在使用的代码(当前编辑的文件)所使用的版本? Or can we tell Cargo to show some warnings/prevent the build if two versions the same crate are required? 还是如果需要两个版本相同的板条箱,我们可以告诉Cargo显示一些警告/防止构建吗?

It was a conscious decision when designing Rust to allow multiple versions of the same crate. 在设计Rust以允许同一箱子的多个版本时,这是一个明智的决定。

You have probably heard of Dependency Hell before, which occurs when 2 (or more) dependencies A and B have a common dependency C but each require a version which is incompatible with the other. 您可能以前曾听说过“ 依赖关系地狱” ,这是当2个(或更多)依赖关系A和B具有共同的依赖关系C,但每个要求的版本都不兼容时发生的。

Rust was designed to ensure that this would not be an issue. Rust旨在确保这不会成为问题。

In general, cargo will attempt to find a common version which satisfies all requirements. 通常,货物将尝试找到满足所有要求的通用版本。 As long as crate authors use SemVer correctly, and the requirements give enough leeway, a single version of the dependency can be computed and used successfully. 只要板条箱作者正确使用SemVer,并且要求具有足够的回旋余地,就可以成功计算并使用依赖项的单个版本。

Sometimes, however, multiple versions of the same dependency are necessary, such as in your case since 0.1.x and 0.2.x are considered two different major versions. 但是,有时需要具有相同依赖项的多个版本,例如在您的情况下,因为0.1.x和0.2.x被视为两个不同的主要版本。 In this case, Rust has two features to allow the two versions to be used within the same binary: 在这种情况下,Rust具有两个功能,以允许在同一二进制文件中使用两个版本:

  • a unique hash per version is appended to each symbol. 每个版本的唯一哈希将附加到每个符号。
  • the type system considers the same type Foo from two versions of C to be different types. 类型系统将两个C版本中的相同类型Foo视为不同类型。

There is a limitation, of course. 当然有一个限制。 If a function of A returns an instance of C::Foo and you attempt to pass it to a function of B, the compiler will refuse it (it considers the two types to be different). 如果A的函数返回C::Foo的实例,而您尝试将其传递给B的函数,则编译器将拒绝它(它认为这两种类型是不同的)。 This is an intractable problem 1 . 这是一个棘手的问题1

Anytime the dependency on C is internal, or the use of C is isolated, it otherwise works automatically. 每当对C的依赖是内部的,或者对C的使用被隔离时,它都会自动起作用。 As your experience shows, it is so seamless that the user may not even realize it is happening. 如您的经验所示,它是如此无缝,以至于用户甚至可能没有意识到它正在发生。

1 See the dtolnay trick that crate authors can use to allow some types to be interchangeable. 1 请参阅dtolnay技巧 ,板条箱作者可以使用dtolnay技巧允许某些类型互换。

Cargo can indeed link multiple versions of some crate, but only one of those versions can be a direct dependency. 货物确实可以链接某个板条箱的多个版本,但是这些版本中只有一个可以直接依赖。 The others are indirect references. 其他是间接引用。

The direct reference is always the version referenced by Cargo.toml and on top-level of Cargo.lock (while the indirect references are in the dependencies subsections). 直接引用总是引用的版本Cargo.toml和顶级Cargo.lock (而间接引用是dependencies小节)。

I am not sure how much it is documented, unfortunately. 不幸的是,我不确定有多少文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM