rusty_ffmpeg
                                
                                
                                
                                    rusty_ffmpeg copied to clipboard
                            
                            
                            
                        Better enum type name
Current we got enum like this:
pub const AVMediaType_AVMEDIA_TYPE_NB: AVMediaType = 5;
Which is not good to use. And the rust-bindgen provides us several options: https://docs.rs/bindgen/0.55.1/bindgen/struct.Builder.html
However they all not suitable for out situation. I want the generated enum can be directly used(rustified_enum creates const in impl, which cannot be used just like the bitflags crate does), and is able to do calculation(which makes newtype_enum not suitable, it construct a new_type containing a integer), and extendable(some consts cnanot be directly generated with bindgen, which need to be add by hand, but generate const value in a specific module makes adding them not possible), The bitfield_enum is better but each enum only occupies a bit, which makes the maximum number of each enum is limited.
The last option is trimming the prefix of each enum, which is suitable since FFmpeg's enum name never overlaps. So it's blocked on https://github.com/rust-lang/rust-bindgen/issues/777
The problem is the callback receives unprefixed identifier, like the AVMEDIA_TYPE_VIDEO for AVMediaType_AVMEDIA_TYPE_VIDEO, which makes it impossible to trim out the AVMediaType. So depending on the https://github.com/rust-lang/rust-bindgen/issues/777 is impossible.
is the idea to make enum out of the types, but it's hard to determine what actually is within said enum?
Current we got enum like this:
pub const AVMediaType_AVMEDIA_TYPE_NB: AVMediaType = 5;Which is not good to use. And the rust-bindgen provides us several options: https://docs.rs/bindgen/0.55.1/bindgen/struct.Builder.html
However they all not suitable for out situation. I want the generated enum can be directly
used (rustified_enumcreates const in impl, which cannot beused just like the bitflags crate does), and is able to do calculation (which makesnewtype_enumnot suitable, it construct a new_type containing a integer), and extendable(some consts cnanot be directly generated with bindgen, which need to be add by hand, but generate const value in a specific module makes adding them not possible), Thebitfield_enumis better but each enum only occupies a bit, which makes the maximum number of each enum is limited.The last option is trimming the prefix of each enum, which is suitable since FFmpeg's enum name never overlaps. So it's blocked on rust-lang/rust-bindgen#777
What exactly is doing math on the constants useful for? I see no other issues with newtype_enum if not for that. You could also manually define a macro_rules! setup afterwards to allow doing math on it.
EDIT 2: bindgen::Builder::raw_line<T>(self, &T) exists; that can be used to add in extra enums.
EDIT 3: I noticed you're simply inlining binding.rs directly into the output file; you could just as well inline the extra enum constants.
What exactly is doing math on the constants useful for?
Enums defined as bitflags are widely used in FFmpeg, e.g., you need to do math on them for sure.
you could just as well inline the extra enum constants.
Yeah, but defining extra enums has several drawbacks:
- It's confusing that there are two same looking definitions, users won't get it clear and usage will messed up by the two variants.
 - Auto-generated functions accept and emit enum as i32. Users need to cast the enum back and forth which looks awkward.
 
I still think it would be better to let bindgen define the enum. Until it is capable, we should use i32 instead of our own defined enums.
Auto-generated functions accept and emit enum as i32. Users need to cast the enum back and forth which looks awkward.
If bindgen defines the enum, it'll do this:
extern "C" {
    pub fn avcodec_get_type(codec_id: AVCodecID) -> AVMediaType;
}
Yeah, but extra enums has several drawbacks
I might not have been clear enough about it. Currently, lib.rs defines the ffi module as:
pub mod ffi {
    pub use crate::avutil::{_avutil::*, common::*, error::*, pixfmt::*, rational::*};
    include!(concat!(env!("OUT_DIR"), "/binding.rs"));
}
Since impls can be split out over multiple blocks, we could add extra pixel formats like so:
pub mod ffi {
    pub use crate::avutil::{_avutil::*, common::*, error::*, /*pixfmt::*,*/ rational::*};
    include!(concat!(env!("OUT_DIR"), "/binding.rs"));
    impl AVPixelFormat {
        pub const AV_PIX_FMT_RGB32: AVPixelFormat = // ...
    }
}
EDIT:
Enums defined as bitflags are widely used in FFmpeg
bindgen has bitfield_enum() for this.