I am working on a little tool using Opencv. I have two envs, dev contains Opencv 4.11, and prod provides Opencv 4.6. There are some API changes between 4.11 and 4.6. And the crate opencv does provide some consts containing the version parts.
How do I compile different function calls depends on the consts?
You can't use consts for conditional compilation. Cargo's feature machanism would be the right tool to use here. It allows you to switch dependency versions (and feature sets) in your Cargo.toml, plus enabling you to use #[cfg(feature = "dev")] etc. to conditionally compile your own crate.
Sorry for hijacking this topic, I have a similar question.
My proc-macro crate generates a function f that should either have return type X or Y. Currently the proc-macro user has to explicitly specify this when using the macro, but it could be determined automatically, if there was a way for conditional compilation based on a const value. Is there a tracking issue for something like:
const B: bool = ...;
// either
fn f() -> if B { X } else { Y } {
match B {
true => return X(...),
false => return Y(...),
}
}
// or
if B { fn f() -> X } else { fn f() -> Y } { /* .. */ }
// or
#[cfg(B)] fn f() -> X { /* .. */ }
#[cfg(not(X)] fn f() -> Y { /* .. */ }
// or any other syntax