Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document somewhere that enums can be either c_int or c_uint #1966

Open
saschanaz opened this issue Jan 10, 2021 · 4 comments
Open

Document somewhere that enums can be either c_int or c_uint #1966

saschanaz opened this issue Jan 10, 2021 · 4 comments

Comments

@saschanaz
Copy link

Input C/C++ Header

enum TestEnum {
  FOO,
  BAR
};

Bindgen Invocation

$ bindgen input.h

Actual Results

On Windows:

/* automatically generated by rust-bindgen 0.56.0 */

pub const TestEnum_FOO: TestEnum = 0;
pub const TestEnum_BAR: TestEnum = 1;
pub type TestEnum = ::std::os::raw::c_int;

On Ubuntu:

/* automatically generated by rust-bindgen 0.56.0 */

pub const TestEnum_FOO: TestEnum = 0;
pub const TestEnum_BAR: TestEnum = 1;
pub type TestEnum = ::std::os::raw::c_uint;

Expected Results

This should be documented somewhere. All I found is this comment: #1096 (comment)

Bindgen uses whatever enum type comes from clang, so if it's a u32, clang is picking a u32 type for it.

saschanaz added a commit to saschanaz/jxl-rs that referenced this issue Jan 10, 2021
tanriol added a commit to tanriol/libftdi1-sys that referenced this issue Jan 29, 2021
Turns out (rust-lang/rust-bindgen#1966) that clang uses different
underlying type for enums in different systems, which causes
insignificant differences between platforms. Let's be consistent.
@emilio
Copy link
Contributor

emilio commented Feb 2, 2021

A more precise definition is that bindgen will use std::underlying_type_t<Enum>. But yeah, adding docs for this would be great.

@Lokathor
Copy link
Contributor

Lokathor commented Mar 5, 2021

can we have an option to just always make a c_int on all platforms, because the signedness only matters to Rust and C doesn't care?

Nufflee added a commit to Nufflee/imgui-rs that referenced this issue Mar 4, 2022
The Win32 and OpenGL3 built-in ImGui backends are now compiled into the Rust library. Bindings are not generated for them so the user needs to create them manually, but this is the first step.
Revert update to latest cimgui and regenerated bindings

Note: do not update to latest cimgui version or regenerate the bindings on Windows due to this bug: rust-lang/rust-bindgen#1966
@ghost
Copy link

ghost commented Mar 15, 2022

Resurrecting this thread because we just stumbled upon the same issue. In our case we have something like the following:

pub const my_enum_VALUE_0: my_enum = 0;
pub const my_enum_VALUE_1: my_enum = 1;
...
pub type my_enum = u32;

I believe the problem here is that the enumeration constants should always be int, as specified by the C standard, rather than my_enum. The type of my_enum itself can be whatever clang spits out. In any case, it would be nice to have an option to control this as @Lokathor suggested.

@FSMaxB
Copy link

FSMaxB commented Aug 12, 2022

Just stumbled over this as well. For reference: https://stackoverflow.com/a/366033/1717115

Basically the C standard specifies that an enum value always needs to be representable by int, although the compiler can choose different types for the actual representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants