2016-09-27 10 views
1

鉴于此小型库在子目录中使用本地箱子,我将如何使其中一个依赖项成为可选项,具体取决于是否启用了某个功能?如何使本地依赖取决于Cargo中的一项功能?

[package] 
name = "image_load" 
description = "Small wrapper for image reading API's." 
version = "0.1.0" 

[features] 

default = ["use_png"] 

[dependencies] 

[dependencies.image_load_ppm] 
path = "ppm" 

# How to make this build _only_ when 'use_png' feature is enabled? 
[dependencies.image_load_png] 
path = "png" 

虽然我读the documentation,这显示了如何具有可选外部依赖。在上面的示例中,我使用本地子目录,我想要构建或不基于某个功能。

我怎样才能使image_load_png只有当use_png功能启用时生成。

+1

您可以查看它的[documentation](http://doc.crates.io/manifest.html#the-features-section)。 – squiguy

+0

我看了这个页面,但没有看到这个例子。 – ideasman42

+0

它在注释之下**#所有可选依赖**的列表。 – Shepmaster

回答

4

这可以通过添加以下来完成:

[package] 
name = "image_load" 
version = "0.1.0" 
description = "Small wrapper for image reading API's." 

[features] 

default = ["use_png"] 
use_png = ["image_load_png"] # <-- new line 

[dependencies] 

[dependencies.image_load_ppm] 
path = "ppm" 

[dependencies.image_load_png] 
path = "png" 
optional = true # <-- new line 

使用箱可选配。

例如为:

#[cfg(feature = "use_png")] // <-- new line 
extern crate image_load_png; 
0

依赖标记为可选双作为特征。但是,如果您想要名称不同的功能,则必须使用define it manually

例如,如果您将依赖关系image_load_png标记为可选项,则只有在启用image_load_png功能时才会编译image_load_png。您可以测试Rust代码中启用的功能,就像其他功能一样。

[dependencies.image_load_png] 
path = "png" 
optional = true 
相关问题