rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Newtype enums but with global constants

Open zopsicle opened this issue 3 years ago • 3 comments

Input C/C++ Header

typedef enum VkResult {
    VK_SUCCESS = 0,
    VK_NOT_READY = 1,
    // ...
} VkResult;

Bindgen Invocation

$ bindgen --no-prepend-enum-name --newtype-enum VkResult input.h

Actual Results

#[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct VkResult(pub ::std::os::raw::c_int);
impl VkResult {
    pub const VK_SUCCESS: VkResult = VkResult(0);
}
impl VkResult {
    pub const VK_NOT_READY: VkResult = VkResult(1);
}
// ...

Expected Results

It would be nice if the variants wouldn't be associated constants when --no-prepend-enum-name is specified, but instead would be module constants. Since that would be a breaking change perhaps there could be a new flag like --newtype-global-enum. What do you think?

#[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct VkResult(pub ::std::os::raw::c_int);
pub const VK_SUCCESS: VkResult = VkResult(0);
pub const VK_NOT_READY: VkResult = VkResult(1);
// ...

zopsicle avatar Jul 03 '22 19:07 zopsicle

Sorry for the lag here. Seems reasonable and should be easy to implement, patch welcome :)

emilio avatar Jul 25 '22 09:07 emilio

(That said maybe just --raw-line "pub use VkResult::*" or so would be an appropriate workaround?)

emilio avatar Jul 25 '22 09:07 emilio

You can't use associated constants :')

zopsicle avatar Jul 25 '22 09:07 zopsicle