Cargo features and the [patch] section in Cargo.toml


#1

Hi,

I have some code which I would like to be able to compile with and without a crate (in this case openexr).

I think I know what to do in my code:

...
#[cfg(feature="openexr")]
extern crate openexr;
...
#[cfg(feature="openexr")]
use openexr::{FrameBuffer, ...};
...
impl InfiniteAreaLight {
    #[cfg(not(feature="openexr"))]
    pub fn new(...) -> Self { ... }
    #[cfg(feature="openexr")]
    pub fn new(...) -> Self { ... }
...
}
...

In my Cargo.toml file I define a feature:

...
[features]
default = ["openexr"]
...
[dependencies]
...
openexr = { version = "0.5", optional = true }
...

That way I can compile either with the openexr crate:

cargo test --release

or without the crate/feature:

cargo test --release --no-default-features

My problem is that, if I do use the crate, I also want to patch it, and I can’t figure out how to specify this in Cargo.toml:

[patch.crates-io]
openexr = { path = "openexr-rs-master", optional = true }

It works fine if I first compile with the crate/feature and then without (which assumes that I do have a local copy of the openexr crate checked out in a subdirectory called openexr-rs-master and that it in fact will compile):

wget https://github.com/cessen/openexr-rs/archive/master.zip
unzip master.zip

What I want to achieve (for a platform where openexr would not compile, even if patched) is that both, the dependencies on the crate, as well as the patch, can be turned on or off.

Any ideas how to achieve this?