如何使本地依赖取决于货物的功能?

ide*_*n42 3 dependencies rust rust-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"
Run Code Online (Sandbox Code Playgroud)

在阅读文档时,这将显示如何使用可选的外部依赖项.在上面的示例中,我使用的是一个本地子目录,我想要构建或不构建 - 基于一个功能.

如何image_load_pnguse_png启用该功能时仅进行构建.

ide*_*n42 5

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

[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
Run Code Online (Sandbox Code Playgroud)

使用包可以是可选的.

例如:

#[cfg(feature = "use_png")]  // <-- new line
extern crate image_load_png;
Run Code Online (Sandbox Code Playgroud)