Use case: for the FFmpeg mapping rust-ffmpeg-sys, I want to ensure it stays compatible with many FFmpeg versions. Some struct fields' existence can only be decided by version number. I can easily retrieve those numbers via build.rs
(compile a C program that outputs the definition).
But then how can I conditionally add struct fields in Rust code if that found version number is greater than some constant X?
Here's an already working example based on FFmpeg feature definitions:
#[repr(C)]
pub struct AVCodecContext {
// [...]
#[cfg(any(feature="ff_api_codec_name", not(feature="ff_api_codec_name_is_defined")))]
pub codec_name: [c_char; 32],
I'd like to have something like that for version numbers, pseudocode:
#[repr(C)]
pub struct AVCodecContext {
// [...]
// Decide struct field size based on FFmpeg version number
#[cfg(version_greater_than("some_variable_set_by_build_rs", "some version number constant to compare against"))]
pub bit_rate: int64_t,
#[cfg(not(version_greater_than("some_variable_set_by_build_rs", "some version number constant to compare against")))]
pub bit_rate: c_int,
How can I easily make this work? I am hoping this could be a feature of cfg!
but didn't find anything similar. Another idea was to auto-generate a Rust source file with the FFmpeg version like let ffmpeg_version_major : int = ...
and then somehow use it in cfg!
. Is a greater-than comparison possible in any way?