Re: [PATCH 1/4] rust: dma: implement DataDirection

From: Miguel Ojeda
Date: Mon Aug 18 2025 - 08:00:02 EST


On Mon, Aug 18, 2025 at 1:27 PM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
>
> Does bindgen ever pick another type than i32 for C enums? If so, it'd be a
> downside that we'd have to mess with the type either in the `repr` or by casting
> the variants.

Yes, it can easily pick `u32` -- the variants are always `int` in C,
but the actual enum can be a type compatible with int, unsigned or
char; and `bindgen` generates each variant with a type alias to the
underlying type of the `enum`, not `int`.

So e.g. for

enum uint_enum { uint_enum_a, uint_enum_b };
enum int_enum { int_enum_a = -1, int_enum_b };

_Static_assert(_Generic(uint_enum_a, int: 1, default: 0), "");
_Static_assert(_Generic(uint_enum_b, int: 1, default: 0), "");
_Static_assert(_Generic(int_enum_a, int: 1, default: 0), "");
_Static_assert(_Generic(int_enum_b, int: 1, default: 0), "");

_Static_assert(_Generic((enum uint_enum)0, unsigned: 1, default: 0), "");
_Static_assert(_Generic((enum int_enum)0, int: 1, default: 0), "");

you get:

pub const uint_enum_uint_enum_a: uint_enum = 0;
pub const uint_enum_uint_enum_b: uint_enum = 1;
pub type uint_enum = ffi::c_uint;

pub const int_enum_int_enum_a: int_enum = -1;
pub const int_enum_int_enum_b: int_enum = 0;
pub type int_enum = ffi::c_int;

Then there is this issue I reported to be careful about, which can
change it back to `i32` if there is a forward reference:

https://github.com/rust-lang/rust-bindgen/issues/3179

I hope that helps.

Cheers,
Miguel