linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* libgpiod: rust bindings and bindgen issue with C enums
@ 2022-11-22 15:37 Bartosz Golaszewski
  2022-11-22 16:55 ` Miguel Ojeda
  0 siblings, 1 reply; 13+ messages in thread
From: Bartosz Golaszewski @ 2022-11-22 15:37 UTC (permalink / raw)
  To: Miguel Ojeda, Viresh Kumar, Kent Gibson; +Cc: open list:GPIO SUBSYSTEM

Hi!

The rust bindings are now in the master branch and I started working
on some additional changes for v2. Among other: using C enum types
explicitly in the core library as suggested by Kent as even though the
C standard says:

---
The expression that defines the value of an enumeration constant shall
be an integer constant expression that has a value representable as an
int.
---

and virtually all compilers store enum variables as signed integers,
modern compilers can spot some errors when using explicit types and it
also benefits IDEs which can interpret them better than ints.

That however led to a problem I'm seeing with rust bindings: bindgen
seems to set the type of an enum constant to ::std::os::raw::c_uint
unless that enum has at least one value explicitly set to a negative
number, in which case the type is ::std::os::raw::c_int.

In order to allow gpiod_line_request_get_value() to still return an
error, I added an additional value to enum gpiod_line_value:
GPIOD_LINE_VALUE_INVALID = -1. Now this one enum in bindgen generated
code has become c_int while all others are still c_uint which - as you
can imagine - leads to all kinds of build-time errors due to strong
typing.

As enums are naturally signed integers in the C world - can we somehow
make bindgen default to c_int for all enum types?

Bartosz

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-11-25  5:49 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-22 15:37 libgpiod: rust bindings and bindgen issue with C enums Bartosz Golaszewski
2022-11-22 16:55 ` Miguel Ojeda
2022-11-22 19:08   ` Bartosz Golaszewski
2022-11-23 18:37     ` Bartosz Golaszewski
2022-11-24 10:45       ` Viresh Kumar
2022-11-24 13:32         ` Kent Gibson
2022-11-24 14:27           ` Bartosz Golaszewski
2022-11-25  5:49             ` Viresh Kumar
2022-11-24 14:46           ` Miguel Ojeda
2022-11-24 14:58             ` Kent Gibson
2022-11-24 16:18               ` Miguel Ojeda
2022-11-25  1:48                 ` Kent Gibson
2022-11-25  5:48           ` Viresh Kumar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).